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.
.. py:function:: model.RenderTemplate(template, *data)
:return: The rendered template as a string.
:rtype: str
:param str template: The template to be rendered.
:param str data: \*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 = '''
Count | Member Status |
{{#each membertypes}}
{{Fmt cnt 'N0'}} | {{Description}} |
{{/each}}
'''
print model.RenderTemplate(t)
HandleBars Helpers
------------------
Besides the standard handlebars helpers, these are added for TouchPoint.
.. py:function:: {{ServerLink}}
:return: The first part of the URL to your TouchPoint database.
:rtype: str
For example::
print model.RenderTemplate("{{ServerLink}}/Person2/0")
results in:
https://bellevue.tpsdb.com/Person2/0
on our church here at Bellevue Baptist.
.. py:function:: {{FmtZip arg}}
:return: The formatted zip code with the plus 4 separated by a hyphen.
:rtype: str
:param str arg: The zip code to be formatted.
For example::
Data.zip = '380184435'
print model.RenderTemplate("{{FmtZip zip}}")
Results in:
38018-4435
.. py:function:: {{IfEqual arg1 arg2}}
:return: The first part of the URL to your TouchPoint database.
:rtype: bool
:param str arg1: One of the two values to compare.
:param str arg2: 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
.. py:function:: {{IfNotEqual arg1 arg2}}
:param str arg1: One of the two values to compare.
:param str arg2: 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.
.. py:function:: {{IfGT arg1 arg2}} text {{/IfGT}}
{{IfLT arg1 arg2}} text {{/IfLT}}
{{IfGE arg1 arg2}} text {{/IfGE}}
{{IfLE arg1 arg2}} text {{/IfLE}}
:param str arg1: First values to compare
:param str arg2: 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}}``
.. py:function:: {{GetToken str n *ntoks *sep}}
:return: The string token specified by `n` of the `str` argument
:rtype: str
:param str str: The string value to split into two tokens.
:param int n: The token number to get, either 0 or 1
:param int ntoks: \*Optional, defaults to 2
:param str sep: \*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
.. py:function:: {{FmtDate date}}
:return: The formatted date
:rtype: str
:param 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.
.. py:function:: {{FmtMoney amt}}
:return: The formatted amount
:rtype: str
:param value: the amount value to be formatted
Formats a value depending on culture as *$1,234.56*
.. py:function:: {{FmtPhone number}}
:return: The formatted phone number
:rtype: str
:param value: the string number to be formatted
Using ``{{FormatPhone '9015551212` 'C'}}`` returns ``C 901-555-1212``.
You can use ``Cell`` or abbreviate as ``C``.
.. py:function:: {{Fmt value format}}
:return: The formated value as specified by the `format` arg.
:rtype: str
:param * value: The value of any type (str, int, number) to be formatted.
:param str format: 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.