Handlebars Templates

We are using this implementation of the handlebars template library https://github.com/rexm/Handlebars.Net

This allows us to templates to write a report template and use it in Python and bind Data to the report with the use of handlebars. Example of a Python script utilizing a handlebars template:

Data.now = model.DateTime
Data.user = model.UserName
template = 'Hi {{user}}, the date and time is {{now}}.'
print model.RenderTemplate(template)

Note that both now and user are created as dynamic attributes on the built-in Data object. Then they are used in the template as {{user}} and {{now}} and the result on the screen might be:

Hi davcar, the date and time is 2/20/2016 10:44 AM.

model.RenderTemplate(template, *data)
Returns:

The rendered template as a string.

Return type:

str

Parameters:
  • template (str) – The template to be rendered.

  • data (str) – *Optional. The Data object is the default. The data containing the attributes to be evaluated for the template.

Example 2:

sql = '''
    select cnt = Count(*), Description
    from lookup.MemberStatus mt
    join People p on p.MemberStatusId = mt.Id
    group by mt.Description
'''
Data.membertypes = q.QuerySql(sql)
t = '''
    <table class="table notwide centered">
    <tr><th class="right">Count</th><th>Member Status</th></tr>
    {{#each membertypes}}
        <tr><td class="right">{{Fmt cnt 'N0'}}</td><td>{{Description}}</td></tr>
    {{/each}}
    </table>
'''
print model.RenderTemplate(t)

HandleBars Helpers

Besides the standard handlebars helpers, these are added for TouchPoint.

{{Calc arg1 arg2}}
Returns:

The calculated difference between arg1 and arg2, formatted as currency

Return type:

str

Parameters:
  • arg1 (number) – First value

  • arg2 (number) – Second value

Calculates arg1 - arg2 and formats the result as currency.

Example:

Data.total = 100.00
Data.paid = 75.50
template = "Amount due: {{Calc total paid}}"
print model.RenderTemplate(template)

Results in:

Amount due: $24.50

{{Fmt value format}}
Returns:

The formated value as specified by the format arg.

Return type:

str

Parameters:
  • value (*) – The value of any type (str, int, number) to be formatted.

  • format (str) – The format to use in formatting the value.

This function is useful for formatting currency or a number with commas, or a date. The format arg is the standard format string for .Net. See http://stringformat.azurewebsites.net.

Examples:

Data.now = model.DateTime
Data.avg = 123456.78 / 7
template = "Daily average was {{Fmt avg 'C'}} as of {{Fmt now 'MMM d, yyyy'}}."
print model.RenderTemplate(template)

Results in:

Daily average was $17,636.68 as of Feb 20, 2016.

{{FmtDate date}}
Returns:

The formatted date

Return type:

str

Parameters:

value – the date to be formatted

Formats a date depending on culture as ‘M/d/yyyy’. If the date argument is passed in as a string, it will be parsed into a date if possible.

{{FmtMDY date}}
Returns:

The formatted date in month/day/year format

Return type:

str

Parameters:

value – the date to be formatted

Formats a date in short date format (M/d/yyyy). If the date argument is passed in as a string, it will be parsed into a date if possible.

{{FmtMoney amt}}
Returns:

The formatted amount

Return type:

str

Parameters:

value – the amount value to be formatted

Formats a value depending on culture as $1,234.56

{{FmtNumber number}}
Returns:

The formatted number with appropriate suffix (K, M)

Return type:

str

Parameters:

value – the number to be formatted

Formats a number with appropriate suffixes: - Numbers >= 100M: formatted as “#,1M” (e.g., 123,456,789 becomes “123.5M”) - Numbers >= 1M: formatted as “0.#M” (e.g., 1,234,567 becomes “1.2M”) - Numbers >= 100K: formatted as “0.#K” with no decimal (e.g., 123,456 becomes “123K”) - Numbers >= 1K: formatted as “0.#K” (e.g., 1,234 becomes “1.2K”) - Numbers < 1K: rounded to 1 decimal place

{{FmtPhone number}}
Returns:

The formatted phone number

Return type:

str

Parameters:

value – the string number to be formatted

Using {{FormatPhone '9015551212` 'C'}} returns C 901-555-1212. You can use Cell or abbreviate as C.

{{FmtZip arg}}
Returns:

The formatted zip code with the plus 4 separated by a hyphen.

Return type:

str

Parameters:

arg (str) – The zip code to be formatted.

For example:

Data.zip = '380184435'
print model.RenderTemplate("{{FmtZip zip}}")

Results in:

38018-4435

{{GetToken str n *ntoks *sep}}
Returns:

The string token specified by n of the str argument

Return type:

str

Parameters:
  • str (str) – The string value to split into two tokens.

  • n (int) – The token number to get, either 0 or 1

  • ntoks (int) – *Optional, defaults to 2

  • sep (str) – *Optional, defaults to ‘ ‘ (a space)

This helper has a special purpose and only works a string of two tokens separated by a space.

For example:

Data.SpouseName = 'Karen Worrell'
print model.RenderTemplate("{{GetToken SpouseName 0}}")

Results in:

Karen

And:

Data.status = 'Member Status: Previous'
print model.RenderTemplate("{{GetToken status 1 2 ':'}}")

Results in:

Previous

{{IfCond arg1 operator arg2}}
Parameters:
  • arg1 (*) – First value to compare

  • operator (str) – Comparison operator (==, !=, <, >, >=, <=, &&, ||)

  • arg2 (*) – Second value to compare

Conditional block helper that supports various comparison operators: - == (equal) - != (not equal) - < (less than) - > (greater than) - >= (greater than or equal) - <= (less than or equal) - && (logical AND) - || (logical OR)

Example:

Data.age = 25
template = '''
    {{IfCond age ">" 21}}
        Can purchase alcohol.
    {{else}}
        Cannot purchase alcohol.
    {{/IfCond}}
'''
print model.RenderTemplate(template)

Results in:

Can purchase alcohol.

{{IfEqual arg1 arg2}}
Returns:

The first part of the URL to your TouchPoint database.

Return type:

bool

Parameters:
  • arg1 (str) – One of the two values to compare.

  • arg2 (str) – The other of the two values to compare.

The IfEqual helper is a block type of helper. This means that it does not return a value itself but rather controls which part of the template is executed next. It also means that you must close the helper with a {{/IfEqual}}.

And optionally, you can have two blocks separated by an {{else}} which means you can have two blocks where one of them is processed based on the equality of the two arguments.

For example:

Data.sum = 500 + 10.32
Data.total = 510.32
template = '''
    They are
    {{IfEqual sum total}}
        equal
    {{else}}
        not equal
    {{/IfEqual}}
'''
print model.RenderTemplate(template)

Results in:

They are equal

{{IfGT arg1 arg2}} text {{/IfGT}}
{{IfLT arg1 arg2}} text {{/IfLT}}
{{IfGE arg1 arg2}} text {{/IfGE}}
{{IfLE arg1 arg2}} text {{/IfLE}}
Parameters:
  • arg1 (str) – First values to compare

  • arg2 (str) – Second value to compare

  • GT Greater than If arg1 > arg2 then the block of text is rendered

  • LT Less than

  • GE Greater than or equal

  • LE Less than or equal

{{/IfGT}} where GT is the type of comparison, closes the block.

Works similarly to IfEqual with {{else}}

{{IfNotEqual arg1 arg2}}
Parameters:
  • arg1 (str) – One of the two values to compare.

  • arg2 (str) – The other of the two values to compare.

This works the same way as the previous IfEqual helper. But the first block is process if the two arguments are not equal. You would likely not need the {{else}} block for this helper. This is because the IfEqual helper will work for that use case. But it the IfNotEqual helper is useful for when you would only want to use the {{else}} block of the IfEqual helper and that would be awkward looking code with the first block being empty.

For example:

Data.sum = 500 + 10.32
Data.total = 510.31
template = '''
    {{IfNotEqual sum total}}
        Warning, the sum of {{sum}}
        does not match the total of {{total}}.
    {{/IfNotEqual}}
'''
print model.RenderTemplate(template)

Results in:

Warning, the sum of 510.32 does not match the total of 510.31.

{{ServerLink}}
Returns:

The first part of the URL to your TouchPoint database.

Return type:

str

For example:

print model.RenderTemplate("{{ServerLink}}/Person2/0")

results in:

on our church here at Bellevue Baptist.



Latest Update

3/28/2025