Class Template
- Namespace
- Dynamicweb.Rendering
- Assembly
- Dynamicweb.Core.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 System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TemplateSample
{
public static 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, CultureInfo.InvariantCulture);
//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?).
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
path
stringThe 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
path
stringThe path to the template relative to /Files/Templates directory, i.e. "MyModule/item.html" to load a template from /Files/Templates/MyModule/item.html.
cultureName
stringName 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.
Filename
Gets the filename of the template, i.e. /News/Template.html.
public string Filename { get; }
Property Value
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.
HtmlOrg
public string? HtmlOrg { get; }
Property Value
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
true
if this instance is XSLT template; otherwise,false
.
LoopCounter
public int LoopCounter { get; }
Property Value
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.
Parent
Gets or sets the parent template - if the template instance is a loop item.
public Template? Parent { get; }
Property Value
- Template
The parent template object instance.
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?) 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.
FindDesignsVersion(string, Layout?)
public static string FindDesignsVersion(string fileName, Layout? layout)
Parameters
Returns
GetLoop(string)
Gets a loop.
public Template GetLoop(string name)
Parameters
name
stringThe 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
name
stringThe 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
name
stringThe 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
name
stringTag name prefix.
Returns
- bool
Value indicating whether there is a tag in the current HTML template that starts with the given prefix.
HtmlHasPartialTag(string, bool)
Determines whether there is a tag in the current HTML template that starts with the given prefix.
public bool HtmlHasPartialTag(string name, bool checkIfStatements)
Parameters
name
stringTag name prefix.
checkIfStatements
boolValue indicating whether to check "If" statements for tag presence.
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
path
stringThe 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
path
stringThe path to the Template file relative to the /Files/Templates/ folder.
forceRecompile
bool
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
name
stringThe name of loop to check for.
Returns
- bool
true
if 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?). 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 System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TemplateSample
{
public static 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, CultureInfo.InvariantCulture);
//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()
RenderPartial(string, Template, IDictionary<string, object>?)
Renders a partial template.
public static string RenderPartial(string templatePath, Template template, IDictionary<string, object>? viewParameters = null)
Parameters
templatePath
stringThe path of the template to render.
template
TemplateThe template object.
viewParameters
IDictionary<string, object>The optional view parameters.
Returns
- string
The rendered partial template as a string.
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
name
stringThe name of the template tag, <--@NameOfTag-->.
value
boolif set to
true
the tag returns "True", otherwise Sting.empty.
Examples
using System;
using System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TemplateSample
{
public static 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, CultureInfo.InvariantCulture);
//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
name
stringThe name of the template tag, <--@NameOfTag-->.
value
DateTimeThe date value replacing the template tag specified in the name.
Examples
using System;
using System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TemplateSample
{
public static 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, CultureInfo.InvariantCulture);
//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
name
stringThe name of the template tag, <--@NameOfTag-->.
value
DateTimeThe date value replacing the template tag specified in the name.
culture
CultureInfoThe culture.
Examples
using System;
using System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TemplateSample
{
public static 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, CultureInfo.InvariantCulture);
//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, DateTime) 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
name
stringThe name of the template tag, <--@NameOfTag-->.
value
DateTimeThe date value replacing the template tag specified in the name.
culture
CultureInfoThe culture.
dateFormat
stringThe date format.
Examples
using System;
using System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TemplateSample
{
public static 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, CultureInfo.InvariantCulture);
//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, DateTime) 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
name
stringThe name of the template tag, <--@NameOfTag-->.
value
DateTimeThe date value replacing the template tag specified in the name.
dateFormat
stringThe date format.
Examples
using System;
using System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TemplateSample
{
public static 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, CultureInfo.InvariantCulture);
//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, DateTime) instead.
SetTag(string, decimal)
Specifies a tag value of type decimal.
public void SetTag(string name, decimal value)
Parameters
name
stringThe name of the template tag, <--@NameOfTag-->.
value
decimalThe decimal value replacing the template tag specified in the name.
Examples
using System;
using System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TemplateSample
{
public static 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, CultureInfo.InvariantCulture);
//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
name
stringThe name of the template tag, <--@NameOfTag-->.
value
doubleThe double value replacing the template tag specified in the name.
Examples
using System;
using System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TemplateSample
{
public static 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, CultureInfo.InvariantCulture);
//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
name
stringThe name of the template tag, <--@NameOfTag-->.
value
intThe integer value replacing the template tag specified in the name.
Examples
using System;
using System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TemplateSample
{
public static 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, CultureInfo.InvariantCulture);
//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
name
stringThe name of the template tag, <--@NameOfTag-->.
value
longThe long value replacing the template tag specified in the name.
Examples
using System;
using System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TemplateSample
{
public static 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, CultureInfo.InvariantCulture);
//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
name
stringThe name of the template tag, <--@NameOfTag-->.
value
stringThe 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
name
stringThe name of the template tag, <--@NameOfTag-->.
value
stringThe value replacing the template tag specified in the name.
htmlEncode
boolif set to
true
the 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
name
stringThe name of the template tag, <--@NameOfTag-->.
value
stringThe value replacing the template tag specified in the name.
xmlAttributeName
stringName of an additional XML attribute that should be added to the tag node when using XSLT.
xmlAttributeValue
stringThe XML attribute value of the
XmlAttributeName
when specified.
Examples
using System;
using System.Globalization;
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering;
public static class TemplateSample
{
public static 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, CultureInfo.InvariantCulture);
//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
viewModel
ViewModelBaseThe view model.
SetViewParameters(IDictionary<string, object>)
public void SetViewParameters(IDictionary<string, object> parameters)
Parameters
parameters
IDictionary<string, object>
TagExists(string)
Checks if a tag exists in the loaded template.
public bool TagExists(string name)
Parameters
name
stringThe name of tag to check for.
Returns
- bool
true
if the tag is found in the template; otherwisefalse
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
text
stringThe text to translate
defaultValue
stringThe default translation
cultureName
stringName of the culture, i.e. 'en-gb'.
Returns
- string
The translated text if a translation exists. Otherwise the default translation.