abap_mustache
A Mustache template engine for ABAP, rendering logic-less templates into HTML or any other text from a structure or table of any type, with partials and a separate Steampunk-compatible branch.
Implements the original mustache spec plus some additions, which the maintainer marks as mainly in plans now; the Plans section lists if/else, each/else, inline partials and helpers as things the maintainer might implement with time. The input may be a structure or a table of any type, detected automatically, and for a table the template is repeated for each line, to a depth that is not limited; a template may be a string or a table of strings and the output comes back as either, and partials are supported, so templates can be split into sections and reused. A Steampunk (740+) compatible version lives in a branch of its own, contributed by DerGuteWolfDerGuteWolfmaintains 1 entry, contributed to 5 entries: it is somewhat slower than master because of the whitelisted features Steampunk allows, and the maintainer says it may become the default in the near future. Installation is a clone into the system with abapGitabapGitA Git client written in ABAP that runs inside the system, so objects are cloned, pulled and pushed without leaving it; with no package manager in ABAP, a cloned repository is how code is distributed.community org, abap, or creating the zmustache and zmustache_ut includes and filling them with the two program files.
Facts
Where it sits
Who is behind it
In the atlas: maintains 5, contributes to 11@sbcguamaintainerKjetil KilhavnContributes to this
In the atlas: contributes to 6@kjetil-kilhavnLars HvamContributes to this
In the atlas: maintains 13, contributes to 92, wrote 1@larshpTheodore TsirpanisContributes to this
In the atlas: contributes to 1@teo-tsirpanis3 contributors
Before choosing this
Signals
ActivityQuietNo published releases; repository quiet for 28 months
It publishes no releases, and its repository has been quiet for at least 24 full months. This may be normal for stable software; check whether its current state still fits your use.
What it declares
Every artifact
- Type
- Package
- Origin
- Independently maintained
As a package
- Where it runs
- Inside its host
- Distributed through
- abapGitCopied into the project
Connections
Producing a web user interface from ABAP code
Template for ABAP applications with an HTML UI
Download
README
quoted from sbcgua/abap_mustache under MIT, readABAP Mustache
Mustache template engine for ABAP. It implements the original 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.
Versions
The library has a Steampunk (740+) compatible version, thanks to @DerGuteWolf. Currently it lives in the steampunk 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 the PR comments), but it has own advantages and may 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
Example
Let's assume you have some data like this:
types:
begin of ty_shop_item
name 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_utilities
lo_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 the ls_my_data appropriately):
Welcome to My Super Store
* Boots - $99.00
* T-Short - $49.00
* Shirts - $59.00
Supported input and technical details
Data structure
- 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 (case insesitive) 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.
append initial line to lt_uni_data assigning <i>.
<i>-name = 'shop_name'.
<i>-val = 'My Super Store'.
append initial line to lt_uni_data assigning <i>.
<i>-name = 'items'.
get reference 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.
Templates
- Template may be a string or a table of strings (
type string_table)
lo_mustache = lcl_mustache=>create( lv_template ). " iv_template = lv_template
* OR
lo_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 ).
* OR
lt_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).
Partials
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
More
Further information can be found in wiki. There are also some notes on performance.
Installation
- The best option to install is to clone the repository to your SAP system using abapGit 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).
Plans
Here are some stuff I might be implementing with time:
- Some stuff inspired by Handlebars, 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
Contribution is always welcomed :)
License
The code is licensed under MIT License. Please see LICENSE for details.
Others providing
Compare all 17 on the UI written in ABAP page →- abap2UI5-localabap2UI5-localThe whole abap2UI5 framework folded into the local classes of one HTTP handler, so an app runs without the framework installed as a package.community org, abapabap2UI5 folded into the local classes of one HTTP handlerfrom ABAP
- abap2UI5abap2UI5One ABAP class implementing z2ui5_if_app is the whole app: the browser loads a generic UI5 shell once, and every interaction after that is an HTTP roundtrip back into ABAP.community org, abapUI5 apps written as one ABAP class, rendered by a generic shellfrom ABAP
- abap-cloud-guiabap-cloud-guiA report-style programming model for abap2UI5: one class produces a selection screen with a WRITE list or an ALV grid, running as a UI5 app on ABAP Cloud as well as on NetWeaver 7.02.independent, abapReport-style programming model for abap2UI5 on ABAP Cloudfrom ABAP
- custom-controlscustom-controlsThirteen custom controls for abap2UI5, a signature pad and a scanner that reads several barcodes at once among them, each one an ABAP builder class with a single render( ) method and all of them shipped in a BSP of their own.community org, abapThirteen custom UI5 controls for abap2UI5 appsfrom ABAP
- http-connectorhttp-connectorAn add-on that makes the abap2UI5 apps of one ABAP system reachable through another: a thin ICF handler on the consumer system forwards every roundtrip to the source system through an SM59 destination.community org, abapForwards abap2UI5 roundtrips to another system over HTTPfrom ABAP
- layout-managementlayout-managementAn abap2UI5 addon that renders an internal table or a structure as a UI5 table or form with a settings popup, where the user lays it out and saves the result as a layout variant.independent, abapTable and form layouts with saved variants for abap2UI5from ABAP
- abap2UI5 for Excelabap2UI5 for ExcelAn Office add-in that runs an abap2UI5 app in Excel's task pane, where the app reads and writes the workbook through the z2ui5.cc.ExcelBridge control.community org, abapOffice add-in running an abap2UI5 app in Excel's task panefrom ABAP
- abap2UI5-addons/popupsabap2UI5-addons/popupsReady-made dialogs for an abap2UI5 app: confirm, inform, select from a list, file up- and download, table, text edit, PDF, HTML, messages, range, value help and search help, each opened with one factory( ) call.community org, abapReady-made dialogs for abap2UI5 appsfrom ABAP
- rap-extrap-extFloorplan classes that read a CDS entity's UI annotations at runtime and render a list report, worklist, tree, object page, overview page, value help or action dialog as an abap2UI5 app.community org, abapFloorplans rendered from CDS UI annotations as abap2UI5 appsfrom ABAP
- selection-screenselection-screenSelect-options with saved variants for an abap2UI5 app, built from a database table name or from an internal table the app passes in.independent, abapSelect-options with saved variants for abap2UI5 appsfrom ABAP
- abap2UI5 frontend-webcomponentabap2UI5 frontend-webcomponentAn alternative abap2UI5 frontend: one ES module registering <abap2ui5-app>, which renders the views an unchanged abap2UI5 app class sends with UI5 Web Components and no UI5 core.community org, abapabap2UI5 frontend rendering with UI5 Web Componentsfrom ABAP
- abap2UI5 frontendabap2UI5 frontendThe abap2UI5 frontend as installable ABAP artefacts: per system type one generated branch carrying the BSP or HTTP service with its handler, pulled into the system with abapGit.community org, abapabap2UI5 frontend as ABAP artefacts installed with abapGitfrom ABAP
- abap2UI5 Layout Editorabap2UI5 Layout EditorA browser editor that lays out a UI5 view by drag and drop and writes it out as one abap2UI5 app class, pulled into the system with abapGit.community org, abapDrag-and-drop UI5 view editor that writes an abap2UI5 app classfrom ABAP
- abap2UI5 protocolabap2UI5 protocolThe abap2UI5 roundtrip written down: a versioned specification of the wire between frontends and backends, JSON Schemas for its messages, and two conformance suites that test either side.community org, abapVersioned specification of the abap2UI5 roundtrip protocolfrom ABAP
- ZFPM_TOOLSZFPM_TOOLSA report that shows the development objects of an FPM hierarchy in one view and edits, copies, deletes, imports and exports the hierarchy as a whole rather than one object at a time.independent, abapEdits, copies and transports a whole FPM hierarchy at oncefrom ABAP
- ZWD_CALENDAR_VHZWD_CALENDAR_VHA value help for date fields in Web Dynpro ABAP screens that shows a calendar view, so the user picks a day from it instead of typing the date.independent, abapWeb Dynpro ABAP calendar value help for date fieldsfrom ABAP
Details
Sources
Sources are graph entities: the same page may back several entries, and an unreachable source flags every row that rests on it.
Compatibility and provenance
abaplint
Flavors
“The library has a Steampunk (740+) compatible version, thanks to @DerGuteWolf. Currently it lives in the [steampunk](https://github.com/sbcgua/abap_mustache/tree/steampunk) branch.” — sbcgua/abap_mustache — README, Versions
Contributors
History in the atlas
- First seen.
For your agent
- Ref
artifact:sbcgua-abap-mustache- JSON
- /artifacts/sbcgua-abap-mustache.json
- MCP
- Connecting an agent