#1 2018-09-21 00:21:24

jairgza
Member
Registered: 2015-01-15
Posts: 10

Mustache know which tags where not found in data context

I recently need to use mustache engine and I really like it a lot, use to render some xml templates.

Is there a way to know which tags where not found in data context, and not rendered ?

The docs are very clear, when a tag in a template is not found in the current data context nothing wil be rendered (and the tag is deleted in result text)

But some templates are created by users and maybe write wrong tags, right now these tags are just deleted
It would be a great help if know which tags where not found in the data context so can validate and display a warning or raise an error
Maybe have a "MissingTags" property to store tags not found on Parse, so we can do something like this:

  rtext := mustache.Render( vData );
  
  //Validate missing tags
  if mustache.MissingTags.Count > 0 then
    showmessage( mustache.MisingTags.Text );

Or another idea is have a flag to disable delete missing key-tags from the result text, so I can then inspect the result for tags not rendered


Thanks a lot

Offline

#2 2018-09-21 17:05:13

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,263
Website

Re: Mustache know which tags where not found in data context

There is no such thing as a "missing" tag in Mustache I am afraid.
There is no expected "1 for 1" mapping between data context and template.

This is a feature that the same data context could be rendered by several Mustache templates, and contain much more data than expected by the templates.
In fact, on production, I always have much more data than the data which is there.

To display an error, you add a section to be rendered on a missing tag.

Or did I miss something from your question?

Offline

#3 2018-09-22 21:57:39

jairgza
Member
Registered: 2015-01-15
Posts: 10

Re: Mustache know which tags where not found in data context

Just to clarify what im doing and why ask this
I'm generating some xml for electronic signed digital invoice and tax declaration in my country, so I want to validate as much as possible the template and result text to avoid invalid xml
The templates sometimes are created by other people and even though there are manuals, errors occur.
ej:

**Data context sample
{
  "DName" : "Jhon Doe",
  "DAux" : "",
  "DAmount" : 100,
  "DTax" : 8
}

**Template
Name is {{DName}}
Aux is {{DAux}}
Amount is {{DAmount}}
Tax is {{DTaxxx}}      <- misspelled variable tag

**FinalRender
Name is Jhon Doe
Aux is              <- Empty string value OK (key en context)
Amount is 100
Tax is              <- Empty value because not found in context (can log variable name DTaxxx in an array for later validation ?? )

I know I can't protect every aspect of the template, but at least for my specific case every variable name in template must be in the data context,
if I could know if there where "tag variables" not rendered because were not found in the data context then I can do something and at least secure a little more the generation of the result xml

The Parser() process already performs the search of variable names recursively within the current context, if not found nothing will be rendered so maybe here just could add the "variable name" to an array just in case someone wants to validate it later


  rtext := mustache.Render( vData );
 
  if Length( mustache.VariableTagNotRenderedArray ) > 0 then
  begin
    showmessage('Template wrong, invalid variables !!');
    //todo: maybe display wrong variable names in array
    exit;
  end;


I just want to explain a little more what I'm trying to achieve and see if is feasible or useful

thanks

Offline

Board footer

Powered by FluxBB