Main functions

List of functions in the src folder.


src.octache(varargin)

Renders mustache templates

USAGE:

output = octache(path_to_file_to_render, ...
                'data', path_data_JSON, ...
                'partials_path', pwd, ...
                'partials_ext', 'mustache', ...
                'l_del', '{{', ...
                'r_del', '}}', ...
                'warn', true, ...
                'keep', true);
Parameters:
  • template (path or char) – The file or string to render

  • partials_path (path) – The directory where your partials reside. Default is pwd.

  • data (path) – The json data file or an equivalent structure

  • partials_ext ((1 x n) char) – The extension for your mustache partials, mustache by default

  • left_delim ((1 x n) char) – The default left delimiter, {{ by default

  • right_delim ((1 x n) char) – The default right delimiter, }} by default

  • warn (boolean) – Print a warning for each undefined template key encountered

  • keep (boolean) – Keep unreplaced tags when a template substitution isn’t found in the data

EXAMPLE 1:

output = octache('"Hello {{value}}! {{who}}"', ...
                 'data', struct('value', 'world', ...
                                'who', 'I am Octache'))
  1. Copyright 2022 Remi Gau

src.renderer(varargin)

Render a mustache template with a data scope and partial capability.

USAGE:

output = renderer(template, ...
                    'data', data, ...
                    'partials_path', pwd, ...
                    'partials_ext', 'mustache', ...
                    'scopes', {}, ...
                    'l_del', '{{', ...
                    'r_del', '}}', ...
                    'padding', '', ...
                    'partials_dict', struct([]), ...
                    'warn', true, ...
                    'keep', true);
Parameters:
  • template (path or char or a (n X 2) cell) – the path to a mustache file, a string or a cell of tokens to render

  • data (structure or char) – The content of a json data file or a string

  • path (path) – The path where your partials reside. Default is pwd.

  • ext ((1 x n) char) – The extension for your mustache partials, mustache by default

  • scopes (cell) – A stash organized as a cell of structure that get_key will look through. Default: {}.

  • l_del ((1 x n) char) – The default left delimiter, {{ by default

  • r_del ((1 x n) char) – The default right delimiter, }} by default

  • padding ((1 x n) char) – This is for padding partials, and shouldn’t be used (but can be if you really want to)

  • partials_dict (structure) – Will be search for partials before the filesystem is. struct('include', 'foo') is the same as a file called include.mustache. Defaults: struct([]).

  • warn (boolean) – Print a warning for each undefined template key encountered

  • keep (boolean) – Keep unreplaced tags when a template substitution isn’t found in the data

Returns:

  • output:

    (char) A string containing the rendered template.

EXAMPLE 1:

output = renderer('"Hello {{value}}! {{>who}}"', ...
                  'data', struct('value', 'world'), ...
                  'partials_dict', struct('who', 'I am Octache'))

EXAMPLE 2:

Given the file structure:

|- main.ms
|- main.m
|__ partials
    |- part.ms

then main.m would make the following call:

output = renderer(fullefile(pwd, 'main.ms'), ...
                    'data', struct('value', 'world'), ...
                    'partials_path', fullefile(pwd, 'partials'), ...
                    'partials_ext', 'ms', ...
                    'scopes', {}, ...
                    'warn', true, ...
                    'keep', true)
  1. Copyright 2022 Remi Gau

src.tokenize(varargin)

Tokenizes a mustache template in a generator fashion, using file-like objects. It also accepts a string containing the template.

USAGE:

tokens = tokenize(template, 'l_del', '{{', 'r_del', '}}')

Arguments:

Parameters:
  • template (path or char) – the path to a mustache file or a string to render

  • l_del ((1 x n) char) – The default left delimiter, {{ by default

  • r_del ((1 x n) char) – The default right delimiter, }} by default

Returns:

  • tokens:

    A n x 2 cell of mustache tags in the form {tag_type, tag_key}

Where tag_type is one of:

  • literal

  • section

  • inverted section

  • end

  • partial

  • no escape

  • set delimiter

And tag_key is either the key or in the case of a literal tag, the literal itself.

  1. Copyright 2022 Remi Gau