Template Printing Function Overview

The template printing function generates printing data by combining a template (Star Document Markup), which defines a print layout, and field data (JSON), which replaces the text in a specified area in the template with a desired character string.
Separating data used for printing into a template and field data allows users to use data variable for each printing easily and reuse the print layout.


Four Major Useful Features of Template Printing Function


1. Specifying field data in JSON format
You can import different data for each printing, such as a receipt or label, in JSON format from the outside by methods such as Web API and use it as printing data.

2. Reusing a print layout
By saving fixed printing content as a template, you can easily reuse a print layout created previously.
You can modify the layout just by editing the template.


3. Support for array field data
Once you specify keys that represent array elements of field data in the template, you can automatically print all the elements in the array.

4. Flexible number format specifications
You can perform detailed operations, such as number conversion between integers and decimals and number printing with the number of digits specified, in a C language-like format.

Implement Printing Data Creation Process Using Template Printing Function

The following image is a print sample using the template printing function.

To perform this printing using the template printing function, create a template using the procedure below (*) and prepare field data.
* The description of Star Document Markup tags irrelevant to the template creation process is omitted.



1 Basic usage
To print the value of the Store_name key ("Star Cafe"), specify a character string containing a replacing specifier.

${store_name}

2 Combination with markup tags
To print the value of the order_number key ("#2-007") and the value of the time key ("10/16 11:13PM") in a two-column layout, specify a character string containing replacing specifiers in the parameter of the [column] tag. Like the left parameter as shown in this example, normal texts and replacing specifiers can coexist.
In addition, specifying the vl (variable-left) option automatically reduces the number of characters of a character string that is replaced with the left parameter even if the number increases, preventing the disruption of the layout.
For details of the option, refer to Tag References.

[column: vl; left Order ${order_number}; right ${time}]

3 Field data of array elements
To print the field data of array elements, specify the template area to repeat using the [templateArray] tag. Specify the [templateArray: start] tag at the start of the repeat and the [templateArray: end] tag at the end of the repeat.
A character string containing replacing specifiers that specify the field data of array elements (item.list key values) written in the area enclosed within the [templateArray] tag is printed repeatedly as many times as the number of array elements.

[templateArray: start]
[column: vl; left ${item_list.quantity%-2d} ${item_list.name}; right ${item_list.unit_price%6.2lf}]
[templateArray: end]\   

4 Number format specification
To print the numeric-type subtotal key value (8.24) in a specified format, specify a character string containing a replacing specifier that specifies the number format.

[column: vl; left Subtotal; right ${subtotal%6.2lf}]

5 Combination with markup tags
To print the value of the transaction_id key ("0123456789"), specify a character string containing a replacing specifier in the data parameter of the [barcode] tag.

[barcode: type code39; data ${transaction_id}; height 15mm; module 0; hri]\

To convert the template and field data created in this section into printer commands, use the following library or tool.


Sample code

Field data: JSON

{
    "store_name" : "Star Cafe",
    "order_number" : "#2-007",
    "time" : "10/16 11:13PM",
    "sales_type" : "Walk-in",
    "server" : "Jane",
    "transaction_id" : "0123456789",
    "item_list" : [
        {
            "name" : "Vanilla Latte",
            "unit_price" : 4.99,
            "quantity" : 1
        },
        {
            "name" : "Chocolate Chip Cookie",
            "unit_price" : 3.25,
            "quantity" : 1
        }
    ],
    "subtotal" : 8.24,
    "tax" : 0.73,
    "total" : 8.97,
    "credit_card_number" : "VISA 0123",
    "approval_code" : "OK2443",
    "amount" : 8.97,
    "address" : "123 Star Road, City,\nState 12345",
    "tel" : "123-456-7890",
    "mail" : "info@star-m.jp",
    "url" : "star-m.jp"
}

Template: Star Document Markup

[align: center]\
 [font: a]\
 [image: url https://star-emea.com/wp-content/uploads/2015/01/logo.jpg; width 60%; min-width 48mm]\
 [magnify: width 2; height 2]
 ${store_name}\

 [plain]\
 [align: left]\
 [column: vl; left Order ${order_number}; right ${time}]
 [column: vl; left Sale for ${sales_type}; right Served by ${server}]\

 Transaction #${transaction_id}
 [fixedWidth: text ------------------------------------------------]\

 [templateArray: start]
 [column: vl; left ${item_list.quantity%-2d} ${item_list.name}; right ${item_list.unit_price%6.2lf}]
 [templateArray: end]\

 [fixedWidth: text ------------------------------------------------]\

 [column: vl; left Subtotal; right ${subtotal%6.2lf}]
 [column: vl; left Tax; right ${tax%6.2lf}]
 [bold: on]\
 [column: vl; left Total; right ${total%6.2lf}]\
 [bold: off]\

 [fixedWidth: text ------------------------------------------------]\

 [column: vl; left ${credit_card_number}; right ${total%6.2lf}]
 [column: vl; left Approval Code; right ${approval_code}]\

 [fixedWidth: text ------------------------------------------------]\

 [column: vl; left Amount; right ${amount%6.2lf}]
 [column: vl; left Total; right ${total%6.2lf}]\

 [fixedWidth: text ------------------------------------------------]\

 Signature\

 [align: center]\


 ------------------------

 ${address}
 ${tel}
 ${mail}
 ${url}\

 [fixedWidth: text ------------------------------------------------]\

 Powered by Star Micronics\

 [barcode: type code39; data ${transaction_id}; height 15mm; module 0; hri]\
 [cut: feed; partial]

Template and Field Data Specifications

Template

A template is a print layout defined by a combination of fixed printing data and replacing specifiers, created with the Star Document Markup.
You can define a field (replacement area) in the template, which is a print layout, using a replacing specifier. A value with the same key from field data in JSON format can be substituted for a replacing specifier.

Specify a replacing specifier in plain text. A replacing specifier can coexist with a normal text.

Date:${date} Time:${time}



Replacing specifier format

A key character string enclosed between ${ and }, like "${key-string}," is treated as a replacing specifier.
If there is a key that matches 'key-string' in the field data, the text in the "${key-string}" area is replaced with the key value.
If there is no matching key, printing is conducted with the "${key-string}" area ignored.


Key specifier

You can specify a key in the field data to identify the field data with which you want to replace.
Only ASCII characters are allowed in keys.


Case of a nested element

To specify a nested element, combine keys with a period (.).

Example of specifying a nested element

“${store.name}"

Field data

{
    "store" : {
        "name" : "Star Store",
        "address" : "123 Star Road, City, State 12345"
    }
}


Case of an array element

By specifying an array element as the key for use with the key specifier, you can perform replacements for a single field based on the number of elements in the array.

In this case, you also need to define the range of the template to be repeated for the number of array elements.
The [templateArray] tag, which specifies the repeat range when array field data exists, allows the range to be repeated as many times as the number of array elements.

In the example below, keys of child elements of an array element (character string type and numeric type) are specified using key specifiers, and the repetition of array elements is specified by the [templateArray] tag. As a result, the target repeat area enclosed within the [templateArray] tag is printed only four times, the number of array elements in the field data.

Template

[templateArray: start]
[column: left ${item_list.name}; right ${item_list.unit_price%6.2lf}
[underline: on]\
[column: left ; right x${item_list.quantity}]
[underline: off]\
[templateArray: end]
[cut: feed; partial]       

Field data

{
    "item_list" : [
        {
            "name" : "BLT",
            "unit_price" : 13.0,
            "quantity" : 1
        },
        {
            "name" : "Chicken Noodle",
            "unit_price" : 7.5,
            "quantity" : 1
        },
        {
            "name" : "Caesar salad",
            "unit_price" : 10.0,
            "quantity" : 2
        },
        {
            "name" : "Coffee",
            "unit_price" : 3.5,
            "quantity" : 2
        }
    ]
}

Print result



Number format specifier

If a field data value is a numeric type, you can specify the display format using a number format specifier. A number format specifier is a subset of a format specifier in printf function in C language. A number format specifier can be omitted. If omitted, the value is used as a character string without being converted.





Escape characters

When you want to print ${ signifying the start of a replacing specifier, add a ; to the end for escape (${;).

To include the following characters, which have special meanings for the template printing function, in a field data key, when using them in a key specifier, place a \\ before the characters for escape.

% . ; }


Template printing function-compatible tags

You can define a field using a replacing specifier in the following tags.




Field data

Field data is a printing data set different for each printing, written in JSON format.
The template printing function creates printing data by substituting the value of field data specified in a replacing specifier in a template.

Key specifications

Only ASCII characters are allowed in keys.

Value specifications

Which type of character string replaces the field is determined based on the field data value type as shown below.