SubTemplate Execution - Technical Prowess

We will post tips and tricks not posted in our documentation to help fill the gap.

SubTemplate Execution - Technical Prowess

Postby mike.griffin on Mon Mar 07, 2005 3:34 am

SubTemplate Execution - A Sickening Display of Technical Prowess

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
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN

Postby mike.griffin on Thu Mar 10, 2005 4:04 pm

You can pass MyMeta objects around via the input hashtable, for example:

The Main Template passing a Sub-Template an ITable
Code: Select all
<%
   input(\"MyTable\") = MyMeta.Databases(\"Northwind\").Tables(\"Employees\")
   context.ExecuteTemplate(\"sub.zeus\")
%>

The sub-template using that table

Code: Select all
<%
    Dim objTable
    Set objTable = input(\"MyTable\")

    output.writeln objTable.Name
%>
User avatar
mike.griffin
Site Admin
 
Posts: 3290
Joined: Sat Apr 03, 2004 6:10 am
Location: Indianapolis, IN


Return to Tips and Tricks

Who is online

Users browsing this forum: No registered users and 1 guest

cron