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.

{{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.

{{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

{{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

{{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.

{{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}}

{{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

{{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.

{{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

{{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.

{{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.