Forge Template Engine
Forge is an intuitive yet powerful template engine for MATLAB.
Forge is an intuitive yet powerful template engine crafted for generating dynamic content with MATLAB. It supports variables, comments, conditionals, and loops as fundamental elements for template creation. Additionally, Forge enables the nesting of templates for more modular dynamic rendering, alongside the capability to cache previously rendered templates to enhance rendering speed. This combination of simplicity and efficiency makes Forge an excellent option for projects of any size.
See the examples folder for more detailed examples.
Installing the toolbox will add Forge to your MATLAB path. Create an instance of this class as a renderer.
% Create the renderer
renderer = Forge();
% Create a template string with variable 'name'
tmpl = "Hello, {name}!";
% Create a context that defines the template variables
ctx.name = "world";
% Render and display the output
greeting = renderer.render(tmpl, ctx);
disp(greeting);
We can reuse the template and render it with a different context to create a new greeting message.
% Render and display with a different context
ctx.name = "friend";
greeting = renderer.render(tmpl, ctx);
disp(greeting);
We can build on this example using loops and conditional statements to greet all names in a list.
% Create the renderer
renderer = Forge();
% Create a template string to loop through 'names'.
% Use a conditional to ensure the grammar is correct if we are at the end of the list.
tmpl = "Hello {for name=names}{if name~=names(end)}{name}, {else}and {name}.{end}{end}";
% Create a context with a list of names
ctx.names = ["Jim", "Jeff", "Jane"];
% Render and display the output
greeting = renderer.render(tmpl, ctx);
disp(greeting);
This example is similar to the Basic Strings example above but shows how Forge can be used to render HTML. First create a file called post.mtl
with the defined template.
<!-- post.mtl -->
<div>
<h2 style="margin-top: 0; color: #333;">{title}</h2>
<p style="color: #666; font-size: 14px;">
Posted on <time>{date}</time> by <span style="font-weight: bold;">{author}</span>
</p>
<p style="color: #555;">{summary}</p>
<a href="{ref}" style="color: blue; text-decoration: none;">Read more →</a>
</div>
Next we load the file into MATLAB using fileread
and render it.
% Create the renderer
renderer = Forge();
% Load template to variable
blogPostTmpl = fileread("post.mtl");
% Define template variables in the context
ctx.title = "Getting started with Forge";
ctx.date = string(datetime);
ctx.author = "David";
ctx.summary = "Forge is a simple yet powerful template engine!";
ctx.ref = "/getting-started";
% Render and save the output to a .html file
post = renderer.render(blogPostTmpl, ctx);
writelines(post, "post.html");
Tags are any substring wrapped in curly braces, for example, {myvar}
. The words if
, elseif
, else
, for
, and end
are reserved keywords and should not be used for variable names. To prevent a tag from being rendered, prepend it with a backslash. The >
character is used for nesting templates. Tags should contain valid MATLAB statements using variables in the context.
Example: "{myvar}"
Comments in Forge are denoted by {%
and %}
. Text within these markers will not be rendered, allowing template authors to include notes or explanations within their templates. Comments can span multiple lines and include spaces, newlines, and other tags without affecting the output.
Example: "{% This won't show up in the rendered string %}"
Conditional logic is supported using the if
, elseif
, and else
keywords within tags. This feature enables the template to render or omit specific substrings based on given conditions. Every conditional block must be concluded with an end
tag.
Example: "{if isempty(posts)}There are no posts!{else}There are posts!{end}"
Loops are constructed with the for
keyword, following MATLAB's loop syntax, and must also be concluded with an end tag. This functionality allows for the iteration over arrays within the template. Every loop must be closed with an end
tag.
Example: "{for name=names}Hi {name}!{end}"
Nested templates are indicated by tags enclosed with {>
and }
. These allow for the inclusion of other template strings that can utilize any context variables provided by the parent template, facilitating modular template design and reuse. All subtemplates have access to variables in the context of the parent template.
Example: "{>subtemplate}"
Forge uses the MATLAB Build Tool to lint, test, and package. To run all tests and package the toolbox, run buildtool
from the repository root. Run buildtool -tasks
to see the list of all available tasks.
Forge has looked to these projects as a source of inspriation:
Cite As
David (2024). Forge Template Engine (https://github.com/dbuzinski/forge/releases/tag/v0.1.1), GitHub. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxTags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
examples/blog
examples/yaml
Version | Published | Release Notes | |
---|---|---|---|
0.1.1 | See release notes for this release on GitHub: https://github.com/dbuzinski/forge/releases/tag/v0.1.1 |
|
|
0.1.0 |
|