Template

HTML Template Toolbox for Matlab

HTML Template Toolbox for Matlab

Template is a Matlab class to handle HTML templates as in Perl or PHP.

It allows for a complete distinction between content and layout. That is, if you write down results or reports of your Matlab toolbox computations in an HTML file, the @template class allows you to separate the HTML code from the Matlab code. Thus it becomes much more easier to modify the layout of your HTML files, because you then just need to modify HTML template files and not string variables in your Matlab code line 1465 !

If you want to learn more on templates philosophy, have a look to the numerous tutorials available on the web for PHPLib Templates. This @template class is just a Matlab rewriting of this PHP package (same functions, same i/o, same behaviour). Here are some tutorials:

Download

The HTML template toolbox for Matlab is available by email request only and is distributed under a GPL license. Please note that it is also embedded in the M2HTML toolbox.

Example

This example is the same than the PHPLib one.

Consider these two HTML template files: page.tpl and box.tpl.

<html>

  <head><title>{PAGETITLE}</title></head>

  <body bgcolor="#ffffff">

    <table border="1" cellpadding="4" cellspacing="0" bgcolor="#eeeeee">

      <tr>

        <td colspan="2"><h1>{PAGETITLE}</h1></td>

      </tr>

      <tr>

        <td>{OUT}</td>

        <td>Content<br>{UNDEFINED_VARIABLE}</td>

      </tr>

    </table>

  </body>

</html>

<!-- start box.tpl -->

<table border="1" bgcolor="#cccccc" cellpadding="4" cellspacing="0">

  <tr>

    <td colspan="2"><b>{TITLE}</b></td>

  </tr>

  <!-- BEGIN row -->

  <tr>

    <td>{NUM}</td>

    <td>{BIGNUM}</td>

  </tr>

  <!-- END row -->

</table>

<!-- end box.tpl -->

The following Matlab code demonstrates how to use the @template class:

%- Create a template object and specify:

%    - template files root path: /var/www/template/

%    - undefined fields management: keep undefined in output

tpl = template('/var/www/template/'),'keep');

%- Set the two template files and assign then a keyword

tpl = set(tpl,'file',{'page' 'box'},{'page.tpl' 'box.tpl'});

%- Set the block 'row' in the template file 'box' and assign it a keyword

tpl = set(tpl,'block','box','row','rows');

%- Set fields 'TITLE' and 'PAGETITLE' to some values

tpl = set(tpl,'var',{'TITLE' 'PAGETITLE'},{'Test Template' 'Guillaume'});

for i=1:3

	n = i;

	nn = 10 * i;

	%- Set fields 'NUM' and 'BIGNUM' to specific values

	tpl = set(tpl,'var',{'NUM' 'BIGNUM'},{num2str(n) num2str(nn)});

	%- Parse the block 'row' into 'rows' using append mode

	tpl = parse(tpl,'rows','row',1);

end

%- Parse template 'box' and then template 'page' and return output in 'OUT'

tpl = parse(tpl,'OUT', {'box' 'page'});

%- Display the content of the output of the parsing

disp(get(tpl,'OUT'))
The content of the 'OUT' variable is:
<html>

  <head><title>Guillaume</title></head>

  <body bgcolor="#ffffff">

    <table border="1" cellpadding="4" cellspacing="0" bgcolor="#eeeeee">

      <tr>

        <td colspan="2"><h1>Guillaume</h1></td>

      </tr>

      <tr>

        <td>

<!-- start box.tpl -->

<table border="1" bgcolor="#cccccc" cellpadding="4" cellspacing="0">

  <tr>

    <td colspan="2"><b>Test Template</b></td>

  </tr>

  <tr>

    <td>1</td>

    <td>10</td>

  </tr>   <tr>

    <td>2</td>

    <td>20</td>

  </tr>   <tr>

    <td>3</td>

    <td>30</td>

  </tr></table>

<!-- end box.tpl --></td>

        <td>Content<br>{UNDEFINED_VARIABLE}</td>

      </tr>

    </table>

  </body>

</html>