在复合 C1 中,是否可以将页面元类型字段传递给 razor 函数



我浏览了 C1 文档,似乎找不到任何将页面元类型字段传递给 razor 函数的文档或示例。

我将为我正在做的 POC 举一个非常简单的例子。我创建了一个页面元类型的页面,该页面具有tag字段。这个tag字段将被传递给一个 razor 函数,该函数将在 Flickr 中查询使用该字段中包含的值标记的所有图像。我也不想在 Razor 函数本身中配置它,以防在不使用此页面元类型的页面上使用 Razor 函数。

更新

据我所知,您不能说使用常量或从函数参数对话框中选择元类型字段,因此我只是添加了一个允许您选择元类型字段的参数。见下文。

@inherits RazorFunction
@functions {
    public override string FunctionDescription
    {
        get  { return "Some tweets based on keyword"; }
    }
    [FunctionParameter(Label="Flickr Data")]
    public DataReference<MyNamespace.Flickr> FlickrData { get;set; }
    [FunctionParameter(Label="Maximum tweets to show", DefaultValue = 5)]
    public int MaxItems { get; set; }
}
@{
    XNamespace atom = "http://www.w3.org/2005/Atom";
    var feed = XDocument.Load("https://api.flickr.com/services/feeds/photos_public.gne?tags=" + this.FlickrData.Data.FlickrTag);
    var items = feed.Descendants(atom + "entry").Take(this.MaxItems).Select(item => new {
        Title = item.Element(atom + "title").Value,
        Published = item.Element(atom + "published").Value,
        Markup = item.Element(atom + "content").Value
    });
}
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://www.composite.net/ns/function/1.0">
    <head>
    </head>
    <body>
    <p>
        @Html.Raw(@Resources.FlickrAtomFeed.FlickrFeedForMessage) <strong>@FlickrData.Data.FlickrTag</strong>
    </p>
        <dl>
            @foreach (var item in items) {
                <dt>@Html.Raw(@item.Markup)</dt>
                <dd>@item.Title
                    <em>@DateTime.Parse(item.Published).ToString("F"))</em>
                </dd>
            }
        </dl>
    </body>
</html>

从那里,如果我想在页面上使用该函数,我只需要确保该元类型字段位于该页面上。事实上,如果您尝试将函数添加到没有元类型字段的页面,它不会让您保存函数参数,因此很明显元类型字段需要位于该页面上。仍然不是我想要的,但它有效。

简而言之,如果您的函数插入到页面上,您可以:

  1. 获取当前页面 ID。(请参阅 http://docs.composite.net/Functions/Composite-C1-API/Sitemap-Navigator)
  2. 查询特定数据项(此页面上的标记)的元类型。(请参阅 http://docs.composite.net/Functions/Razor/Getting-C1-Data 和 http://docs.composite.net/Legacy/Razor/List-to-Details 以获取灵感)
  3. 处理检索到的数据。

该函数可以像这样(假设您的页面元类型是"My.Meta.Type"):

@using Composite.Data
@inherits RazorFunction
@functions {
    public override string FunctionDescription
    {
        get  { return "..."; }
    }   
}
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://www.composite.net/ns/function/1.0">
    <head>
    </head>
    <body>
        @using (DataConnection dataConnection = new DataConnection())
        {
            // (1) get the current page ID
            SitemapNavigator sitemapNavigator = new SitemapNavigator(dataConnection);
            Guid PageId = sitemapNavigator.CurrentPageNode.Id;
            if (PageId != null)
            {
            // (2) Get the meta field from the current page
                var myTag = Data.Get<My.Meta.Type>().Where(t => t.PageId == PageId).FirstOrDefault();
            // (3) Process the tag the way you like
                if(myTag != null)
                { 
                    <p>@myTag.Tag</p>
                }
            }
        }
    </body>
</html>

最新更新