As of MyGeneration 1.1.1 sub-template execution is extremely powerful. Executing a sub-template can be more powerful than using includes, and you can make cross language sub-template calls. SubTemplate execution can greatly help when writing large templates, or if you want to provide a suite of templates with lots of common code between them. It could even aid multi-developer teams working on templates. Justin has made all of this possible and it is truly remarkable.
In the following example this is the order of sub-template calls:
VBScript -> C# -> VB.NET -> JScript
Our VBScript is acting as the parent, which executes a C# tempate, which in turn executes a VB.NET tempate which finally executes a JScript tempate, each tempate writes to the output and each child pass a value back up to its parent (a JScript template passing a value to a C# tempate? yep).
This is the VBScript parent template, it calls \"C# Child.zeus\"
- Code: Select all
<%
output.writeln \"START - Parent\"
context.ExecuteTemplate(\"C# Child.zeus\")
output.writeln \"END - Parent\"
output.writeln \"VBScript:\" + input.item(\"Data\")
%>
This is the C# template, it calls \"VBNET Child.zeus\"
- Code: Select all
<%
public class GeneratedTemplate : DotNetScriptTemplate
{
public GeneratedTemplate(ZeusContext context) : base(context) {}
public override void Render()
{
output.writeln(\"The C# Child subtemplate wrote this.\");
context.ExecuteTemplate(\"VBNET Child.zeus\");
input[\"Data\"] = \"C#:\" + input[\"Data\"];
}
}
%>
This is the VB.NET template, it calls \"JScript Child.zeus\"
- Code: Select all
<%
Public Class GeneratedTemplate
Inherits DotNetScriptTemplate
Public Sub New(context As ZeusContext)
MyBase.New(context)
End Sub
Public Overrides Sub Render
output.writeLn(\"The VB.NET Child Subtemplate wrote this\")
context.ExecuteTemplate(\"JScript Child.zeus\")
input.item(\"Data\") = \"VB.NET:\" + input.item(\"Data\")
End Sub
End Class
%>
This is the JScript template.
- Code: Select all
<%
output.writeln(\"The JScript Child wrote this\");
input.Item(\"Data\") = \"JSCRIPT\"
%>
Basically, all you have to do is execute the top level VBScript template, here is the output.
START - Parent
The C# Child subtemplate wrote this.
The VB.NET Child Subtemplate wrote this
The JScript Child wrote this
END - Parent
VBScript:C#:VB.NET:JSCRIPT
