果园投影页面默认视图

时间:2014-05-29 21:24:14

标签: orchardcms orchardcms-1.8

我正在使用Orchard 1.8并创建了一个新的内容类型(称为PressRelease),查询结果和投影以使用自定义模板查看查询(使用格式为List-ProjectionPage-url-PressRelease.cshtml的URL替换)和所有这很好。

让我难以理解的一部分是,如果我使用主题机作为我的主题(未触动),此投影视图将显示在无序列表中,其中包含指向各个ContentItem实体的相应AutoRoute链接,其元数据等等上。我正在试图弄清楚我如何访问诸如特定项目的AutoRoute URL,元数据(创建/发布日期)等等,以便与Facebook Share按钮等内容一起使用。基本上我正在尝试重新创建默认视图,尽管有自定义。

以下是List-ProjectionPage-url-PressRelease.cshtml的代码:

@using Orchard.Utility.Extensions;
@using System.Linq

@functions
{
public class PressRelease
{
    public PressRelease()
    {
        this.Attachments = new List<Attachment>();
    }

    public string Title { get; set; }
    public string Source { get; set; }
    public DateTime PublishDate { get; set; }
    public string Body { get; set; }
    public List<Attachment> Attachments { get; set; }
}

    public class Attachment
    {
        public string Filename { get; set; }
        public string Path { get; set; }
    }
}

@{
    //add list of dynamic objects to strongly typed class
    var releases = new List<PressRelease>();
    foreach (var item in @Model.Items)
    {

    var release = new PressRelease
    {
        Title = item.ContentItem.TitlePart.Title,
        Source = item.ContentItem.PressRelease.Source.Value,
        PublishDate = item.ContentItem.PressRelease.Date.DateTime,
        Body = item.ContentItem.BodyPart.Text
    };

    //load attachment(s) to class
    var attachments = (Orchard.MediaLibrary.Fields.MediaLibraryPickerField)item.ContentItem.PressRelease.Attachment;
    if (attachments.MediaParts.Count() > 0)
    {
        foreach (var part in attachments.MediaParts)
        {
            release.Attachments.Add(new Attachment { Filename = part.FileName, Path = part.MediaUrl });
        }
    }

    releases.Add(release);
    }
}
@{
foreach (var item in releases)
{
    <div class="press-release">
        <div class="press-release-title">@item.Title</div>
        <div class="press-release-meta">
            <span class="press-release-source">Source: @item.Source</span>
            @if (item.PublishDate != DateTime.MinValue)
            {
                <span class="press-release-date">@item.PublishDate.ToShortDateString()</span>
            }
        </div>
        @if (item.Attachments.Count() > 0)
        {
            <div class="press-release-attachments">
                <span class="press-release-attachments-title">Attached: </span>
                @foreach (var attachment in item.Attachments)
                {
                    var linkText = attachment.Filename;
                    var url = attachment.Path;
                    @Html.Link(linkText, url);
                    if (attachment != item.Attachments.Last())
                    {
                        <span>, </span>
                    }
                }
            </div>
        }
        <div class="press-release-body">
            <p>@Html.Raw(item.Body.Replace("\r\n", "<br />"))</p>
        </div>
    </div>
    <div class="social">
        <!-- ** This is where I need AutoRoute URL so I can do FB share link **-->
        <div class="fb-share-button" data-href="" data-type="button_count"></div>
    </div>
    if (item != releases.Last())
    {
        <hr />   
    }
}
}

思想?

1 个答案:

答案 0 :(得分:0)

利用Shape Tracer(结合@ Bertrand在上述评论中提供的帮助)帮助我找到了我需要的地方。这是我使用的最终布局代码(其中包含一些超级hacky内容):

@using Orchard.Utility.Extensions;
@using System.Linq

@functions
{
    public class PressRelease
    {
        public PressRelease()
        {
            this.Attachments = new List<Attachment>();
        }

        private string _NavigateUrl = string.Empty;

        public string Title { get; set; }
        public string Source { get; set; }
        public DateTime PublishDate { get; set; }
        public string Body { get; set; }
        public List<Attachment> Attachments { get; set; }
        public string NavigateUrl
        {
            get { return string.Format("{0}://{1}/{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, _NavigateUrl); }
            set { this._NavigateUrl = value; }
        }
    }

    public class Attachment
    {
        public string Filename { get; set; }
        public string Path { get; set; }
    }
}

@{
    //add list of dynamic objects to strongly typed class
    var releases = new List<PressRelease>();
    foreach (var item in @Model.Items)
    {

        var release = new PressRelease
        {
            Title = item.ContentItem.TitlePart.Title,
            Source = item.ContentItem.PressRelease.Source.Value,
            PublishDate = item.ContentItem.PressRelease.Date.DateTime,
            //this is super hacky to get a chopped version of the HTML submitted for a summary
            Body = item.ContentItem.BodyPart.Text,
            NavigateUrl = item.ContentItem.AutoroutePart.Path
        };

        //load attachment(s) to class
        var attachments = (Orchard.MediaLibrary.Fields.MediaLibraryPickerField)item.ContentItem.PressRelease.Attachment;
        if (attachments.MediaParts.Count() > 0)
        {
            foreach (var part in attachments.MediaParts)
            {
                release.Attachments.Add(new Attachment { Filename = part.FileName, Path = part.MediaUrl });
            }
        }

        releases.Add(release);
    }
}
@{
    foreach (var item in releases)
    {
        <div class="press-release">
            <div class="press-release-title"><a href="@item.NavigateUrl">@item.Title</a></div>
            <div class="press-release-meta">
                <span class="press-release-source">Source: @item.Source</span>
                @if (item.PublishDate != DateTime.MinValue)
                {
                    <span class="press-release-date">@item.PublishDate.ToShortDateString()</span>
                }
            </div>
            @if (item.Attachments.Count() > 0)
            {
                <div class="press-release-attachments">
                    <span class="press-release-attachments-title">Attached: </span>
                    @foreach (var attachment in item.Attachments)
                    {
                        <a href="@attachment.Path" target="_blank">@attachment.Filename</a>
                        if (attachment != item.Attachments.Last())
                        {
                            <span>, </span>
                        }
                    }
                </div>
            }
            <div class="press-release-body">
                @{
                    var body = new HtmlString(Html.Excerpt(item.Body, 200).ToString().Replace(Environment.NewLine, "</p>" + Environment.NewLine + "<p>"));
                    <p>@body <a href="@item.NavigateUrl">(read more)</a></p>
                }
            </div>
        </div>
        <div class="social">
            <div class="fb-share-button" data-href="@item.NavigateUrl" data-type="button_count"></div>
        </div>
        if (item != releases.Last())
        {
            <hr />
        }
    }
}