c# - 使用 Sitecore().Field() 格式化 Sitecore 日期?

标签 c# sitecore sitecore7 sitecore7.5

我需要为日期使用自定义格式(即 dddd dd MMMM yyyy)。是否可以将此格式传递给 Sitecore().Field()?我想做这样的事情:

@Html.Sitecore().Field("Day1", new { @format="dddd dd MMMM yyyy"})

但是,在谷歌搜索之后,我发现我要么必须创建一个自定义字段助手来执行此操作,要么必须创建一个自定义模型。使用基本 Sitecore 真的没有办法做到这一点吗?这通过 Sitecore().Field() 完成很重要,因为我需要内容编辑器能够编辑值。

我们使用的是 Sitecore 7.5

最佳答案

据我所知,Sitecore 没有开箱即用的功能。 您可以使用帮助程序来实现此功能,请检查以下代码。 我使用了这段代码并且工作正常。您也可以从页面编辑器编辑日期字段,因为该字段是通过 Sitecore 管道编辑的。

public static class Helper
{

    public static HtmlString RenderField(
      this SC.Mvc.Helpers.SitecoreHelper sitecoreHelper,
      string fieldNameOrId,
      bool disableWebEdit = false,
      SC.Collections.SafeDictionary<string> parameters = null)
    {
        if (parameters == null)
        {
            parameters = new SC.Collections.SafeDictionary<string>();
        }

        return sitecoreHelper.Field(
          fieldNameOrId,
          new
            {
                DisableWebEdit = disableWebEdit,
                Parameters = parameters
            });
    }

    public static HtmlString RenderField(
      this SC.Mvc.Helpers.SitecoreHelper sitecoreHelper,
      SC.Data.ID fieldId,
      bool disableWebEdit = false,
      SC.Collections.SafeDictionary<string> parameters = null)
    {
        return RenderField(
          sitecoreHelper,
          fieldId.ToString(),
          disableWebEdit,
          parameters);
    }

    public static HtmlString RenderDate(
      this SC.Mvc.Helpers.SitecoreHelper sitecoreHelper,
      string fieldNameOrId,
      string format = "D",
      bool disableWebEdit = false,
      bool setCulture = true,
      SC.Collections.SafeDictionary<string> parameters = null)
    {
        if (setCulture)
        {
            Thread.CurrentThread.CurrentUICulture =
              new CultureInfo(SC.Context.Language.Name);
            Thread.CurrentThread.CurrentCulture =
              CultureInfo.CreateSpecificCulture(SC.Context.Language.Name);
        }

        if (parameters == null)
        {
            parameters = new SC.Collections.SafeDictionary<string>();
        }

        parameters["format"] = format;
        return RenderField(
          sitecoreHelper,
          fieldNameOrId,
          disableWebEdit,
          parameters);
    }

    public static HtmlString RenderDate(
      this SC.Mvc.Helpers.SitecoreHelper sitecoreHelper,
      SC.Data.ID fieldId,
      string format = "D",
      bool disableWebEdit = false,
      bool setCulture = true,
      SC.Collections.SafeDictionary<string> parameters = null)
    {
        return RenderDate(
          sitecoreHelper,
          fieldId.ToString(),
          format,
          disableWebEdit,
          setCulture,
          parameters);
    }

    public static HtmlString TagField(
      this SC.Mvc.Helpers.SitecoreHelper sitecoreHelper,
      string fieldNameOrId,
      string htmlElement,
      bool disableWebEdit = false,
      SC.Collections.SafeDictionary<string> parameters = null)
    {
        SC.Data.Items.Item item =
          SC.Mvc.Presentation.RenderingContext.Current.ContextItem;

        if (item == null || String.IsNullOrEmpty(item[fieldNameOrId]))
        {
            return new HtmlString(String.Empty);
        }

        string value = sitecoreHelper.RenderField(
          fieldNameOrId,
          disableWebEdit,
          parameters).ToString();
        return new HtmlString(String.Format(
          "<{0}>{1}</{0}>",
          htmlElement,
          value));
    }

    public static HtmlString TagField(
      this SC.Mvc.Helpers.SitecoreHelper sitecoreHelper,
      SC.Data.ID fieldId,
      string htmlElement,
      bool disableWebEdit = false,
      SC.Collections.SafeDictionary<string> parameters = null)
    {
        return TagField(
          sitecoreHelper,
          fieldId.ToString(),
          htmlElement,
          disableWebEdit,
          parameters);
    }
}

在您的 cshtml 中,您将拥有:

       @Html.Sitecore().RenderDate("Name of field or id", "your format")

John West 在这里写了关于如何扩展 sitecore 助手的文章: http://www.sitecore.net/learn/blogs/technical-blogs/john-west-sitecore-blog/posts/2012/06/sitecore-mvc-playground-part-4-extending-the-sitecorehelper-class.aspx

关于c# - 使用 Sitecore().Field() 格式化 Sitecore 日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34467044/

相关文章:

验证 - 渲染项目是否有效 xhtml - 异常

xml - Sitecore 7 :Calling one xml control from other xml control

c# - 捕获线程未处理的异常

c# - Linq-to-Entities 动态排序

c# - 在运行时检查 Glass Mapper V3 类型

url-rewriting - 如何重写或缩短 Sitecore URL

javascript - 或者在 2 个文本框中键入验证 - ASP.NET Webforms

c# - 使用 Linq 进行数学统计

sitecore - 在 Sitecore 7.2 中获取页面级测试结果

c# - Sitecore 检查索引是否完整