How to make a function available throughout all templates?

686 views
Skip to first unread message

Peteris Krumins

unread,
Apr 4, 2010, 1:08:29 PM4/4/10
to Mako Templates for Python
Hello,

I am curious what is the best way to make a function available
throughout all templates?

Here is an example. I wish to add a function `plural` that takes a
number, the singular form and the plural form of a word and returns
either singular or plural.

Then instead of writing ${'singular' if count==1 else 'plural'} I can
write ${plural(count, 'singular', 'plural')}, which makes it much more
readable.

How do I do that?


Sincerely,
P.Krumins

João Pinto

unread,
Apr 4, 2010, 2:24:14 PM4/4/10
to mako-d...@googlegroups.com
Hello,
I am doing it by introducing a render wrapper function which injects the internal functions into the template arguments.
It's something like:

def render(template_name, **kwargs):
    global _template_lookup
    mytemplate = _template_lookup.get_template(template_name)
    kwargs["myfunc"] =  myfunc
    return mytemplate.render(**kwargs)

Best regards,

2010/4/4 Peteris Krumins <peteris...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
To post to this group, send email to mako-d...@googlegroups.com.
To unsubscribe from this group, send email to mako-discuss...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mako-discuss?hl=en.




--
João Pinto
IRC: joaopinto @ irc.freenode.net
Jabber ID: lamego...@gmail.com
GetDeb Team Leader - http://www.getdeb.net

Deron Meranda

unread,
Apr 4, 2010, 5:02:36 PM4/4/10
to mako-d...@googlegroups.com
On Sun, Apr 4, 2010 at 2:24 PM, João Pinto <lamego...@gmail.com> wrote:
> Hello,
> I am doing it by introducing a render wrapper function which injects the
> internal functions into the template arguments.
> It's something like:
>
> def render(template_name, **kwargs):
>     global _template_lookup
>     mytemplate = _template_lookup.get_template(template_name)
>     kwargs["myfunc"] =  myfunc
>     return mytemplate.render(**kwargs)


That's one way--essentially, anything you put into the template
'context' object is available in all templates.


You can also put it (along with perhaps other "helper" type functions)
into it's own template and import them through a namespace...

For example, make a template file "/helpers.mako":

<%def name="plural(cnt,sing,plur=None)"><%
try:
n = int(cnt)
except ValueError:
word = sing
else:
if n == 1:
word = sing
elif plur:
word = plur
elif word.endswith('s'):
word = sing + 'es'
else:
word = sing + 's'
%>${ word }</%def>

and then where ever you want to use it, import it via a namespace
at the top of the template:

<%namespace file="/helpers.mako" name="helpers"/>

and then call it using either:

<%helpers:plural cnt="${n}" sing="foot" plur="feet"/>

or

${ helpers.plural(n, "dragon") }


And of course, another way is to put it into a plain old Python module that
is on your python path, then just import it

<%! import helpers %>

${ helpers.plural(n, "person", "people") }

--
Deron Meranda
http://deron.meranda.us/

Reply all
Reply to author
Forward
0 new messages