CodeCharge Studio All ASP PHP Perl ColdFusion Java C#.NET VB.NET

Working with Custom Template Blocks

This example shows how to work with a custom template block and custom template variables.

  1. Add a custom block in the HTML mode (shown in blue)
  2. Add custom template variables. (shown in red)
    <table>
      <tr>
       <td>Server Variable</td>
       <td>Value</td>
      </tr>
    <!-- BEGIN MyTable -->
      <tr>
       <td>{VariableName}</td>
       <td>{VariableValue}</td>
      </tr>
    <!-- END MyTable -->
    </table>
  3. To set values for the template variables, enter the code below:

ASP

Version 1

For a top level block which is not nested within another block:

Function Page_BeforeShow(Sender)
Dim CustomBlock
Dim strKey

  Set CustomBlock = Tpl.Block("MyTable")
  For Each strKey In Request.ServerVariables
    CustomBlock.Variable("VariableName") =  strKey 
    CustomBlock.Variable("VariableValue") =  Request.ServerVariables(strKey)
    CustomBlock.Parse ccsParseAccumulate
  Next 

End Function

Version 2

For a custom block located inside another block (e.g. Mytable block inside Row block inside Tasks block):

Function Tasks_BeforeShowRow(Sender)
Dim CustomBlock
Dim strKey

  Set CustomBlock = Tasks.TemplateBlock.Block("Row").Block("MyTable")
  CustomBlock.Clear

  For Each strKey In Request.ServerVariables
    CustomBlock.Variable("VariableName") =  strKey 
    CustomBlock.Variable("VariableValue") =  Request.ServerVariables(strKey)
    CustomBlock.Parse ccsParseAccumulate
  Next 

End Function

Note: If you need to parse the block only once you can use 'ccsParseOverwrite' instead of 'ccsParseAccumulate'.

PHP

Version 1

For a top level block which is not nested within another block:

function Page_BeforeShow(& $sender) {
global $HTTP_SERVER_VARS;
global $Tpl;

  foreach($HTTP_SERVER_VARS as $strKey => $value) {
    $Tpl->SetVar("VariableName",$strKey);
    $Tpl->SetVar("VariableValue",$value);
    $Tpl->Parse("MyTable",True);
  }

}

Version 2

For a custom block located inside another block (e.g. Mytable block inside Row block inside Tasks block):

function Tasks_BeforeShowRow(& $sender) {
global $HTTP_SERVER_VARS;
global $Tpl;
  
  $Tpl->SetBlockVar("MyTable","");
  foreach($HTTP_SERVER_VARS as $strKey => $value) {
    $Tpl->SetVar("VariableName",$strKey);
    $Tpl->SetVar("VariableValue",$value);
    $Tpl->Parse("MyTable",True);
  }
}

Perl

Version 1

For a top level block which is not nested within another block:

sub Page_BeforeShow() {

  while (($key,$value) = each %ENV) {
    $Tpl->setvar("VariableName",$key);
    $Tpl->setvar("VariableValue",$value);
    $Tpl->parse("MyTable","true");
  }

}

Version 2

For a custom block located inside another block (e.g. Mytable block inside Row block inside Tasks block):

sub Tasks_BeforeShowRow() {

  $Tpl->setblockvar("MyTable","");
  while (($key,$value) = each %ENV) {
    $Tpl->setvar("VariableName",$key);
    $Tpl->setvar("VariableValue",$value);
    $Tpl->parse("MyTable","true");
  }

}

ColdFusion with Templates

Version 1

For a top level block which is not nested within another block:

<!--Page_BeforeShow --->

  <CFLOOP LIST="#StructKeyList(CGI)#" INDEX="item">   
    <CFMODULE Template="CCSetVar.cfm" Var="@VariableName" Value="#item#">
    <CFMODULE Template="CCSetVar.cfm" Var="@VariableValue" Value="#StructFind(CGI, item)#">
    <CFMODULE Template="CCParse.cfm" mode="Parse" blockName="MyTable" bAccumulate="True">
  </CFLOOP>

Version 2

For a custom block located inside another block (e.g. Mytable block inside Row block inside Tasks block):

<!--Tasks_BeforeShowRow --->

  <CFMODULE Template="CCSetVar.cfm" Var="#strBlockToParse#/Row/MyRow" Value="">
  <CFLOOP LIST="#StructKeyList(CGI)#" INDEX="item">    
    <CFMODULE Template="CCSetVar.cfm" Var="#strBlockToParse#/Row/MyRow/@VariableName1" Value="#item#">
    <CFMODULE Template="CCSetVar.cfm" Var="#strBlockToParse#/Row/MyRow/@VariableValue1" Value="#StructFind(CGI, item)#">
    <CFMODULE Template="CCParse.cfm" mode="Parse" blockName="#strBlockToParse#/Row/MyRow" bAccumulate="True">
  </CFLOOP>

Servlet with Templates

Version 1

For a top level block which is not nested within another block:

//Page_BeforeShow

  //The data used for output into the template variables.
  Enumeration enum = StringUtils.split("1;111;2;222;3;333;4;444");

  Template tmpl = e.getPage().getTemplate();
  String currentPath = e.getPage().getCurrentPath();
  while (enum.hasMoreElements()) {
    tmpl.setVar(currentPath+ "/MyTable/@VariableName", (String) enum.nextElement());
    if (enum.hasMoreElements()) {
      tmpl.setVar(currentPath+ "/MyTable/@VariableValue", (String) enum.nextElement());
    }
    tmpl.parse(currentPath+ "/MyTable");
  }

Version 2

For a custom block located inside another block (e.g. Mytable block inside Row block inside Tasks block):

// Tasks_BeforeShowRow 

  //The data used for output into the template variables.
  Enumeration enum = StringUtils.split("5;555;6;666;7;777;8;888");
  
  Template tmpl = e.getPage().getTemplate();
  String currentPath = e.getPage().getCurrentPath();
  tmpl.setVar(currentPath+ "/Row/MyTable");
  while (enum.hasMoreElements()) {
    tmpl.setVar(currentPath+ "/Row/MyTable/@VariableName", (String) enum.nextElement());
    if (enum.hasMoreElements()) {
      tmpl.setVar(currentPath+ "/Row/MyTable/@VariableValue", (String) enum.nextElement());
    }
    tmpl.parse(currentPath+ "/Row/MyTable");
  }

See Also:

Before Show event, Before Show Row event


On-line, printable versions and updates