EMat: Embedded Matlab Templating

EMat class provides Ruby's ERB-like templating system in Matlab.
385 Downloads
Aktualisiert 22. Dez 2011

Lizenz anzeigen

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');

Zitieren als

Kota Yamaguchi (2024). EMat: Embedded Matlab Templating (https://www.mathworks.com/matlabcentral/fileexchange/32362-emat-embedded-matlab-templating), MATLAB Central File Exchange. Abgerufen.

Kompatibilität der MATLAB-Version
Erstellt mit R2011a
Kompatibel mit allen Versionen
Plattform-Kompatibilität
Windows macOS Linux
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

emat/

Version Veröffentlicht Versionshinweise
1.5.0.0

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

1.4.0.0

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

1.3.0.0

Fixed incompatibility to Matlab R2010b;
Added syntax check;

1.2.0.0

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

1.1.0.0

Added screenshot

1.0.0.0