Views:
TypeRead/writeAuthorAvailability
Direct providerReadFinbourneProvided with LUSID

The Tools.Mustache provider enables you to write a Luminesce query that applies Mustache templating to a dataset. 

Note: The LUSID user running the query must have sufficient access control permissions to use this provider. This should automatically be the case if you are the domain owner.

You can use this provider in conjunction with other providers to return data in Mustache templating, see example 4.

Basic usage

@data = select <column> from <some-provider> where <filter-expression>;

@mustache_txt =
use Tools.Mustache with @data
{{#@data}}
  {{<column>}}
{{/@data}}
enduse;

select * from @mustache_txt

Input tables

Tools.Mustache can take in different kinds of variables and outputs a table of data which can take various forms:

InputOutputInformationSyntax
@@variablesTable of data with one row and columnSimple scalar variables which can be used within Mustache templates. See example 1.
@@data = select <some-data>;

@mustache_txt = 
use Tools.Mustache with @@data
<optional-text> {{@@data}} <optional-text>
enduse;

select * from @mustache_txt
@variablesTable of data with one row and columnFull tables of data which can be used within Mustache templates. Requires a specific syntax to be able to iterate over rows. Using this syntax, column names can be specified within Mustache templates to return values. See example 2.
@data = select <column> from <dataset>;

@mustache_txt = 
use Tools.Mustache with @data
{{#@data}}
 {{<column>}}
{{/@data}}
enduse;

select * from @mustache_txt
@partialsTable of data with one row and columnA table with two columns (Name and Template) of type string which allow partial templates to be defined for reuse, for example when creating custom views. Requires a specific syntax. See example 3. 
@data = select <some-data> as <some-name>, <other-data> as <other-name>;
   
@partials =
select '' as Name, '' as Template where 1 = 2 -- required, this just has the effect of creating two columns 
union all
values
    ('<some-string>', '{{<some-name>} <optional-text> {{<other-name>}}')
;

@mustache_txt = 
use Tools.Mustache with @partials, @data
{{#@data}}
 {{><some-string>}}
{{/@data}}
<optional-text>
enduse;
    
select * from @mustache_txt
@foreachTable of data with one or more rows and columns

Returns all columns specified in a multi-line result, plus an additional column containing the populated Mustache template for each row. See example 4.

Also returns a nested table if AsTable is specified in the Mustache template. See example 5.

@foreach = select <column>[..., <another-column>] from <dataset>;

@mustache_txt = 
use Tools.Mustache with @@foreach
{{<column>}} <optional-text> {{<another-column>}}
enduse;

select * from @mustache_txt

Examples

Example 1: Applying Mustache templating to some data

In this example, we use a simple scalar variable with Tools.Mustache to return a table of data with one column and one row which contains our completed Mustache template. 

@@txt = select 'just like this';

@mustache_txt = 
use Tools.Mustache with @@txt
You can use Mustache templating {{@@txt}}
enduse;

select * from @mustache_txt


The table of data returned looks like this:

Example 2: Creating a Mustache template with a table of data

In this example, we create an input table of data which we use with Tools.Mustache to create a Mustache template.

@data = select 'this' as SomeText, 'that' as MoreText, 10 as SomeNumber;
    
@mustache_txt = 
use Tools.Mustache with @data
{{#@data}}
  You can use {{SomeText}} and {{MoreText}} in Mustache templating.
  And even {{SomeNumber}}!
{{/@data}}
enduse;

select * from @mustache_txt


The table of data returned by the query looks like this, with the Mustache template populated with values:

Example 3: Creating a reusable partial Mustache template

You can use a special kind of variable called @partials in your query to define a reusable partial Mustache template which you could use, for example, to create custom views that output a populated Mustache template.

In this example, we create a table of data and populate a @partials variable with the required special syntax. We then use both variables with the Tools.Mustache provider to return a single cell table of data. 

@some_data = select 'x' as SomeText, 'y' as MoreText, 4 as SomeNumber;
   
@partials =
select '' as Name, '' as Template where 1 = 2 -- required, this just has the effect of creating two columns 
union all
values
    ('XRow', '{{SomeText}} : {{SomeNumber}} on {{MoreText}}'),
    ('YRow', '{{MoreText}}')
;

@mustache_txt = 
use Tools.Mustache with @partials, @some_data
{{#@some_data}}
 {{>XRow}}
{{/@some_data}}
and
{{#@some_data}}
 {{>YRow}}
{{/@some_data}}
enduse;
    
select * from @mustache_txt


The table of data returned by the query looks like this, with the partial template populated with data from our @some_data variable, all within the larger Mustache template:

Example 4: Creating a Mustache template which outputs multiple rows

In this example, we use a special kind of variable called @foreach with the Tools.Mustache provider to return all columns and rows in the table, plus an additional column containing a populated Mustache template for each row.

@foreach = select distinct TableName, FieldName, DataType from Sys.Field;
    
@mustache_txt = 
use Tools.Mustache with @foreach
{{TableName}}.{{FieldName}} -> {{DataType}}
enduse;

select * from @mustache_txt


The first ten rows of the table of data returned by the query looks like this:

Example 5: Creating a Mustache template which outputs a nested table

In this example, we first define a @foreach variable which uses the group_concat aggregate function with a data field. This variable is then used with Tools.Mustache to create a Mustache template that outputs a nested table in an additional column.

Note the specific syntax used in @mustache_txt. When constructing the Mustache message, AsTable is appended to the column we want to format as a nested table. Within the column which is to become a nested table, fields are split on , and each field is referred to by its order number e.g., {{1}}, {{2}}

@foreach = select
TableName,
group_concat(FieldName) filter (where FieldType = 'Column'as Columns
from Sys.Field
group by TableName;
    
@mustache_txt = 
use Tools.Mustache with @foreach
{{TableName}}:
{{#ColumnsAsTable}}
  {{1}}
{{/ColumnsAsTable}}
enduse;

select * from @mustache_txt


The first ten rows of the table of data returned by the query looks like this: