|
| 1 | +--- |
| 2 | +outline: [2, 4] |
| 3 | +--- |
| 4 | +# XML Templating |
| 5 | + |
| 6 | +abap2UI5 sends views to the browser as XML strings. XML Templating lets you construct those strings dynamically in ABAP — branching on conditions, looping over data, and composing fragments — so that the view the browser receives is already fully resolved without any client-side template engine. |
| 7 | + |
| 8 | +#### How It Works |
| 9 | + |
| 10 | +Every abap2UI5 view is ultimately an XML string. You can build that string however you like: literal assignment, string concatenation, helper method calls, or a dedicated XML builder. The browser never sees template syntax — it only receives the final, rendered XML. |
| 11 | + |
| 12 | +```abap |
| 13 | +METHOD z2ui5_if_app~main. |
| 14 | + IF client->check_on_init( ). |
| 15 | + DATA(xml) = z2ui5_cl_xml_view=>factory( ). |
| 16 | + client->view_display( xml->stringify( ) ). |
| 17 | + ENDIF. |
| 18 | +ENDMETHOD. |
| 19 | +``` |
| 20 | + |
| 21 | +#### Conditional Rendering |
| 22 | + |
| 23 | +Use ABAP `IF` statements to include or exclude controls depending on runtime state: |
| 24 | + |
| 25 | +```abap |
| 26 | +DATA(xml) = z2ui5_cl_xml_view=>factory( ). |
| 27 | +DATA(page) = xml->page( 'Conditional View' ). |
| 28 | +
|
| 29 | +IF is_admin = abap_true. |
| 30 | + page->button( |
| 31 | + text = 'Admin Action' |
| 32 | + press = client->_event( 'ADMIN_ACTION' ) ). |
| 33 | +ENDIF. |
| 34 | +
|
| 35 | +page->text( 'Hello, ' && username ). |
| 36 | +client->view_display( xml->stringify( ) ). |
| 37 | +``` |
| 38 | + |
| 39 | +Only the controls added to the builder are serialized — nothing from the skipped branch appears in the XML. |
| 40 | + |
| 41 | +#### Repeating Controls |
| 42 | + |
| 43 | +Loop over an internal table to generate repeated controls: |
| 44 | + |
| 45 | +```abap |
| 46 | +DATA(list) = page->list( ). |
| 47 | +
|
| 48 | +LOOP AT items INTO DATA(item). |
| 49 | + list->item( |
| 50 | + title = item-title |
| 51 | + description = item-description ). |
| 52 | +ENDLOOP. |
| 53 | +``` |
| 54 | + |
| 55 | +Each iteration appends a child node. The resulting XML contains exactly as many `<Item>` elements as there were rows in `items`. |
| 56 | + |
| 57 | +#### Composing Fragments |
| 58 | + |
| 59 | +Break complex views into reusable helper methods that each return a subtree: |
| 60 | + |
| 61 | +```abap |
| 62 | +METHOD build_header. |
| 63 | + rv_header = io_page->toolbar( )->title( title ). |
| 64 | +ENDMETHOD. |
| 65 | +
|
| 66 | +METHOD build_table. |
| 67 | + DATA(tbl) = io_page->table( ). |
| 68 | + " ... add columns and rows |
| 69 | + rv_table = tbl. |
| 70 | +ENDMETHOD. |
| 71 | +``` |
| 72 | + |
| 73 | +Call the helpers in sequence inside `main` to assemble the final view. Because the builder accumulates children as regular ABAP object references, standard ABAP patterns (loops, conditions, method calls, function modules) all apply without restriction. |
| 74 | + |
| 75 | +#### Dynamic Column Sets |
| 76 | + |
| 77 | +A common pattern is selecting which columns to show based on user settings or authorization: |
| 78 | + |
| 79 | +```abap |
| 80 | +DATA(cols) = xml->table( )->columns( ). |
| 81 | +
|
| 82 | +IF show_price = abap_true. |
| 83 | + cols->column( )->text( 'Price' ). |
| 84 | +ENDIF. |
| 85 | +IF show_stock = abap_true. |
| 86 | + cols->column( )->text( 'Stock' ). |
| 87 | +ENDIF. |
| 88 | +``` |
| 89 | + |
| 90 | +The same loop that drives the column headers can drive the cell binding paths, keeping them in sync. |
| 91 | + |
| 92 | +#### Tips |
| 93 | + |
| 94 | +- Build the full view on every roundtrip. abap2UI5 is stateless by default, so the view is always reconstructed from the current model state — there is no diff or patch step. |
| 95 | +- Keep view-construction logic out of business-logic methods. A dedicated `build_view` method or a set of small fragment builders keeps `main` readable. |
| 96 | +- Prefer the fluent builder API (`z2ui5_cl_xml_view`) over raw string concatenation. The builder escapes special characters automatically and produces well-formed XML. |
| 97 | + |
| 98 | +See `Z2UI5_CL_DEMO_APP_032` and `Z2UI5_CL_DEMO_APP_033` for complete examples of conditional and dynamic views. |
0 commit comments