Skip to content

Rule Reference

Alexander Tsybulsky edited this page Jan 31, 2017 · 2 revisions

Rule reference

The ABAP mustache mostly follows the original mustache spec - please, read it first if you are not familiar with mustache concept. Bellow is a brief listing of the rules.

'Classical' rules/tags

Variables

  • {{name}} - variable in current context, if not found - search upper context levels
  • {{{name}}} or {{& name}} - unescaped output (e.g. '<b>' will not be converted to '&lt;b&gt;')
  • escaping is done with escape() call. Default format is cl_abap_format=>e_html_text. You can change this passing the format as iv_x_format parameter to create() or constructor(). Explicit empty value will disable escaping completely.

Sections

Section is a template like this:

{{#items}}
  <b>{{name}}</b>
{{/items}}
  • Tag with # starts a section ({{#items}}), tag with / ends it ({{/items}})
  • Section is rendered if the corresponding component is not initial. The component may be a value, a structure or a table
  • In case of non-empty table the template inside a section is repeated for each license
  • Section tag can also start with ^ (like {{^items}}) - this mean inverted condition: the section is rendered if the component IS initial
  • If a table consists only from one unnamed element (DATA tab TYPE TABLE OF strings), the line content can be addressed with a special tag {{@tabline}}. E.g. {{#string_tab}}{{@tabline}},{{/string_tab}}

Variable context

  • If the section component is a table or a structure it is added on top of variable context stack.
    • E.g the root structure contains component name and table items
    • each of items contain components name and price
    • then {{name}} tag inside the section will address name on item level
    • if it does not find it on item level it will search previous levels recursively

Partials

ABAP mustache supports partials. You may think of partials as of includes. Here is an example:

The root template:

Welcome to {{store_name}}!
{{>offers}}

The 'offers' template

{{#items}}
  * {{name}} - {{price}}
{{/items}}

Partials are registered with add_partial() call (see API Reference). A Partial may contain other partials.

Other tag types

  • {{! ignore me }} - comment, is not rendered to output
  • {{=<% %>=}} - changes tag delimiters, e.g. to <%, %> in the example
{{=<% %>=}}
Hello <% variable_name %>
<%={{ }}=%>

Ideas for future development

The below is mainly inspired by handlebars.js library and might be implemented in future.

  • {{> partial someParameter="XXX" }} - parameters to sections and partials
  • {{../permalink}} - explicit path to variable
  • {{#if name}}{{else}} - if/else operator. Actually can be achieved by combination or # and / section but looks better
  • {{#each list}}{{else}} - another syntaxic sugar for lists
  • special tags for lists {{@index}}, {{@last}}, {{@first}}
  • {{#with struc}} - another version of section, the feature is limiting upper level context search

Also potentially interesting improvements:

  • Support references to objects and object attributes
  • Support calling object methods for data

Clone this wiki locally