HTML Template in ABAP

sai kiran anagani 61187 unsplash 1

HTML Template in ABAP is a powerfull to tool to save template for document, SAP long texts and email in HTML format in SAP.

The SAP HTML Template contains more than the content with layout and data reference to generate dynamic Email and/or rich text.

These fields will be replaced by the corresponding value.

SAP HTML Template Tcode

First, you have to implement the HTML template. Use WYSWYG tools or even a notepad editor.
Check here how to set ABAP Synthax in Notepad++.

Once the HMTL file is ready, run the tcode SMW0 to enter the SAP Web Repository.

The SMW0 Tcode can be used to store HMTL Template as well as to upload Binary data ( such as Picture, PDF … )

for HTML files, select the first option : HTML templates for WebRFC applications.

SAP Web Repository - html template

HTML Template Uploading

  1. Click to Create a new HTML file in SAP Web Repository.
  2. Fill a name and a description
  3. Upload the created HTML

Here a simple of HTML Template for an email


HANDLING HTML TEMPLATE in ABAP / SAP
�
Hello;
�
This is a test for HTML Template in SAP
�
Your SAP UserId is !USERID!
The date is !DATE!
�

SAP Template Attributes

Once uploaded, the system generates some attributes for HTML such as:

  • fileextension .html
  • filename C:tempHTML_TEMPLATE.html
  • filesize 693
  • mimetype text/html
  • version 00001

You can add your own attributes ( to handle language for example )

Handling HTML Template in ABAP

SAP offers a function module to Merge two HTML.

The function Module WWW_HTML_MERGER can remplace the variables declared in the HTML template with their value.

The input:

  • TEMPLATE: the name of the Template in the SAP Web Repository
  • MERGE_TABLE: is the table containing the variables and their values:
    • name(30): The name of Variable : placeholder: ‘’
    • html like w3html occurs 100, ” HTML code to be inserted ( it is a table ! )
    • command: the action to take place when merging.
      • ‘?’-replace placeholderline(default)
      • ‘b’ insert before placeholder line
      • a’ insert after placeholder line
      • ‘r’ replace placeholder only

The output:

  • HTML_TABLE: type SWWW_T_HTML_TABLE and containing HTML line (255)

ABAP Sample How to handle HTML Template in SAP

Here an ABAP Tutorial sample code, to retrieve a template and exchange the variables with the data.

*&---------------------------------------------------------------------*
*& ABAP CODE : HOW TO HANDLE HTML TEMPLATE IN ABAP 
*& REMPLACE THE VARIABLES IN HTML WITH VALUE 
*&---------------------------------------------------------------------*

REPORT ZTEST_HTML_TEMPLATE.


" Declare the SWWW Type-Pool
TYPE-POOLS: swww.

" Data Declarations
DATA: lt_merge_table  TYPE  swww_t_merge_table.
DATA: lt_html_table   TYPE  swww_t_html_table.
DATA: ls_merge_table  LIKE LINE OF lt_merge_table.
DATA: ls_html_table   LIKE LINE OF lt_html_table.

DATA: lv_string       TYPE string.


" Selection Screen
PARAMETERS: p_temp TYPE swww_t_template_name DEFAULT 'ZZ_HTML_TEMPLATE_TEST'.
PARAMETERS: p_user TYPE syuname DEFAULT sy-uname.
PARAMETERS: p_date TYPE sydatum DEFAULT sy-datum.

START-OF-SELECTION.

  REFRESH lt_html_table.

  " Remplace USERID with its value
  CLEAR ls_merge_table.
  ls_merge_table-name    = '!USERID!'.
  ls_merge_table-command = 'R'.
  lv_string              = p_user.
  APPEND LV_string to ls_merge_table-html[].
  APPEND ls_merge_table TO lt_merge_table.

  " Remplace DATE with its value
  CLEAR ls_merge_table.
  ls_merge_table-name    = '!DATE!'.
  ls_merge_table-command = 'R'.
  lv_string              = p_date.
  APPEND LV_string to ls_merge_table-html[].
  APPEND ls_merge_table TO lt_merge_table.


  " Call the HTML MERGE function
  CALL FUNCTION 'WWW_HTML_MERGER'
    EXPORTING
      template    = p_temp
    IMPORTING
      html_table  = lt_html_table[]
    CHANGING
      merge_table = lt_merge_table[].


END-OF-SELECTION.

  LOOP AT lt_html_table INTO ls_html_table.
    WRITE / ls_html_table-line.
  ENDLOOP.

SAP Transporting HTML Template

When aving a HTML template in the SAP Web Repository, you will be asked to select a Transport. You can also save them locally in you are making some ABAP testing.

In Transport, the line corresponding to the Template:

  • Description: Web Reporting/Internet Transaction Server HTML Templates
  • Program Id: R3TR
  • Object Type: W3HT
  • Object Name: ZZ_HTML_TEMPLATE_TEST

If you want to even deeper into HTML Template, visit the official documentaire at help.sap.com