The rparse() Method
In the previous pages we have become familiar with the parse()
method. Now we'll take a look at a powerful alternative,
rparse().
For this example we'll use the same template we used to build our
table in the last example.
- <?php
- /*include the class*/
- include_once('../class.XiTemplate.php');
-
- /*instantiate the class and pass the path to your HTML template file*/
- $xitpl = new XiTemplate('templates/parse.tpl');
-
- /*assign a value to a template variable.*\
- /*In this example {NAME} will be replaced with "XiTemplate"*/
- $xitpl->assign('NAME', 'XiTemplate');
-
- /*let's make dynamically make a table*/
- $rows = 1;
- $cols = 1;
-
- $xitpl->assign('VARIABLE', 'row: 1, column: 1');
-
- /* ---- recursively ---- */
- /* parse the "main" block of the template (the whole page)*/
- $xitpl->rparse('main');
-
- /*send the final page out to the browser*/
- $xitpl->out('main');
- ?>
So what the heck happened? Well, let's say you had some pretty
complicated code and at some point in your code you realized that there
may be a time when only one record is returned from a query or only one
value may exist in code that is built for many. This is just an example.
There may be a hundred other uses for rparse().
In the preceding example we determined that there was only going to
be one column and one row. Rather than go through all the complication of
running two "for" loops and parsing multiple blocks we just assigned our
one template variable and used rparse() to parse the
whole darn thing one time.
Recursive parsing will parse all the blocks within the parent block
called by rparse().
Next up... file includes!
To be continued soon.