Dynamicweb 8 Documentation
Template Class
Members  Example 

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.
Object Model
Template ClassTagCollection ClassTag Class
Syntax
'Declaration
 
<DefaultMemberAttribute("Item")> 
Public Class Template 
[DefaultMemberAttribute("Item")] 
public class Template 
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).

Template tag reference: http://templates.dynamicweb-cms.com/
Implementing designs with Dynamicweb templates: http://developer.dynamicweb-cms.com/documentation/for-designers.aspx
Example
Full template exampleFull template exampleSimple template usage exampleTemplate usage example with loop
using System;
using Dynamicweb.Rendering;

namespace Dynamicweb.Examples.CSharp.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();
        }

    }
}
Imports Dynamicweb.Rendering

Public Class TemplateSample
    Public Function RenderTemplate() As String
        'Load template from /Files/Templates/MyModuleName/List.html
        Dim t As New Template("MyModuleName/List.html")

        'Render string template tag <!--@WelcomeMessage-->
        t.SetTag("WelcomeMessage", "Hello world")

        'Render boolean template tag <!--@IsTuesDay-->
        t.SetTag("IsTuesday", Now.DayOfWeek = System.DayOfWeek.Tuesday)

        'Render a datetime template tag <!--@TodaysDate--> - and also the <!--@TodaysDate.*--> date time extensions
        t.SetTag("TodaysDate", Now)

        'Test if the loaded template contains a specific tag - if not do not do expensive work.
        If t.TagExists("MyCalculation") Then
            t.SetTag("MyCalculation", "CallExpensiveMethod()")
        End If

        'Test if the loop MyItems is present in the loaded template, and if not the code will not be executed.
        If t.LoopExists("MyItems") Then
            'Create a loop in the template <!--@LoopStart(MyItems)--><!--@LoopEnd(MyItems)-->
            Dim t2 As Template = t.GetLoop("MyItems")
            For i As Integer = 1 To 5
                '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()
            Next
        End If

        'Render the template html with the parsed template tags and loops.
        Return t.Output()
    End Function
End Class
//Load a template from /Files/Templates/MyFolder/MyTemplate.html
Rendering.Template t = new Template("MyFolder/MyTemplate.html");
//Set a tag value 
t.SetTag("MyTag", "Hello world!");
//Return the parsed template
return t.Output();
Template t = new Template("MyFolder/MyTemplate.html");
//Set a tag value 
t.SetTag("MyTag", "Hello world!");
 
//Create a loop in the template
Template loopTemplate = t.GetLoop("MyLoop");
foreach (Content.Page p in Content.Page.GetPagesByAreaID(1)) {
	loopTemplate.SetTag("PageName", p.MenuText);
	loopTemplate.CommitLoop();
}
 
//Return the parsed template
return t.Output();
Inheritance Hierarchy

System.Object
   Dynamicweb.Rendering.Template

Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

Template Members
Dynamicweb.Rendering Namespace

Send Feedback