- Notifications
You must be signed in to change notification settings - Fork11
Mustache template engine for ABAP
License
sbcgua/abap_mustache
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Mustache template engine for ABAP. It implements theoriginal mustache spec plus some additions (mainly in plans now :) Mustache is a logic-less template syntax. It is good for generation of HTML or any other text stuff.
The library has a Steampunk (740+) compatible version, thanks to @DerGuteWolf. Currently it lives in thesteampunk branch. This version, at the time of publication, is somewhat slower compared to the original (master) due to limited whitelisted features of the steampunk (see thePR comments), but it has own advantages andmay become the default in near future.If you are using the library in older environments, please notify in the issues to take it into account
Let's assume you have some data like this:
types:begin of ty_shop_itemname type string, price type s_price,end of ty_shop_item, ty_shop_item_tt type standard table of ty_shop_item,begin of ty_shop, shop_name type string, items type ty_shop_item_tt,end of ty_shop.
Create an instance of abap mustache class, feed the desired template to it and then render with your data:
data lo_mustache type ref to lcl_mustache.data lv_text type string." c_nl is a shortcut for newline char, e.g. from cl_abap_char_utilitieslo_mustache= lcl_mustache=>create('Welcome to {{shop_name}}!'&& c_nl&&'{{#items}}'&& c_nl&&'* {{name}} - ${{price}}'&& c_nl&&'{{/items}}' ).lv_text= lo_mustache->render( ls_my_data )." ls_my_data type ty_shop
As the result (lv_text) you'll get some output like this (assuming you filled thels_my_data appropriately):
Welcome to My Super Store* Boots - $99.00* T-Short - $49.00* Shirts - $59.00- Input data may be a structure or a table of any type - it is detected automatically. For a table the template is repeated for each line. The depth of the data is not limited for the moment (so can be table in struc in table in struc ...).
- In addition, there is a special table type
lcl_mustache=>ty_struc_ttwhich processed as a universal structure. Fieldnamecorresponds to tag name (caseinsesitive) and the value may be put either tovalfield or as a reference todref. The latter can be then a reference to a structure or a table. So the above example may would look like:
data lt_uni_data type lcl_mustache=>ty_struc_tt.field-symbols<i> like line of lt_uni_data.appendinitial line to lt_uni_dataassigning<i>.<i>-name='shop_name'.<i>-val='My Super Store'.appendinitial line to lt_uni_dataassigning<i>.<i>-name='items'.getreference of lt_items to<i>-dref." lt_items is the ty_shop_item_tt filled elsewhere...lv_text= lo_mustache->render( lt_uni_data ).
- The
lcl_mustache=>ty_struc_ttis 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 wherelcl_mustache=>ty_struc_ttmay serve well. Although you are free to choose your own way of course.
- Template may be a string or a table of strings (
type string_table)
lo_mustache= lcl_mustache=>create( lv_template )." iv_template = lv_template* ORlo_mustache= lcl_mustache=>create( it_template= lt_template_tab )." TABLE of strings
- Output can be received as a string or a table of string (
type string_table)
lv_text= lo_mustache->render( ls_my_data ).* ORlt_text_tab= lo_mustache->render_tt( ls_my_data )." TABLE
- Template as table is supposed to be separated by newlines. So in case of string rendering newline char will be inserted between template lines. In case of
render_ttnewline char is not inserted (still if any template line contain line breaks it will be split into several output table lines). - Input string template is split by newline chars for internal processing. Both
LFandCRLFseparators are supported (detected automatically).
The class supports partials (see example below). So you may prepare your templates in convenient and logical sections. And reuse them if necessary.
lo_mustache= lcl_mustache=>create('Welcome to {{shop_name}}!'&& c_nl&&'{{>items_template}}' )." << Calling partial !lo_partial= lcl_mustache=>create('{{#items}}'&& c_nl&&'* {{name}} - ${{price}}'&& c_nl&&'{{/items}}' ).lo_mustache->add_partial(" Register partial iv_name='items_template' io_obj= lo_partial ).lv_text= lo_mustache->render( ls_my_data )." ls_my_data type ty_shop
Further information can be found inwiki. There are also some notes on performance.
- The best option to install is to clone the repository to your SAP system usingabapGit tool.
- Alternatively, create
zmustacheandzmustache_utincludes and fill with the content ofzmustache.prog.abapandzmustache_ut.prog.abaprespectively.zmustache_utis the unit tests include, so you might potentially skip it (just remove the references toltcl_mustachefrom the first include).
Here are some stuff I might be implementing with time:
- Some stuff inspired byHandlebars, in particular:
- inline partials (within one template)
if/elseeach/elsewithand maybe path features (not sure)- section parameters
- maybe some sort of helpers
- calling object methods for parts of data
- references to object attributes (maybe, if will be needed)
Still some real usage statistics would be valuable to decide on this or that feature.
Contribution is always welcomed :)
The code is licensed under MIT License. Please seeLICENSE for details.
About
Mustache template engine for ABAP
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.