Skip to content

Instantly share code, notes, and snippets.

@willprice76
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save willprice76/1efa2855c04a58ae0e93 to your computer and use it in GitHub Desktop.
Save willprice76/1efa2855c04a58ae0e93 to your computer and use it in GitHub Desktop.
Field Helper functions for the Razor Mediator for Tridion
@using Tridion.ContentManager.Templating.Expression
@using Tridion.ContentManager.ContentManagement
@using Tridion.Extensions.Mediators.Razor.Models
@* --------------- IDENTIFYING FIELDS ---------------------- *@
@helper FieldStartMarker(string fieldExpression)
{
var fn = new BuiltInFunctions(TridionHelper.Engine, TridionHelper.Package);
@fn.FieldStartMarker(fieldExpression)
}
@helper FieldEndMarker()
{
@:</tcdl:Field>
}
@helper FieldValueStartMarker(int index=0)
{
@:<tcdl:FieldValue index="@index">
}
@helper FieldValueEndMarker()
{
@:</tcdl:FieldValue>
}
@helper StartField(string fieldExpression = null, int index = 0, bool singleValue = true, bool useTcdl = false)
{
if (fieldExpression!=null && singleValue)
{
@FieldStartMarker(fieldExpression)
}
if (fieldExpression!=null||useTcdl)
{
@FieldValueStartMarker(index)
}
}
@helper EndField(bool useTcdl = false, bool singleValue = true)
{
if (useTcdl)
{
@FieldValueEndMarker()
}
if (useTcdl && singleValue)
{
@FieldEndMarker()
}
}
@* --------------- COMMON FIELD TYPES ---------------------- *@
@helper Text(string text, int index, bool useTcdl=true)
{
@Text(text, null, index, false, useTcdl)
}
@helper Text(string text, string fieldExpression = null, int index = 0, bool singleValue = true, bool useTcdl = false)
{
@StartField(fieldExpression,index,singleValue,useTcdl)
@HtmlEncode(text)
@EndField(fieldExpression!=null||useTcdl,singleValue)
}
@helper RichText(string text, int index, bool addParagraph = true, bool useTcdl=true)
{
@RichText(text, null, addParagraph, index, false, useTcdl)
}
@helper RichText(string text, string fieldExpression = null, bool addParagraph = true, int index = 0, bool singleValue = true, bool useTcdl = false)
{
@StartField(fieldExpression,index,singleValue,useTcdl)
@((!addParagraph || text==null || text.Contains("</p>")) ? text : "<p>" + text + "</p>");
@EndField(fieldExpression!=null||useTcdl,singleValue)
}
@helper Img(ComponentModel imgComp, int index, string attributes="", bool useTcdl=true)
{
@Img(imgComp, null, attributes, index, false, useTcdl)
}
@helper Img(ComponentModel imgComp, string fieldExpression = null, string attributes = "", int index = 0, bool singleValue = true, bool useTcdl = false)
{
@StartField(fieldExpression,index,singleValue,useTcdl)
if (imgComp!=null) {
var alt = imgComp.Title;
if(imgComp.MetaData != null && imgComp.MetaData.HasValue("AltText"))
{
alt = imgComp.MetaData.AltText;
}
@:<img src="@imgComp.ID" alt="@alt" @attributes />
}
@EndField(fieldExpression!=null||useTcdl,singleValue)
}
@helper Date(DateTime date, int index, string attributes="", string format = null, bool useTcdl=true)
{
@Date(date, null, attributes, format, index, false, useTcdl)
}
@helper Date(DateTime date, string fieldExpression = null, string attributes = "", string format = "dd MMM yyyy", int index = 0, bool singleValue = true, bool useTcdl = false)
{
@StartField(fieldExpression,index,singleValue,useTcdl)
if (date!=null && date > System.DateTime.MinValue)
{(
@:<time datetime="@date.ToString("s")" @attributes>@date.ToString(format))</time>
}
@EndField(fieldExpression!=null||useTcdl,singleValue)
}
@helper Link(dynamic linkComp, string linkText, int index, string attributes="", bool useTcdl=true)
{
@Link(linkComp, linkText, null, attributes, index, false, useTcdl)
}
@helper Link(dynamic linkComp, string linkText = "", string fieldExpression = null, string attributes = "", int index = 0, bool singleValue = true, bool useTcdl = false)
{
@StartLink(linkComp, fieldExpression, attributes, index, singleValue, useTcdl)
@linkText
@EndLink(fieldExpression!=null||useTcdl,singleValue)
}
@helper ExternalLink(string href, string linkText, int index, string attributes="", bool useTcdl=true)
{
@ExternalLink(href, linkText, null, attributes, index, false, useTcdl)
}
@helper ExternalLink(string href, string linkText, string fieldExpression = null, string attributes = "", int index = 0, bool singleValue = true, bool useTcdl = false)
{
@StartExternalLink(href, fieldExpression, attributes, index, singleValue, useTcdl)
@linkText@
@EndLink(fieldExpression!=null||useTcdl,singleValue)
}
@helper StartLink(dynamic linkComp, string fieldExpression = null, string attributes = "", int index = 0, bool singleValue = true, bool useTcdl = false)
{
@StartField(fieldExpression,index,singleValue,useTcdl)
if (linkComp != null) {
@:<a tridion:href="@linkComp.ID" @attributes>
}
}
@helper StartExternalLink(string href, string fieldExpression = null, string attributes = "", int index = 0, bool singleValue = true, bool useTcdl = false)
{
@StartField(fieldExpression,index,singleValue,useTcdl)
if (!String.IsNullOrEmpty(href)) {
@:<a href="@href" @attributes>
}
}
@helper EndLink(dynamic value = null, bool useTcdl = false, bool singleValue = true)
{
if (value!=null)
{
@:</a>
}
@EndField(useTcdl,singleValue)
}
@charlieanstey
Copy link

Hi Will,

I forked this gist and made a few fixes, check out the updates if you get a chance. Would have pulled, if only you could on gists!

Cheers,
Charlie

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