Link Search Menu Expand Document

Mustachioed template engine

   Most modern template engines in the web (and also in ABAP) work according to one simple principle.
Data + Template = Ready document
So how is XTT different from its peers?

image





   Without diminishing the merits of more popular report generation libraries, let’s compare XTT with the main ones:

image




  • abap2xlsx uses ZCL_EXCEL_READER_2007 class to read a template

image




  • ZWWW uses the familiar WYSIWYG MS Excel or Word editor.
    Bookmarks are also needed in Word (2 previous ones specialize in Excel format)

image





XTT - Xml template toolkit

   This opus is more like the latest development and maximizes the capabilities of the WYSIWYG editor itself.
For data transfer, as in the XLSX Workbench, the concept of context is used.

   All you need to do to combine the data with the template is to call the MERGE method
Context IS_BLOCK (usually an ABAP structure) matches the structure of the document.
The root label (usually ‘R’) is an optional parameter and is most likely needed if the data is combined with the template several times.

image





   Since the MERGE signature is as simple as possible, the main difficulty can arise with the template itself

image




I hope, due to its obviousness, in most cases the study of {mustachioed syntax} is not required at all.
The structure of the document will look approximately the same everywhere.

    " Document structure
    BEGIN OF ts_root,
      header   TYPE string,
      t        TYPE tt_rand_data, " internal flat table ( In template {R-T} )
      date     TYPE d,            " 8
      datetime TYPE char14,       " date(8) + time(6)
    END OF ts_root.

MS Excel

image

MS Word

image

Adobe LiveCycle

image





Directives

   All special features (directives) are always inside {curly braces}
They, quite conditionally, can be divided into 3 main groups:

  • 1) Related to one value
  • 2) Clarification for the entire table or tree
  • 3) Block’s directives

Let’s look at each of them individually:



1) Directives for one value

1-1) ‘;type=’ Data type

   The first part of them relates to the clarification of data type {;type=}
In general, it is advisable to leave the type as is without converting to others.

  • If the report contains numbers, then they do not need to be converted to strings. If you need to output an empty cell instead of 0, it is easier to use a change in the format itself.
    Ctrl+1 - > Custom -> 0;-0;;@

  • For dates, conversion to strings is also not advisable. If the report displays a number like 43964 instead of the date, just change the format of the cell itself to the date

But there are exceptions:

  • {;type=boolean} & {;type=datetime} since they are not in the ABAP language itself

  • {;type=mask} if you need an analog of WRITE TO (for example WBS element)

  • {;type=integer} to convert from type CHAR
    If you are 100% sure that only numbers will be in the characters (but even in this case it is advisable to declare the type as NUMC for the report)

With (BTRTL) and without(WERKS) type specification

image

Result

image




1-2) ‘;cond=’ Conditional output

    ‘;cond=’ addition allows you to write a simple condition for displaying one or the other value using String Templates or any other ABAP operator (SWITCH, COND, VALUE) that returns only one resulting value

1-3) ‘;func=’ Built-in functions

   The another part is related to aggregation functions ;func= SUM | AVG | COUNT | FIRST which work in conjunction with 3-2) tree output directives.
For more complex cases there is a special event in ABAP

For simple totals in Excel, you can use standard tools

image

But if you need a universal method that works in Word and Pdf, you can use these functions

image



2) Clarification for a table or tree

    Unlike previous directives that were directly in the cell itself, this kind of directives can be placed anywhere in the template and works for entire table or tree

2-1) ‘;direction=column’

{;direction=column} works only for in the ZCL_XTT_EXCEL_XLSX class and specifies how to display the table. By default, tables and trees are displayed row by row.

   If you need a dynamic table that expands row by row, and column by column please look at this example

2-1) ‘;group=’

 Allows you to group tabular data in a hierarchical structure similar to a tree.
 To convert a table to a tree, you can use ABAP methods zcl_xtt_replace_block=>tree_create or zcl_xtt_replace_block=>tree_create_relat.
 But making such a group declaration in the template itself allows you to give a clear picture of the fact that the table has been transformed into a tree.

  • Instead tree_create => {;group=Comma-separated table fields}.
    For subtotals with BUKRS {R-T;group=BUKRS}

  • Instead tree_create_relat => {;group=Fields Parent-subsidiary through dash}
    For tree org units by IT 1001 {R-T;group=OBJID-SOBID}

 In the simplest case, you need to create simple subtotals,
level=0 (overall totals) in which there is level=1 with the data of the table itself

image


 If we need subtotal on BUKRS {;group=BUKRS} we get 3 levels (0,1,2)
level=0 Total for all BUKRSs
level=1 Total for 1 BUKRS
level=2 Table data

In the ZCL_XTT_EXCEL_XLSX class, the declaration of a tree at the sheet level makes it possible to display the same table on different sheets with different groupings

In this example, the table {R-T} is displayed differently depending on the declaration on a particular sheet

image




3) Block’s directives

3-1) ‘;type=block’ Conditional block

‘;type=block;’ addition together with ‘;cond=’ addition is intended for conditional output of the one block.

That is, it works similarly to ‘show_if’ for a tree. But it is intended for one-time execution only. ‘show_if’ works for each table row and their subtotals at different levels

3-2) Tree output directives

 After the table has been converted to a tree, you can set the boundaries and output conditions of each level.

  • ;level= Specifies for which level this row (column) is intended
  • ;top=X It says that the withdrawal will be carried out before a lower level (empty or ;top= that after a lower level)
  • ;show_if= & ;hide_if= allow you to specify the output condition of a particular level
    The condition is written in ABAP and has a special reserved name row to refer to the current row in the tree

This example is grouped by 1 field

image

Result

image

 More about directives level, top, show_if, hide_if