Contents

 
Home
PHPRunner 7.1 manual
Prev Page Next Page
 
 

PHPRunner templates

 

PHPRunner uses built-in template language. PHPRunner templates cleanly separates your presentation layer (HTML, CSS, etc.) from your application code. The main idea is to simply visual templates moving all logic and javascript to PHP files. This makes Visual Editor stable and eliminates the need to reset pages.

Template language reference

- all templates tags are enclosed within delimiters { and }. All content outside delimiters is displayed as static HTML.

- template variables start with dollar ($) sign. They may contain numbers, letters and underscores.

Example: {$variable_name}

{BEGIN block_name} {END block_name} is used to define block section (loops, condition statements).

Functions

· assign($name, $val) is used to assign value $val to the variable $name.
· assignbyref($name, &$val) is similar to assign() except for value $val is passed by reference. Use assignbyref() when $val variable is array. See {block body} example below.
· assign_section($name, $begin, $end) is used to define block section.
· assign_loopsection($name, &$data) is used to define loop section.
· display($template) renders and displays the template file.

How templates work

Template files (*.htm) can be found in templates directory under output directory.

Here is the basic example of how templates work:

list.php

include('libs/xtempl.php');

// create object

$xt = new Xtempl();

// assign some content. This would typically come from

// a database or other source, but we'll use static

// values for the purpose of this example.

$xt->assign('name''george smith');

$xt->assign('address''45th & Harris');

// display it

$xt->display('list.htm');

The template file then contains the output interspersed with tags that PHPRunner replaces with assigned content.

list.htm


output

<html>

<head>

<title>User Info</title>

</head>

<body>

User Information:<p>

Name: {$name}<br>

Address: {$address}<br>

</body>

</html>

 

<html>

<head>

<title>User Info</title>

</head>

<body>

User Information:<p>

Name: george smith<br>

Address: 45th & Harris<br>

</body>

</html>

{BEGIN} ... {END} blocks

Template file contains a set of code sections wrapped by {BEGIN ...} and {END ...}.

view.htm

{BEGIN Model_fieldblock}

  <tr><td class=shade width=150>Model</td><td width=250>

  {$Model_value}&nbsp;

  </td></tr>

{END Model_fieldblock}

In view.php file use the following:

$xt->assign("Model_fieldblock",true); - code snippet between {BEGIN ...} and {END ...} appears in the output.

$xt->assign("Model_fieldblock",true); - code snippet goes away.

How to deal with Javascript

Earlier we mentioned that javascript and some HTML tags like <form> and <input type=hidden ...> need to be moved to PHP code in order to keep templates clean.

On the server side javascript code needs to be assigned to body or end variables in block array.

list.php

// create an array

$body = array();

// code assigned to "begin" variable replaces {BEGIN body} tag

$body["begin"]='<form name="frmSearch" method="GET" 

  action="carsadmin_cars_list.php">

<input type="Hidden" name="a" value="search">

<input type="Hidden" name="value" value="1">

</form>';

// code assigned to "end" variable replaces {END body} tag

$body["end"]="<script>if(document.getElementById('SearchFor'))

document.getElementById('ctlSearchFor').focus();</script>";

// using assignbyref() function as body is an array

$xt->assignbyref("body",$body);

Template file and generated output

list.htm


output

{BEGIN body}

...

{END body}

 

<form name="frmSearch" method="GET" 

action="carsadmin_cars_list.php">

<input type="Hidden" name="a" value="search">

<input type="Hidden" name="value" value="1">

</form>

...

<script>

if(document.getElementById('SearchFor'))

document.getElementById('ctlSearchFor').focus();

</script>

Converted from CHM to HTML with chm2web Standard 2.85 (unicode)