-
-
Save dervalp/9f4b3eb636accf4f30d2 to your computer and use it in GitHub Desktop.
Razor vs C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Sitecore.Web.UI.Controls.Common.Texts | |
{ | |
using System.Web.UI; | |
using Sitecore.Diagnostics; | |
using Sitecore.Mvc.Presentation; | |
/// <summary> | |
/// The type of text. | |
/// </summary> | |
public enum TextType | |
{ | |
/// <summary>The text.</summary> | |
Text, | |
/// <summary>The label.</summary> | |
Label, | |
/// <summary>The help label value.</summary> | |
HelpLabel, | |
/// <summary>The large label value.</summary> | |
LargeLabel, | |
/// <summary>The value.</summary> | |
Value, | |
/// <summary>The large value.</summary> | |
LargeValue, | |
/// <summary>The large title.</summary> | |
LargeTitle, | |
/// <summary>The title.</summary> | |
Title, | |
/// <summary>The small title.</summary> | |
SmallTitle, | |
/// <summary>The divider.</summary> | |
Divider | |
} | |
/// <summary> | |
/// The text control. | |
/// </summary> | |
public class Text : ControlBase | |
{ | |
#region Constants | |
/// <summary> | |
/// The default data bind field. | |
/// </summary> | |
private const string DefaultDataBind = "text: text, visible: isVisible"; | |
#endregion | |
#region Constructors | |
/// <summary> | |
/// Initializes a new instance of the <see cref="Text"/> class. | |
/// </summary> | |
public Text() | |
{ | |
this.Class = "sc-text"; | |
this.DataBind = DefaultDataBind; | |
this.Requires.Script(RequireCollection.BusinessLibraryCategory, "text.js"); | |
} | |
/// <summary>Initializes a new instance of the <see cref="Text"/> class.</summary> | |
/// <param name="parametersResolver">The parameters resolver.</param> | |
public Text([NotNull] RenderingParametersResolver parametersResolver) : base(parametersResolver) | |
{ | |
Assert.ArgumentNotNull(parametersResolver, "parametersResolver"); | |
this.Class = "sc-text"; | |
this.DataBind = DefaultDataBind; | |
this.Requires.Script(RequireCollection.BusinessLibraryCategory, "text.js"); | |
this.Content = parametersResolver.GetString("Text", "text"); | |
this.TextType = parametersResolver.GetEnum("TextType", TextType.Text); | |
this.FieldName = parametersResolver.GetString("FieldName"); | |
this.ContentBinding = parametersResolver.GetString("TextBinding"); | |
} | |
#endregion | |
#region Public Properties | |
#region Public properties | |
/// <summary> | |
/// Gets or sets the content. | |
/// </summary> | |
[CanBeNull] | |
public string Content { get; set; } | |
/// <summary> | |
/// Gets or sets the name of the field. | |
/// </summary> | |
/// <value>The name of the field.</value> | |
[CanBeNull] | |
public string FieldName { get; set; } | |
/// <summary> | |
/// Gets or sets the type of the text. | |
/// </summary> | |
/// <value>The type of the text.</value> | |
public TextType TextType { get; set; } | |
#endregion | |
#region Protected properties | |
/// <summary> | |
/// Gets or sets the text binding. | |
/// </summary> | |
[CanBeNull] | |
protected string ContentBinding { get; set; } | |
#endregion | |
#endregion | |
#region Methods | |
/// <summary> | |
/// Called after the control object is created. | |
/// </summary> | |
protected override void PreRender() | |
{ | |
base.PreRender(); | |
if (!string.IsNullOrEmpty(this.ContentBinding)) | |
{ | |
this.AddBinding("text", this.ContentBinding); | |
} | |
switch (this.TextType) | |
{ | |
case TextType.Label: | |
this.AppendClass("sc-text-label"); | |
break; | |
case TextType.HelpLabel: | |
this.AppendClass("sc-text-helplabel"); | |
break; | |
case TextType.LargeLabel: | |
this.AppendClass("sc-text-largelabel"); | |
break; | |
case TextType.Value: | |
this.AppendClass("sc-text-value"); | |
break; | |
case TextType.LargeValue: | |
this.AppendClass("sc-text-largevalue"); | |
break; | |
case TextType.LargeTitle: | |
this.AppendClass("sc-text-largetitle"); | |
break; | |
case TextType.Title: | |
this.AppendClass("sc-text-title"); | |
break; | |
case TextType.SmallTitle: | |
this.AppendClass("sc-text-smalltitle"); | |
break; | |
case TextType.Divider: | |
this.AppendClass("sc-text-divider"); | |
break; | |
} | |
if (string.IsNullOrEmpty(this.FieldName)) | |
{ | |
return; | |
} | |
var item = this.GetDataSource(); | |
if (item == null) | |
{ | |
return; | |
} | |
var text = item[this.FieldName]; | |
if (!string.IsNullOrEmpty(text)) | |
{ | |
this.Content = text; | |
} | |
} | |
/// <summary>Renders the specified output.</summary> | |
/// <param name="output">The output.</param> | |
protected override void Render(HtmlTextWriter output) | |
{ | |
Debug.ArgumentNotNull(output, "output"); | |
this.AddAttributes(output); | |
output.RenderBeginTag(HtmlTextWriterTag.Span); | |
if (!string.IsNullOrEmpty(this.Content)) | |
{ | |
output.WriteEncodedText(this.Content); | |
} | |
output.RenderEndTag(); | |
} | |
#endregion | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model Sitecore.BCL2.Client.Components.Common.Texts.TextRenderingModel | |
@{ | |
Model.Class.AppendIf(Model.TextType, string.Empty, ""). | |
AppendIf(Model.TextType, "Text", ""). | |
AppendIf(Model.TextType, "Label", "sc-text-label"). | |
AppendIf(Model.TextType, "HelpLabel", "sc-text-helplabel"). | |
AppendIf(Model.TextType, "LargeLabel", "sc-text-largelabel"). | |
AppendIf(Model.TextType, "Value", "sc-text-value"). | |
AppendIf(Model.TextType, "LargeValue", "sc-text-largevalue"). | |
AppendIf(Model.TextType, "Title", "sc-text-title"). | |
AppendIf(Model.TextType, "LargeTitle", "sc-text-largetitle"). | |
AppendIf(Model.TextType, "SmallTitle", "sc-text-smalltitle"). | |
AppendIf(Model.TextType, "Divider", "sc-text-divider"); | |
Model.NoScript(); | |
Model.DataBind.IsVisible().Text(); | |
} | |
<span @Model.HtmlAttributes>@Model.Text</span> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think Razor is the way to go for these sort of presentational concerns. Keep the C# code for business logic and the Razor view will be much cleaner.