Class Template
- Namespace
- Dynamicweb.Rendering
- Assembly
- Dynamicweb.dll
Loads HTML (XSLT) based templates and expose methods for setting dynamic values and loops. Returns a parsed version of the template replacing template tags with dynamic values.
public class Template : TemplateData
- Inheritance
-
Template
- Inherited Members
Examples
using System;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
class TemplateSample
{
public string RenderTemplate()
{
//Load template from /Files/Templates/MyModuleName/List.html
var t = new Template("MyModuleName/List.html");
//Render string template tag <!--@WelcomeMessage-->
t.SetTag("WelcomeMessage", "Hello world");
//Render boolean template tag <!--@IsTuesDay-->
t.SetTag("IsTuesday", DateTime.Now.DayOfWeek == System.DayOfWeek.Tuesday);
//Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
t.SetTag("TodaysDate", DateTime.Now);
//Test if the loaded template contains a specific tag - if not do not do expensive work.
if (t.TagExists("MyCalculation"))
{
t.SetTag("MyCalculation", "CallExpensiveMethod()");
}
//Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
if (t.LoopExists("MyItems"))
{
//Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
Template t2 = t.GetLoop("MyItems");
for (var i = 1; i <= 5; i++)
{
//Render a counter as a template tag inside the loop <!--@MyCounter-->
t2.SetTag("MyCounter", i);
//Commit the loop and make ready for the next iteration
t2.CommitLoop();
}
}
//Render the template html with the parsed template tags and loops.
return t.Output();
}
}
}
Remarks
Templates are HTML or XSLT files placed in the /Files/Templates folder of a Dynamicweb
installation. Templates are the rendering engine of Dynamicweb merging the markup (or XSLT) with the dynamicweb values delivered by the system.
Template language looks like normal markup with comment style variables: <h1><--@TagName--><h1>. Runtime the <--@TagName--> is replaced by a value set with SetTag(string, string, string, string, bool).
Template language looks like normal markup with comment style variables: <h1><--@TagName--><h1>. Runtime the <--@TagName--> is replaced by a value set with SetTag(string, string, string, string, bool).
Constructors
Template()
Initializes a new instance of the Template class without reading a template file.
public Template()
Examples
See usage example in LoadTemplate(string)Remarks
Use Html property or LoadTemplate(string) to load html to the instance.
Template(string)
Initializes a new instance of the Template class and loads a template into the object.
public Template(string path)
Parameters
pathstring- The path to the template relative to /Files/Templates directory, i.e. "MyModule/item.html" to load a template from /Files/Templates/MyModule/item.html.
Template(string, string)
Initializes a new instance of the Template class and loads a template into the object.
public Template(string path, string cultureName)
Parameters
pathstring- The path to the template relative to /Files/Templates directory, i.e. "MyModule/item.html" to load a template from /Files/Templates/MyModule/item.html.
cultureNamestring- Name of the culture used for translation of translate tags, ie. 'en-GB'
Properties
CurrentLoop
Gets or sets the loop instance if the current template object is a member of a parent template
objects loop collection. Otherwise null (Nothing in Visual Basic).
public Loop CurrentLoop { get; set; }
Property Value
- Loop
- The loop instance of the current template object
Remarks
This property is used by the template object in loop situations. Should not be
called.
Html
Gets or sets the HTML of the template. Usually loaded when instantiated
New Template("MyFolder/MyTemplate.html").public string Html { get; set; }
Property Value
- string
- The HTML of a template.
IsViewModelTemplate
public bool IsViewModelTemplate { get; }
Property Value
IsXsltTemplate
Gets a value indicating whether this instance is XSLT template.
public bool IsXsltTemplate { get; }
Property Value
- bool
trueif this instance is XSLT template; otherwise,false.
Loop
Gets or sets the loop instance if the current template object is a member of a parent template
objects loop collection. Otherwise null (Nothing in Visual Basic).
[Obsolete("Use CurrentLoop instead")]
public Loop Loop { get; set; }
Property Value
- Loop
- The loop instance of the current template object
Remarks
This property is used by the template object in loop situations. Should not be
called.
LoopName
Gets or sets the name of the loop.
public string LoopName { get; set; }
Property Value
- string
- The name of the loop.
Loops
Gets the loops.
public LoopCollection Loops { get; }
Property Value
- LoopCollection
- The loop collection.
Path
Gets or sets the full path of the template path.
public string Path { get; }
Property Value
- string
- The path.
Tags
Gets or sets the collection of template tags.
public TagCollection Tags { get; set; }
Property Value
- TagCollection
- The tag collection.
Remarks
Use SetTag(string, string, string, string, bool) to add tags or tag collections to the current instance.
Type
Gets a enum of the type of template.
public Template.TemplateType Type { get; set; }
Property Value
- Template.TemplateType
- The template type.
ViewModel
public ViewModelBase ViewModel { get; }
Property Value
ViewParameters
Gets the collection of custom view parameters.
public IDictionary<string, object> ViewParameters { get; }
Property Value
XmlDocument
Gets a XML document representing the template tags as xml - tags are not present until Template.Output has been called.
public XmlDocument XmlDocument { get; }
Property Value
- XmlDocument
- The XML document.
Methods
CombineTagName(string, params string[])
Combines an array of strings into a template tag name.
public static string CombineTagName(string tagNamespace, params string[] components)
Parameters
Returns
CommitLoop()
Commits a loop.
public void CommitLoop()
Examples
See usage example in GetLoop(string)Remarks
Call this every time an iteration of a template loop is done. See GetLoop for example.
GetLoop(string)
Gets a loop.
public Template GetLoop(string name)
Parameters
namestring- The name.
Returns
GetRawTags()
public IDictionary<string, object> GetRawTags()
Returns
GetRawValue(string)
Gets the value of the passed variable name.
public object GetRawValue(string name)
Parameters
namestring- The name of the variable.
Returns
- object
- Returns
null(Nothing in VB) if the passed name is not found in the rawTags collection
GetVariable(string)
Gets xslt variable.
public string GetVariable(string name)
Parameters
namestring- The name.
Returns
- string
- Value of xslt variable.
HtmlHasPartialTag(string)
Determines whether there is a tag in the current HTML template that starts with the given prefix.
public bool HtmlHasPartialTag(string name)
Parameters
namestring- Tag name prefix.
Returns
- bool
- Value indicating whether there is a tag in the current HTML template that starts with the given prefix.
LoadTemplate(string)
Loads a Template file into the object from disk specified by
path parameter.public override void LoadTemplate(string path)
Parameters
pathstring- The path to the Template file relative to the /Files/Templates/ folder.
Remarks
Usually used with the constructor of the Template object. See the Template reference for a full example.
Use Html property to load markup from a string.
LoadTemplate(string, bool)
Loads a Template file into the object from disk specified by
path parameter.public override void LoadTemplate(string path, bool forceRecompile)
Parameters
pathstring- The path to the Template file relative to the /Files/Templates/ folder.
forceRecompilebool
Remarks
Usually used with the constructor of the Template object. See the Template reference for a full example.
Use Html property to load markup from a string.
LoopExists(string)
Checks if a loop exists in the loaded template.
public bool LoopExists(string name)
Parameters
namestring- The name of loop to check for.
Returns
- bool
trueif the loop is found in the template; otherwisefalse
Remarks
Used for optimizing performance. If the loop is expensive to render, there is no need to execute the code if the template is not going to use the data.
Output()
Parses the loaded template html with the data set on the object with SetTag(string, string, string, string, bool). If the loaded template is a XSLT file, the passed tags are converted into a XML-document which is applied with the loaded XSLT.
public string Output()
Returns
- string
- A merged string of the resulting html.
Examples
using System;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
class TemplateSample
{
public string RenderTemplate()
{
//Load template from /Files/Templates/MyModuleName/List.html
var t = new Template("MyModuleName/List.html");
//Render string template tag <!--@WelcomeMessage-->
t.SetTag("WelcomeMessage", "Hello world");
//Render boolean template tag <!--@IsTuesDay-->
t.SetTag("IsTuesday", DateTime.Now.DayOfWeek == System.DayOfWeek.Tuesday);
//Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
t.SetTag("TodaysDate", DateTime.Now);
//Test if the loaded template contains a specific tag - if not do not do expensive work.
if (t.TagExists("MyCalculation"))
{
t.SetTag("MyCalculation", "CallExpensiveMethod()");
}
//Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
if (t.LoopExists("MyItems"))
{
//Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
Template t2 = t.GetLoop("MyItems");
for (var i = 1; i <= 5; i++)
{
//Render a counter as a template tag inside the loop <!--@MyCounter-->
t2.SetTag("MyCounter", i);
//Commit the loop and make ready for the next iteration
t2.CommitLoop();
}
}
//Render the template html with the parsed template tags and loops.
return t.Output();
}
}
}
Remarks
Should only be called once for each instance of the template object. Use CommitLoop() inside a loop.
ParseXml()
public void ParseXml()
Reset()
Resets this instance. Clears the buffers, the tags and loops.
public void Reset()
SetTag(string, bool)
Specifies a tag value of type boolean.
public void SetTag(string name, bool value)
Parameters
namestring- The name of the template tag, <--@NameOfTag-->.
valuebool- if set to
truethe tag returns "True", otherwise Sting.empty.
Examples
using System;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
class TemplateSample
{
public string RenderTemplate()
{
//Load template from /Files/Templates/MyModuleName/List.html
var t = new Template("MyModuleName/List.html");
//Render string template tag <!--@WelcomeMessage-->
t.SetTag("WelcomeMessage", "Hello world");
//Render boolean template tag <!--@IsTuesDay-->
t.SetTag("IsTuesday", DateTime.Now.DayOfWeek == System.DayOfWeek.Tuesday);
//Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
t.SetTag("TodaysDate", DateTime.Now);
//Test if the loaded template contains a specific tag - if not do not do expensive work.
if (t.TagExists("MyCalculation"))
{
t.SetTag("MyCalculation", "CallExpensiveMethod()");
}
//Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
if (t.LoopExists("MyItems"))
{
//Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
Template t2 = t.GetLoop("MyItems");
for (var i = 1; i <= 5; i++)
{
//Render a counter as a template tag inside the loop <!--@MyCounter-->
t2.SetTag("MyCounter", i);
//Commit the loop and make ready for the next iteration
t2.CommitLoop();
}
}
//Render the template html with the parsed template tags and loops.
return t.Output();
}
}
}
Remarks
It renders an empty string if false so if defined can be used on the output.
SetTag(string, DateTime)
Specifies a tag value of type date with the specified culture. Adds a series of Date/Time extensions to the base template name.
public void SetTag(string name, DateTime value)
Parameters
namestring- The name of the template tag, <--@NameOfTag-->.
valueDateTime- The date value replacing the template tag specified in the name.
Examples
using System;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
class TemplateSample
{
public string RenderTemplate()
{
//Load template from /Files/Templates/MyModuleName/List.html
var t = new Template("MyModuleName/List.html");
//Render string template tag <!--@WelcomeMessage-->
t.SetTag("WelcomeMessage", "Hello world");
//Render boolean template tag <!--@IsTuesDay-->
t.SetTag("IsTuesday", DateTime.Now.DayOfWeek == System.DayOfWeek.Tuesday);
//Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
t.SetTag("TodaysDate", DateTime.Now);
//Test if the loaded template contains a specific tag - if not do not do expensive work.
if (t.TagExists("MyCalculation"))
{
t.SetTag("MyCalculation", "CallExpensiveMethod()");
}
//Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
if (t.LoopExists("MyItems"))
{
//Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
Template t2 = t.GetLoop("MyItems");
for (var i = 1; i <= 5; i++)
{
//Render a counter as a template tag inside the loop <!--@MyCounter-->
t2.SetTag("MyCounter", i);
//Commit the loop and make ready for the next iteration
t2.CommitLoop();
}
}
//Render the template html with the parsed template tags and loops.
return t.Output();
}
}
}
SetTag(string, DateTime, CultureInfo)
Specifies a tag value of type date with the specified culture. Adds a series of Date/Time extensions to the base template name.
public void SetTag(string name, DateTime value, CultureInfo culture)
Parameters
namestring- The name of the template tag, <--@NameOfTag-->.
valueDateTime- The date value replacing the template tag specified in the name.
cultureCultureInfo- The culture.
Examples
using System;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
class TemplateSample
{
public string RenderTemplate()
{
//Load template from /Files/Templates/MyModuleName/List.html
var t = new Template("MyModuleName/List.html");
//Render string template tag <!--@WelcomeMessage-->
t.SetTag("WelcomeMessage", "Hello world");
//Render boolean template tag <!--@IsTuesDay-->
t.SetTag("IsTuesday", DateTime.Now.DayOfWeek == System.DayOfWeek.Tuesday);
//Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
t.SetTag("TodaysDate", DateTime.Now);
//Test if the loaded template contains a specific tag - if not do not do expensive work.
if (t.TagExists("MyCalculation"))
{
t.SetTag("MyCalculation", "CallExpensiveMethod()");
}
//Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
if (t.LoopExists("MyItems"))
{
//Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
Template t2 = t.GetLoop("MyItems");
for (var i = 1; i <= 5; i++)
{
//Render a counter as a template tag inside the loop <!--@MyCounter-->
t2.SetTag("MyCounter", i);
//Commit the loop and make ready for the next iteration
t2.CommitLoop();
}
}
//Render the template html with the parsed template tags and loops.
return t.Output();
}
}
}
Remarks
Used internally. Use SetTag(String, Date) instead.
SetTag(string, DateTime, CultureInfo, string)
Specifies a tag value of type date with the specified culture. Adds a series of Date/Time extensions to the base template name.
public void SetTag(string name, DateTime value, CultureInfo culture, string dateFormat)
Parameters
namestring- The name of the template tag, <--@NameOfTag-->.
valueDateTime- The date value replacing the template tag specified in the name.
cultureCultureInfo- The culture.
dateFormatstring- The date format.
Examples
using System;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
class TemplateSample
{
public string RenderTemplate()
{
//Load template from /Files/Templates/MyModuleName/List.html
var t = new Template("MyModuleName/List.html");
//Render string template tag <!--@WelcomeMessage-->
t.SetTag("WelcomeMessage", "Hello world");
//Render boolean template tag <!--@IsTuesDay-->
t.SetTag("IsTuesday", DateTime.Now.DayOfWeek == System.DayOfWeek.Tuesday);
//Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
t.SetTag("TodaysDate", DateTime.Now);
//Test if the loaded template contains a specific tag - if not do not do expensive work.
if (t.TagExists("MyCalculation"))
{
t.SetTag("MyCalculation", "CallExpensiveMethod()");
}
//Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
if (t.LoopExists("MyItems"))
{
//Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
Template t2 = t.GetLoop("MyItems");
for (var i = 1; i <= 5; i++)
{
//Render a counter as a template tag inside the loop <!--@MyCounter-->
t2.SetTag("MyCounter", i);
//Commit the loop and make ready for the next iteration
t2.CommitLoop();
}
}
//Render the template html with the parsed template tags and loops.
return t.Output();
}
}
}
Remarks
Used internally. Use SetTag(String, Date) instead.
SetTag(string, DateTime, string)
Specifies a tag value of type date with the specified culture. Adds a series of Date/Time extensions to the base template name.
public void SetTag(string name, DateTime value, string dateFormat)
Parameters
namestring- The name of the template tag, <--@NameOfTag-->.
valueDateTime- The date value replacing the template tag specified in the name.
dateFormatstring- The date format.
Examples
using System;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
class TemplateSample
{
public string RenderTemplate()
{
//Load template from /Files/Templates/MyModuleName/List.html
var t = new Template("MyModuleName/List.html");
//Render string template tag <!--@WelcomeMessage-->
t.SetTag("WelcomeMessage", "Hello world");
//Render boolean template tag <!--@IsTuesDay-->
t.SetTag("IsTuesday", DateTime.Now.DayOfWeek == System.DayOfWeek.Tuesday);
//Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
t.SetTag("TodaysDate", DateTime.Now);
//Test if the loaded template contains a specific tag - if not do not do expensive work.
if (t.TagExists("MyCalculation"))
{
t.SetTag("MyCalculation", "CallExpensiveMethod()");
}
//Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
if (t.LoopExists("MyItems"))
{
//Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
Template t2 = t.GetLoop("MyItems");
for (var i = 1; i <= 5; i++)
{
//Render a counter as a template tag inside the loop <!--@MyCounter-->
t2.SetTag("MyCounter", i);
//Commit the loop and make ready for the next iteration
t2.CommitLoop();
}
}
//Render the template html with the parsed template tags and loops.
return t.Output();
}
}
}
Remarks
Used internally. Use SetTag(String, Date) instead.
SetTag(string, decimal)
Specifies a tag value of type decimal.
public void SetTag(string name, decimal value)
Parameters
namestring- The name of the template tag, <--@NameOfTag-->.
valuedecimal- The decimal value replacing the template tag specified in the name.
Examples
using System;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
class TemplateSample
{
public string RenderTemplate()
{
//Load template from /Files/Templates/MyModuleName/List.html
var t = new Template("MyModuleName/List.html");
//Render string template tag <!--@WelcomeMessage-->
t.SetTag("WelcomeMessage", "Hello world");
//Render boolean template tag <!--@IsTuesDay-->
t.SetTag("IsTuesday", DateTime.Now.DayOfWeek == System.DayOfWeek.Tuesday);
//Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
t.SetTag("TodaysDate", DateTime.Now);
//Test if the loaded template contains a specific tag - if not do not do expensive work.
if (t.TagExists("MyCalculation"))
{
t.SetTag("MyCalculation", "CallExpensiveMethod()");
}
//Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
if (t.LoopExists("MyItems"))
{
//Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
Template t2 = t.GetLoop("MyItems");
for (var i = 1; i <= 5; i++)
{
//Render a counter as a template tag inside the loop <!--@MyCounter-->
t2.SetTag("MyCounter", i);
//Commit the loop and make ready for the next iteration
t2.CommitLoop();
}
}
//Render the template html with the parsed template tags and loops.
return t.Output();
}
}
}
SetTag(string, double)
Specifies a tag value of type double.
public void SetTag(string name, double value)
Parameters
namestring- The name of the template tag, <--@NameOfTag-->.
valuedouble- The double value replacing the template tag specified in the name.
Examples
using System;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
class TemplateSample
{
public string RenderTemplate()
{
//Load template from /Files/Templates/MyModuleName/List.html
var t = new Template("MyModuleName/List.html");
//Render string template tag <!--@WelcomeMessage-->
t.SetTag("WelcomeMessage", "Hello world");
//Render boolean template tag <!--@IsTuesDay-->
t.SetTag("IsTuesday", DateTime.Now.DayOfWeek == System.DayOfWeek.Tuesday);
//Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
t.SetTag("TodaysDate", DateTime.Now);
//Test if the loaded template contains a specific tag - if not do not do expensive work.
if (t.TagExists("MyCalculation"))
{
t.SetTag("MyCalculation", "CallExpensiveMethod()");
}
//Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
if (t.LoopExists("MyItems"))
{
//Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
Template t2 = t.GetLoop("MyItems");
for (var i = 1; i <= 5; i++)
{
//Render a counter as a template tag inside the loop <!--@MyCounter-->
t2.SetTag("MyCounter", i);
//Commit the loop and make ready for the next iteration
t2.CommitLoop();
}
}
//Render the template html with the parsed template tags and loops.
return t.Output();
}
}
}
SetTag(string, int)
Specifies a tag value of type integer.
public void SetTag(string name, int value)
Parameters
namestring- The name of the template tag, <--@NameOfTag-->.
valueint- The integer value replacing the template tag specified in the name.
Examples
using System;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
class TemplateSample
{
public string RenderTemplate()
{
//Load template from /Files/Templates/MyModuleName/List.html
var t = new Template("MyModuleName/List.html");
//Render string template tag <!--@WelcomeMessage-->
t.SetTag("WelcomeMessage", "Hello world");
//Render boolean template tag <!--@IsTuesDay-->
t.SetTag("IsTuesday", DateTime.Now.DayOfWeek == System.DayOfWeek.Tuesday);
//Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
t.SetTag("TodaysDate", DateTime.Now);
//Test if the loaded template contains a specific tag - if not do not do expensive work.
if (t.TagExists("MyCalculation"))
{
t.SetTag("MyCalculation", "CallExpensiveMethod()");
}
//Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
if (t.LoopExists("MyItems"))
{
//Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
Template t2 = t.GetLoop("MyItems");
for (var i = 1; i <= 5; i++)
{
//Render a counter as a template tag inside the loop <!--@MyCounter-->
t2.SetTag("MyCounter", i);
//Commit the loop and make ready for the next iteration
t2.CommitLoop();
}
}
//Render the template html with the parsed template tags and loops.
return t.Output();
}
}
}
SetTag(string, long)
Specifies a tag value of type long (Int64).
public void SetTag(string name, long value)
Parameters
namestring- The name of the template tag, <--@NameOfTag-->.
valuelong- The long value replacing the template tag specified in the name.
Examples
using System;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
class TemplateSample
{
public string RenderTemplate()
{
//Load template from /Files/Templates/MyModuleName/List.html
var t = new Template("MyModuleName/List.html");
//Render string template tag <!--@WelcomeMessage-->
t.SetTag("WelcomeMessage", "Hello world");
//Render boolean template tag <!--@IsTuesDay-->
t.SetTag("IsTuesday", DateTime.Now.DayOfWeek == System.DayOfWeek.Tuesday);
//Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
t.SetTag("TodaysDate", DateTime.Now);
//Test if the loaded template contains a specific tag - if not do not do expensive work.
if (t.TagExists("MyCalculation"))
{
t.SetTag("MyCalculation", "CallExpensiveMethod()");
}
//Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
if (t.LoopExists("MyItems"))
{
//Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
Template t2 = t.GetLoop("MyItems");
for (var i = 1; i <= 5; i++)
{
//Render a counter as a template tag inside the loop <!--@MyCounter-->
t2.SetTag("MyCounter", i);
//Commit the loop and make ready for the next iteration
t2.CommitLoop();
}
}
//Render the template html with the parsed template tags and loops.
return t.Output();
}
}
}
SetTag(string, string)
Specifies a tag name and value for the instance of the template object.
public void SetTag(string name, string value)
Parameters
namestring- The name of the template tag, <--@NameOfTag-->.
valuestring- The value replacing the template tag specified in the name.
SetTag(string, string, bool)
Specifies a tag name and value for the instance of the template object.
public void SetTag(string name, string value, bool htmlEncode)
Parameters
namestring- The name of the template tag, <--@NameOfTag-->.
valuestring- The value replacing the template tag specified in the name.
htmlEncodebool- if set to
truethe value is HTML encoded.
SetTag(string, string, string, string)
Specifies a tag value for the instance of the template object and add an attribute with a value on the xml node when using xslt templates.
public void SetTag(string name, string value, string xmlAttributeName, string xmlAttributeValue)
Parameters
namestring- The name of the template tag, <--@NameOfTag-->.
valuestring- The value replacing the template tag specified in the name.
xmlAttributeNamestring- Name of an additional XML attribute that should be added to the tag node when using XSLT.
xmlAttributeValuestring- The XML attribute value of the
XmlAttributeNamewhen specified.
Examples
using System;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
class TemplateSample
{
public string RenderTemplate()
{
//Load template from /Files/Templates/MyModuleName/List.html
var t = new Template("MyModuleName/List.html");
//Render string template tag <!--@WelcomeMessage-->
t.SetTag("WelcomeMessage", "Hello world");
//Render boolean template tag <!--@IsTuesDay-->
t.SetTag("IsTuesday", DateTime.Now.DayOfWeek == System.DayOfWeek.Tuesday);
//Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
t.SetTag("TodaysDate", DateTime.Now);
//Test if the loaded template contains a specific tag - if not do not do expensive work.
if (t.TagExists("MyCalculation"))
{
t.SetTag("MyCalculation", "CallExpensiveMethod()");
}
//Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
if (t.LoopExists("MyItems"))
{
//Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
Template t2 = t.GetLoop("MyItems");
for (var i = 1; i <= 5; i++)
{
//Render a counter as a template tag inside the loop <!--@MyCounter-->
t2.SetTag("MyCounter", i);
//Commit the loop and make ready for the next iteration
t2.CommitLoop();
}
}
//Render the template html with the parsed template tags and loops.
return t.Output();
}
}
}
SetTagValue(string, object)
Sets a tag as its object value for use in Razor templates.
public void SetTagValue(string name, object value)
Parameters
Remarks
If the template is Razor the value will persist as an object of the passed type and can be used directly in the template. In this way it is possible to pass
a full object, i.e. a 'Person', 'Product' or similar to the template engine. If the template being used is HTML or XSLT the passed value is converted to
either
string, integer, boolean, date, double or decimal.SetViewModel(ViewModelBase)
Sets the view model.
public void SetViewModel(ViewModelBase viewModel)
Parameters
viewModelViewModelBase- The view model.
SetViewParameters(IDictionary<string, object>)
public void SetViewParameters(IDictionary<string, object> parameters)
Parameters
parametersIDictionary<string, object>
TagExists(string)
Checks if a tag exists in the loaded template.
public bool TagExists(string name)
Parameters
namestring- The name of tag to check for.
Returns
- bool
trueif the tag is found in the template; otherwisefalse
Examples
Dim t As New Template("MyFolder/MyTemplate.html")
t.SetTag("MyTag", "Hello world!")
If t.TagExists("MyOtherTag") Then
t.SetTag("MyOtherTag", RunExpensiveOperation())
End If
Remarks
Used for optimizing performance. If the value of a tag is expensive to render, there is no need to execute the code if the template is not going to use the data.
TranslateText(string, string)
Translates a text using the current language and design context using a specified default translation if no translation exists.
public string TranslateText(string text, string defaultValue)
Parameters
Returns
- string
- The translated text if a translation exists. Otherwise the default translation.
TranslateText(string, string, string)
Translates a text using the current language and design context using a specified default translation if no translation exists.
public string TranslateText(string text, string defaultValue, string cultureName)
Parameters
textstring- The text to translate
defaultValuestring- The default translation
cultureNamestring- Name of the culture, i.e. 'en-gb'.
Returns
- string
- The translated text if a translation exists. Otherwise the default translation.