In a typical CodeCharge
Studio page, the HTML content is placed in a HTML template file which contains
specially designated template variables
and blocks. A template variable
within the HTML content is specified by placing the variable name within curly
braces e.g.
{myVariable}
On the other hand, a template
block usually consists of one or more HTML lines within HTML comments of the
nature:
<!--
BEGIN Record Name -->
<p> some html
<!-- END Record Name -->
Optionally, a template
block such as that above can contain a template variable.
When a page is requested,
the script files substitutes the template variables with values that are determined
at run time and the template blocks are conditionally parsed as determined by
the script code. For instance, in the block below, the template block called
Record Name is parsed and the template variables Variable1 and
Variable2 are replaced with script values if any are set.
<!-- BEGIN Record Name -->
<tr>
<td>{Variable1}</td>
<td>{Variable2}</td>
</tr>
<!-- END Record Name -->
In order for the script
file to properly parse the HTML template, the template blocks and variables
must appear in a particular manner. As such, caution should be observed when
manually editing the HTM templates so as not to render them unusable. In CCS
2.0, a warning message is displayed if the user makes what is determined to
be a harmful change to the HTML code.
Below are some examples
of how you can manipulate template variables and blocks programmatically. The
examples shown here are implemented in a sample
project file located here:
1. Custom template variables.
Custom template variables can be used to place dynamic content at specific locations
of a page. First, a template variable is placed in the HTML template at the
location where the dynamic content should appear. Then, you use event code to
assign a value to the template variable so that when the HTML template is parsed,
the template variable is replaced by the assigned value. If no value is assigned,
the template variable does not appear in the final page.
ASP
In ASP, template variables are referenced based on their location relative to
the blocks in which they are contained. For instance, a template variable called
Variable_Name located in a template block called Block_Name
would be referenced using the code:
EventCaller.TemplateBlock.Block(“Block_Name”).Variable(“Variable_Name”)
= “value”
Note that the blocks could be nested in which case the reference would have
the same level of nesting e.g.
EventCaller.TemplateBlock.Block(“Block_Name1”).
... .Block(“Block_NameX”).Variable(“variable_name”)
= “value”
In the above code, EventCaller
is a variable that stores the reference to the object which calls the event
used to set the value.
In the example project,
the following code is used to set the value of a template variable called inside_row
that is located within a block called Row. The code is located in the
Before Show event of the grid form.
EventCaller.TemplateBlock.Block("Row").Variable("inside_row")
= "<tr><td class=”"MultipadsDataTD”" colspan=\"3\"><b>additional
Grid row</b></td></tr>"
In the next example, the
following code is used to set the value of a template variable called inside_navigator
that is located within a block called Navigator Navigator.
EventCaller.TemplateBlock.Block("Navigator Navigator").Variable("inside_navigator")
= " Records found:" & rec_number
You can find the same code in the Before Show event of “employees_depar”
grid form (Template Example page).
The last example below
shows how the template variable is set when it is not located within any template
block. In this case, the Block(“name”) construct is omitted. In
this case, the variable table_title is not located within any block:
EventCaller.TemplateBlock.Variable("table_title")
= "<font class=""MultipadsFormHeaderFont"">List
of Employees</font>"
PHP
The handling of template variables in PHP is much simpler since you don't need
to reference the blocks. The procedure simply involves creating a global reference
to the $Tpl object then using the SetVar method to set the template variable
value. e.g.
global $Tpl;
$Tpl->SetVar(“template_variable_name”, “value”);
In the PHP version of the
sample project, similar code is used to assign values to the same template variables
listed in the ASP section above. The code is also located in the Before Show
event of the employees_depar grid form (TemplateExample page).
2. Custom template blocks.
In addition to the template blocks that are automatically generated for a page,
you can add you own custom template blocks. In the script file, the Parse function
is used to process the template blocks by substituting the values of any template
variables. The Parse function works in such a manner that it sets the variables
that occur within the specified block only. Even if similar variables are present
in other blocks, they are not set unless the Parse function is called on those
other blocks.
The Parse function has
two parameters. The first one is the name of template block while the second
indicates if the block is to be repeated several time or not. When the block
should be displayed once, the second parameter (accumulate) should be set to
false. For instance, for the block below:
<!-- BEGIN LoggedInUser -->
Logged in user name is: {user_name}
<!-- END LoggedInUser -->
The following code is used
to parse it after the variable has been set:
'ASP
EventCaller.TemplateBlock.Block("LoggedInUser").Variable("user_name")
= name
HTMLTemplate.Parse "LoggedInUser",
false
//PHP
global $Tpl;
$Tpl->SetVar("user_name", $name);
$Tpl->Parse("LoggedInUser", false);
In the example project,
the code above is located in the Before Show event of the employees_depar grid
form (TemplateExample page).
In the Parse function,
the accumulate parameter is set to true when the block is intended to be displayed
multiple times. In this case, the code would be placed within a loop so that
on each iteration of the loop, new variables are substituted into the block
then the new block is added to the existing blocks. This is the method that
is used to display multiple rows in a grid form whereby all the rows are based
on one template block. |