TemplateTags

TemplateTags are tags which are used to retrieve data from a Dynamicweb solution and render it in a template, e.g. a product name, a paragraph text, or – well – everything else, really. Dynamicweb uses ASP.NET Razor to work with template tags, which means that you can use regular C# methods to work with e.g. string objects or datetime objects.

As the name implies, TemplateTags belong to a specific Template – you can read more about templates in Dynamicweb here – and for performance reasons not all tags are available in all template contexts. Alongside the tags you will find loops which enable iteration through collections of items like products, variants, or countries.

To know which tags and loops are available in a context you can:

  • Browse this area of the Dynamicweb documentation portal – e.g. Content, Ecommerce, etc. – for information about the tags and loops available in a given template context
  • Use the @TemplateTags() helper tag inside a template or loop context to generate a table with tags & loops available (Figure 1.1)
Figure 1.1 A generated table showing template tags, loops & values in a given context

To retrieved the value behind a particular TemplateTag you use a Get*-method with the name of the template tag as the parameter:

Method

Returns

Comments

@GetValue(‘TagName’)

An untyped object

 

@GetString(‘TagName’)

A string object

 

@GetInteger(‘TagName’)

An integer

 

@GetBoolean(‘TagName’)

A boolean

 

@GetDouble(‘TagName’)

A double

 

@GetLong(‘TagName’)

A long

 

@GetDate(‘TagName’)

A datetime object

 

For example, in a shopping cart template you may want to retrieve the the Order ID of the order being created:

RAZOR
<div> <h3>Cart Information</h3> <div>Order ID: @GetString("Ecom:Order.ID")</div> </div>

Please note that all Get-methods return safe values; GetInteger will always return a valid integer (0), even if the passed tag does not exist or does not contain an integer.

When you use Get*-methods to retrieve typed objects you have immediate access to all the lovely C#-methods for the type:

RAZOR
@GetDate("DwPageCreatedDate").ToShortDateString(); @GetString("DwPageName").ToLower(); @if (@GetInteger("Comments.Count") > 0) { //Do stuff }

When you want to access values inside a loop – for instance products in a product list template – you use a foreach statement:

RAZOR
@foreach (var product in @GetLoop("Products")) { <div>@product.GetString("Ecom:Product.Name")</div> }

If you want to know what’s available inside a given loop, you can use the @TemplateTag() helper tag there as well:

RAZOR
@foreach (var product in @GetLoop("Products")) { <div>@product.TemplateTags()</div> }