charlieanstey
7/22/2014 - 9:15 AM

Tridion :: Razor helpers for Experience Manager in 2013 SP1

Tridion :: Razor helpers for Experience Manager in 2013 SP1

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