Code covered by the BSD License  

Highlights from
EMat: Embedded Matlab Templating

4.5

4.5 | 2 ratings Rate this file 4 Downloads (last 30 days) File Size: 4.96 KB File ID: #32362
image thumbnail

EMat: Embedded Matlab Templating

by

 

28 Jul 2011 (Updated )

EMat class provides Ruby's ERB-like templating system in Matlab.

| Watch this File

File Information
Description

EMAT Embedded Matlab templating
 
    EMat class provides a templating system in Matlab like Ruby's ERB
    system. Matlab code can be embedded inside any text document to
    easily control the document generation flow.
 
    A simple example is illustrated here:
      >> x = 42;
      >> tmpl = ' The value of x is <%= x %>';
      >> obj = EMat(tmpl);
      >> disp(obj.render);
          The value of x is 42
  
  Synopsis:
 
    obj = EMat
    obj = EMat( S )
    obj = EMat( file_path )
 
    obj.set( S )
    obj.set( file_path )
 
    S = obj.render()
    obj.render( file_path )
 
    obj = EMat creates an empty EMat object. EMat object accepts
    template string either by string variable S or by specifying a
    path to the template text file_path. To set a template to the
    object, use obj.set(S) or obj.set(file_path). EMat(S) and
    EMat(file_path) are the shorthand for obj=EMat; obj.set(...);
 
    S = obj.render() returns a string of the rendered document.
    obj.render(file_path) instead renders output to a file specified
    by the file_path.
 
  Properties:
 
      tmpl_path: template file path. Use set() method to change
           tmpl: template string. Use set() method to change
         errchk: logical flag to enable/disable syntax check
                  (default: true)
           trim: logical flag to enable/disable whitespace trim when
                  suppresseing newline at the end (default: true)
 
  Template format:
    
    Any text document can embed matlab code with the following syntax.
    
    <% stmt %> matlab statement
    <% stmt -%> matlab statement with newline suppression at the end
    <%= expr %> matlab expression with rendering
    <%# comt %> comment line
    <%# comt -%> comment line with newline suppression at the end
    <%% %%> escape to render '<%' or '%>', respectively
 
    <%= expr %> renders output of the matlab expression to the output.
    Note that numeric variables will be converted to string by
    NUM2STR(). When -%> is specified at the end of the line in
    statement or comment, a following newline will be omitted from the
    rendering. Any other texts appearing outside of these special
    brackets are rendered as is. When trim property is set true,
    leading whitespace in the template is also removed from the output
    with newline suppression syntax.
 
  Example:
 
    <!-- template.html.emat -->
    <html>
    <head><title><%= t %></title></head>
    <body>
    <%# this is a comment line -%>
    <p><%= a %></p>
    <ul>
    <% for i = 1:3 -%>
      <li><%= i %></li>
    <% end -%>
    </ul>
    </body>
    </html>
 
    % In your matlab code
    % Prepare variables used in the template
    t = 'My template document';
    a = 10;
    
    % Create an EMat object
    obj = EMat('/path/to/template.html.emat');
    
    % Render to a file
    obj.render('/path/to/rendered.html');

Required Products MATLAB
MATLAB release MATLAB 7.12 (R2011a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
13 Sep 2012 Muthu Annamalai

Thanks, Yamaguchi! I like the idea of templates and the API - its a great add on for many types of projects; HTML generation, scripts etc. However my design would be different.

30 Oct 2011 Vicente  
Updates
28 Jul 2011

Added screenshot

10 Aug 2011

Added automatic num2str conversion in render.
Added support for direct file export.
Added newline suppression syntax.

11 Aug 2011

Fixed incompatibility to Matlab R2010b;
Added syntax check;

17 Aug 2011

Added trim flag to suppress whitespaces;
Added workaround to suppress unnecessary syntax warning;

22 Dec 2011

Fixed a bug that deletes the last line in a template;
Fixed a bug not escaping a '%' character in a template

Contact us