Sitecore Date format using Sitecore (). Field ()?

I need to use a custom format for a date (i.e. dddd dd MMMM yyyy ). Can this format be passed to Sitecore().Field() ? I would like to do something like this:

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

However, after some Googling, I found that I need to create a special field helper for this or a custom model. Is there no way to do this using the base Sitecore? This is important to do with Sitecore().Field() , since I need a content editor to be able to edit the value.

We are on Sitecore 7.5

+5
source share
2 answers

As far as I know, Sitecore does not have such functionality out of the box. You can use the helper for this function, please check the code below. I used this code and it works great. You can also edit the date field from the page editor, because the field is edited through the Sitecore pipelines.

 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); } } 

In your cshtml you will have:

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

John West wrote about how to extend sitcom assistants here: 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

+4
source

I changed @SitecoreClimber's answer because it does not work for rendering child elements since it uses the default value RenderingContext.Current

So, I updated all the arguments to the method and added an Item item and used this element to render the base field, for example:

  public static HtmlString RenderField(this SitecoreHelper sitecoreHelper, string fieldNameOrId, Item item, bool disableWebEdit = false, SafeDictionary<string> parameters = null) { if (parameters == null) { parameters = new SafeDictionary<string>(); } return sitecoreHelper.Field(fieldNameOrId, item, new { DisableWebEdit = disableWebEdit, Parameters = parameters }); } 

So, in my MVC view, I can now have:

 @foreach (Item item in @Model.Item.Children) { <div class="event-date"> @Html.Sitecore().RenderDate("Date", item, "d MMM") </div> } 
0
source

Source: https://habr.com/ru/post/1239226/


All Articles