GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - dbuzinski/matstache: Mustache templates for MATLAB

BranchesTags
Open more actions menu

Latest commit

 

History

39 Commits
39 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
.gitmodules
.gitmodules
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
buildfile.m
buildfile.m
 
 
matlab.toml
matlab.toml
 
 

Repository files navigation

Matstache

Mustache templates for MATLAB.

Matstache is fully spec compliant and also implements lambdas. For more details on tag syntax (variables, sections, partials, delimiters, and etc.), see the mustache(5) manual.

Usage

Rendering a template

Templates can be rendered using the matstache.render function. The required inputs are the template, specified as a string scalar, and a context, which can be specified as a struct scalar or a matstache.Context scalar. The rendered text will be output as a string scalar.

template = "Hello {{name}}!";
context = struct("name", "world");
out = matstache.render(template, context);
% Hello world!

Loading templates from a files

A .mustache file can be rendered by first reading the file using fileread.

template = fileread("views/page.mustache");
context = struct("title", "Home");
out = matstache.render(template, context);

Partials

Partials can be passed to matstache.render as an optional third argument, specified as a scalar struct. The field names are the partial names and the values are the partial’s template specified as a string scalar.

template = "{{>header}}Body{{>footer}}";
context = struct("title", "Hello");
partials = struct( ...
    "header", "<h1>{{title}}</h1>", ...
    "footer", fileread("views/footer.mustache"));
out = matstache.render(template, context, partials);

Lambdas

Lambdas can be included by as function_handle values in the context.

  • Variable Tags: the handle must be callable with no arguments. The returned value is rendered as a Mustache template.
  • Sections: the handle must accept one argument, specified as a scalar string. At render time, it is called with the inner section text. The returned string replaces the section.
% Pass lambdas as variables
context = struct( ...
    "planet", "world", ...
    "lambda", @() "{{planet}}");
out = matstache.render("Hello {{lambda}}!", context); 
% "Hello world!"

% Pass lambdas to sections
ctx = struct("wrap", @(text) "<b>" + text + "</b>");
out = matstache.render("{{#wrap}}inner{{/wrap}}", ctx);
% <b>inner</b>

Custom Context Classes

A custom subclass of matstache.Context can be passed to matstache.render for structured data reuse. Context lookups use property and method names from the class.

classdef MyCustomContext < matstache.Context
    properties
        name = "world"
    end

    methods
        function context = MyCustomContext(name)
            context.name = name;
        end

        function out = greeting(context)
            out = "Hello " + context.name + "! It's a pleasure.";
        end
    end
end
% MyCustomContext defines name property and greeting method
context = MyCustomContext("Jim");
out = matstache.render("Greeting for {{name}}: {{greeting}}", context);
% Greeting for Jim: Hello Jim! It's a pleasure.

Caching

If the same template will be rendered many times, it can be more efficient to use a matstache.Renderer instance, which automatically caches parsed templates. This can have huge performance benefits for large templates or templates that will be rendered many times. The rendering is still dynamic and depends on the context passed to each render call.

% Instantiate a renderer for reuse
renderer = matstache.Renderer;

context = struct("student", "Alice", "score", 92);

tic
out = renderer.render("{{student}} got a {{score}} out of 100 on the big exam.", context);
t = toc;
fprintf("First render took %f seconds.\n", t);

context = struct("student", "Bob", "score", 89);

tic
out = renderer.render("{{student}} got a {{score}} out of 100 on the big exam.", context);
t = toc;
fprintf("Cached render took %f seconds.\n", t);

Installation

Requires MATLAB R2023a or later.

Install the .mltbx from the MATLAB File Exchange or using the Add-On Explorer.

About

Mustache templates for MATLAB

Topics

matlabmustachetemplate-enginetemplates

Resources

Readme
MIT license
Activity

Stars

1 star

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.