File Exchange

image thumbnail

EMat: Embedded Matlab Templating

version 1.5 (4.96 KB) by

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



View License

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
    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.
      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.
    <!-- template.html.emat -->
    <head><title><%= t %></title></head>
    <%# this is a comment line -%>
    <p><%= a %></p>
    <% for i = 1:3 -%>
      <li><%= i %></li>
    <% end -%>
    % 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

Comments and Ratings (2)

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.




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


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


Fixed incompatibility to Matlab R2010b;
Added syntax check;


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


Added screenshot

MATLAB Release
MATLAB 7.12 (R2011a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video