Skip to content

Instantly share code, notes, and snippets.

@dervalp
Last active August 29, 2015 14:07
Show Gist options
  • Save dervalp/9f4b3eb636accf4f30d2 to your computer and use it in GitHub Desktop.
Save dervalp/9f4b3eb636accf4f30d2 to your computer and use it in GitHub Desktop.
Razor vs C#
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
}
}
@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>
@KayeeNL
Copy link

KayeeNL commented Oct 1, 2014

In case of simplicity I would go for Razor! Definately!

@kevinobee
Copy link

For me markup should be in the Razor file. If you put HTML / CSS in the C# code you are only one step away from storing it in the DB.

@brooksyd2
Copy link

Definitely Razor for both simplicity, and maintaining separation of HTML/CSS and code. It also aligns more with standard ASP.NET MVC, and so would be more familiar to people just picking it up.

@TimHacker
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment