Template

HTML Template Toolbox for MATLAB

HTML Template Toolbox for MATLAB

Template is a MATLAB class to handle HTML templates as in Perl (FastTemplate) or PHP (PHPLib).

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.

Download

The HTML template toolbox for MATLAB is available here 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 one from PHPLib.

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>