08/31/2015 10:34 PM Tags
Virto Commerce

Tags

Updated:

Liquid tags are the programming logic that tells templates what to do. Tags are wrapped in: {% %}

Certain tags, such as for and cycle can take on parameters. Details for each parameter can be found in their respective sections.

Tags can be broken down to four categories:

  • Control Flow Tags
  • Iteration Tags
  • Theme Tags
  • Variable Tags

Creating your own tags

To create a new tag, simply inherit from DotLiquid.Tag and register your tag with DotLiquid.Template inside EngineConfig.cs file.

public class Random : DotLiquid.Tag
{
	private int _max;
	public override void Initialize(string tagName, string markup, List<string> tokens)
	{
		base.Initialize(tagName, markup, tokens);
		_max = Convert.ToInt32(markup);
	}
	
	public override void Render(Context context, TextWriter result)
	{
		result.Write(new Random().Next(_max).ToString());
	}
}

Template.RegisterTag<Random>("random");
Template template = Template.Parse(" {% random 5 %}");
template.Render(); // => "3"