08 Printouts - Handlebars
SRS supports the Handlebars templating engine. Template files should be stored in the /templates directory of the application.
Execute
To execute a Handlebars template, use the following API
HTTP /api/srs/{id}/{template-name}.hbs.html?id={id}
Creating a new template with basic helpers srs_administrator
HTTP /api/srs/{id}/_new.hbs.html
You can also use following extensions:
.hbs.html - html
.hbs.pdf - pdf
.hbs.csv - csv
.hbs.txt - txt
.hbs.xml - xml
.hbs.epp - epp
.hbs.svg - svg
Example data object
JSON {
da taset 0 : //object
{
"field0" : "some test <b>test</b>" ,
"field1" : "some test2" ,
"field2" : "some test3" ,
}
,
da taset 1 : [ //array
{
"field0" : "some text4" ,
"field1" : "some text5" ,
"field2" : "some text6" ,
}
],
da taset 2 : [] //empty array
}
Handlebars FAQ
Built in functions
Text Only {{#if (isArray this)}}
...
{{/if}}
Text Only {{#each items}}
{{add @index 1}}
{{/each}}
isEqual - allow subiteration
Text Only {{#each items}}
{{#each ../files}}
{{#if (isEqual this.commisionID ../commisionID)}}
<img src="data:image/png;base64,{{path}}" />
{{/if}}
{{/each}}
{{/each}}
Text Only {{get commandname "ret"}}
{{get srstranslation "ret"}}
return second property based on first
Iterate datasets
Text Only {{#each this}}
<li>{{@key}}</li>
{{/each}}
IF in Handlebars
Text Only {{#if dataset2}}
NOT VISIBLE - Array is empty
{{/if}}
{{#if dataset0}}
VISIBLE - object is not empty
{{/if}}
Text Only {{#unless isValid}}
{{/unless}}
Also keep in mind that you can insert an {{else}} in between an {{#if}} or {{#unless}}
raw text in Handlebars
Text Only raw: {{{dataset0.field0}}}
html-escaped: {{dataset0.field0}}
generate csv
separator: space replace with any characer eg. comma {{#unless @last}},{{/unless}}
Text Only {{#each api}}
{{#each this}}{{{this}}}{{#unless @last}} {{/unless}}{{/each}}
{{/each}}
Includes (images)
There are two ways to include images in the Handlebars template:
Direct reference to storage
You can include images from any storage location using the [[ and ]] tags.
HTML < img src = "data:image/png;base64,[[/api/core/storage/get/www/images/logo.png]]" />
Srs data object
You can include images from the SRS data object using the tbase64 column type.
srs:
XML <srs>
<itm model= "column" type= "tbase64" name= "testimg" />
<itm model= "command" opts= "dtsingle" name= "settings" >
select '33d52ff9-9914-923c-10bd-3251ea2db555.png' testimg
</itm>
</srs>
hbs:
HTML < img src = "data:image/png;base64,{{{settings.testimg}}}" />
Text Only {{! This comment will not show up in the output}}
Generic table horizontal
HTML < table >
<!--START Optional width -->
< colgroup >
< col style = "width:20mm" >
< col style = "width:40mm" >
< col style = "width:60mm" >
</ colgroup >
<!--END Optional width -->
< thead >
< tr >
{{#each dataset1.[0]}}
< th > {{@key}}</ th >
{{/each}}
</ tr >
</ thead >
< tbody >
{{#each dataset1}}
< tr >
{{#each this}}
< td > {{this}}</ td >
{{/each}}
</ tr >
{{/each}}
</ tbody >
</ table >
Generic table vertical
HTML < table >
< tbody >
{{#each dataset1}}
{{#each this}}
< tr >
< td > {{@key}}</ td >
< td >< b > {{this}}</ b ></ td >
</ tr >
{{/each}}
{{/each}}
</ tbody >
</ table >
Custom table
HTML < table >
< thead >
< tr >
{{#each dataset1.[0]}}
< th > {{@key}}</ th >
{{/each}}
</ tr >
</ thead >
< tbody >
{{#each dataset1}}
< tr >
{{#each this}}
< td > {{this}}</ td >
{{/each}}
</ tr >
{{/each}}
</ tbody >
</ table >
Template
Size of the printout can be changed in @page section
header element will be repeated on each page
footer will be repeated on each page
Experimental:
Experimental optional properties only in dev versions:
When single page then first, last , all
HTML <!doctype html>
< html >
< head >
< meta name = "jq:page-width" content = "210mm" > <!--DEFAULT-->
< meta name = "jq:page-height" content = "297mm" > <!--DEFAULT-->
< meta name = "jq:margin-top" content = "10mm" > <!--DEFAULT-->
< meta name = "jq:margin-bottom" content = "10mm" > <!--DEFAULT example value with background page: 30mm-->
< meta name = "jq:margin-left" content = "10mm" > <!--DEFAULT-->
< meta name = "jq:margin-right" content = "10mm" > <!--DEFAULT-->
< meta name = "jq:footer-right" content = "[page]/[topage]" > <!--DEFAULT -->
< meta name = "jq:footer-spacing" content = "0" > <!--DEFAULT example value with background page value: 19 -->
< meta name = "jq:page-template-first" content = "/api/core/storage/get/appui/assets/first.pdf" >
< meta name = "jq:page-template-last" content = "/api/core/storage/get/appui/assets/last.pdf" >
< meta name = "jq:page-template-all" content = "/api/core/storage/get/appui/assets/background.pdf" >
</ head >
...
Example template for background page: https://s3.jetquery.io/mk/2511/background.docx
HTML <!doctype html>
< html >
< head >
< title > TITLE</ title >
< meta charset = "utf-8" />
</ head >
< body >
< header > HEADER SECTION</ header >
CONTENT
< footer > FOOTER SECTION</ footer >
< style >
@ page {
width : 210mm ;
height : 297mm ;
margin-top : 20mm ;
margin-bottom : 20mm ;
}
/*EXAMPLE BACKGROUND IMAGE*/
body {
background : url ( 'data:image/png;base64,[[/api/core/storage/get/appui/assets/images/logo-background.png]]' );
background-repeat : no-repeat ;
background-position : 50 % 300 px ;
background-attachment : fixed ;
background-size : 100 % ;
min-height : 800 px ;
}
</ style >
</ body >
</ html >