Skip to content

API Reference

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

API reference

Instance creation

To create instance of class use regular constructor or method create(). Both accept template in form of string (iv_template) or table of strings (it_template). Note that table template will be rendered with line-breaks between lines.

  data lo_mustache type ref to lcl_mustache.

  lo_mustache = lcl_mustache=>create( 'Hello {{name}}!' ).

*OR

  data lt_strtab type string_table.
  append 'Hello {{name}}!' to lt_strtab.
  append 'Good bye {{name}}!' to lt_strtab.

  lo_mustache = lcl_mustache=>create( it_template = lt_strtab ).

*OR

  create object lo_mustache
    exporting
      iv_template = 'Hello {{name}}!'.

There is also an optional parameter iv_x_format which controls how escaping of values is done (escape() function is used). Default option is cl_abap_format=>e_html_text. Pass empty value to completely disable escaping.

Adding partials

To register a partial use method add_partial(). It accepts 2 parameters:

  • iv_name - label to call the partial,
  • io_obj - reference to another lcl_mustache instance.

A partial may contain another partials. But only up to 10th level of depth. This may be changed though by changing C_MAX_PARTIALS_DEPTH constant (in lcl_mustache class).

  lo_mustache = lcl_mustache=>create(
    'Welcome to {{store_name}}!' && c_nl && " c_nl is a constant with newline char
    '{{>offers}}' ).

  lo_partial = lcl_mustache=>create(
    '{{#items}}'               && c_nl &&
    '  * {{name}} - {{price}}' && c_nl &&
    '{{/items}}' ).

  lo_mustache->add_partial( iv_name = 'offers' io_obj = lo_partial ).

Rendering

To render the template on the given data use method render() to receive a string or method render_tt() to receive a table of strings.

  data lv_output type string.
  lv_output = lo_mustache->render( ls_some_structure ).

As input render methods expect a structure or a table. Structure/table line may have any content, it will be automatically detected based on component name. A component may be a table - to support data for repetative sections. If the initial parameter is a table then there are 2 options: this table has type of ty_struc_tt (see below) or any other - then the template will be rendered for each line.

Universal structure lcl_mustache=>ty_struc_tt

ty_struc has the following structure:

  begin of ty_struc,
    name type string,
    val  type string,
    dref type ref to data,
  end of ty_struc,
  • name - component name
  • val - component value
  • dref - reference to component value (may be a reference to value, structure or table)

Thus the table ty_struc_tt is treated as a linear structure. This is mainly intended to be the root structure. Supposedly it is convenient to prepare parts of data as regular structures and tables. However, combining them all together at the top level in yet another dedicated structure may be cumbersome. This is where lcl_mustache=>ty_struc_tt may serve well. Optionally of course.

  data lt_data type lcl_mustache=>ty_struc_tt.
  data lv_output type string.

  lt_data = value #(
    ( name = 'user'  val = sy-uname )
    ( name = 'other' val = 'some other value' ) ) ).

  lv_output = lo_mustache->render( lt_data ).

Note on line-breaks

Supposed that input template table 'has' line-breaks between lines. Also each line of such table or also a single string template are split into separate string lines at line-breaks. Thus internally:

  string1
  string2\nstring3 << '\n' is a line break here
  string4

becomes

  string1
  string2
  string3
  string4

This line structure is then kept after rendering resulting in:

render_tt():
  rendered_string1
  rendered_string2
  rendered_string3
  rendered_string4

*OR

render():
  rendered_string1\nrendered_string2\nrendered_string3\nrendered_string4

Exceptions

On error lcl_mustache methods will throw lcx_mustache_error exception. It is inherited from cx_static_check. An instance of the exception contain 2 public read-only attributes:

  • msg - string describing the error,
  • rc - error code (for debug and unit test only).

Utilities

zmustache include contains also an utility class lcl_mustache_utils. It is used internally by lcl_mustache but also can be useful for other purposes. It implements 2 methods:

split_string() - splits a string at LF or CRLF or given iv_sep character into table of strings.

  • importing:
    • iv_text type string
    • iv_sep type clike optional
  • returning: rt_tab TYPE string_table

join_strings() - joins a table of strings into single string with a given iv_sep character (if not supplied, then cl_abap_char_utilities=>newline)

  • importing
    • it_tab type string_table
    • iv_sep type clike optional
  • returning rv_text TYPE string

Clone this wiki locally