Umbraco razor 模板 - 从参数中指定的字段中获取格式化日期 [英] Umbraco razor template - Get Formatted Date From Field specified in parameter

查看:52
本文介绍了Umbraco razor 模板 - 从参数中指定的字段中获取格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 umbraco 中的剃刀模板返回格式化的日期.不过,我不确定如何从参数中定义的字段中获取值.

I'm trying to return a formatted date from a razor template in umbraco. I'm not sure how to get a value from a field defined in a parameter though.

这是我正在使用的代码.我传入的字段称为articleDate".我正在获取参数值输出,但是当我尝试使用参数名称获取字段的值时,它不返回任何内容.如果我通过字段名称本身要求值,那行得通.如何创建这样的通用宏?

Here is the code I'm playing with. The field I'm passing in is called "articleDate". I'm getting the parameter value output, however when I try to get the value of the field using the parameter name it returns nothing. If I ask for the value by the field name itself, that works. How can I create a generic macro like this?

@{var param = @Parameter.dateField;}
Field Name: @param
<br/>
Field Value: @Model.param
<br/>    
Field Value: @Model.articleDate

我也尝试过使用 @Model.GetDynamicMember(..),但这只是引发异常.

I tried using @Model.GetDynamicMember(..) as well, but that just throws an exception.

Field Value: @Model.GetDynamicMember("articleDate");​

Error loading Razor Script getDate.cshtml
Cannot invoke a non-delegate type

有人能指出我正确的方向吗?我只是想创建一个简单的宏,我可以用它来格式化页面上的日期.

Can someone point me in the right direction? I'm just trying to create a simple macro I can use to format dates across my page.

是否可以将我的日期值直接传递到 razor 宏中?这就是我目前的称呼:

Is it possible to pass the value of my date directly into the razor macro? This is how I'm currently calling it:

<umbraco:Macro ID="Macro1" Alias="getDate" dateField="articleDate" runat="server"></umbraco:Macro>

推荐答案

如果你像你写的那样调用你的宏:

If you call your Macro like you wrote:

<umbraco:Macro ID="Macro1" Alias="getDate" dateField="articleDate" runat="server"></umbraco:Macro>

您实际上是在传递字段articleDate"的名称.在宏中,您可以使用以下方法获取模型的 articleDate 属性的值:

You're actually passing the name of the field "articleDate". In the macro, you then may get the value of the Model's articleDate property by using:

Field Value: @Model.getProperty(Parameter.dateField).Value

我还建议使用 helpers 代替宏,或者 - 对于更复杂的脚本 - RenderPage.一个不错的文章可以在这里找到:http://joeriks.wordpress.com/2011/03/11/better-structure-for-your-razor-scripts-with-renderpage-in-umbraco/

Instead of macro's I also recommend using helpers or - for more complex scripts - RenderPage. A nice writeup can be found here: http://joeriks.wordpress.com/2011/03/11/better-structure-for-your-razor-scripts-with-renderpage-in-umbraco/

示例:

@helper GetDate(dynamic dateField)
{
     @dateField.ToString("[yourFormat]")
}

您可以使用 Page 对象将参数传递给使用 RenderPage 呈现的脚本:

You may pass parameters to scripts rendered with RenderPage by using the Page object:

<umbraco:macro language="razor" runat="server">
@{
    Page.dateField=Model.articleDate;
}
@RenderPage("~/macroscripts/getdate.cshtml")
</umbraco:macro>

getdate.cshtml:

getdate.cshtml:

@Page.datefield.ToString("[yourFormat]")

这篇关于Umbraco razor 模板 - 从参数中指定的字段中获取格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆