New to mako, didn't find in the docs...
One of the stuff I'd like to do with that is:
<%mytag n=12>
blabla ${x}
</%mytag>
which works like the following:
if the X[n]
(here, X[12]
) variable is an int or string then render the content with x = X[12]
=> With X[12] = 23
, it'd render: blabla 23
else if X[n]
is a list, then do a for
loop, repeating the content for each value of x in X[n]
=> With X[12] = [1, 2, 'bla']
, it'd render:
blabla 1
blabla 2
blabla bla
Please try to answer the main question (is it possible to do custom tags?) if possible, before giving advice on how to do what I want to do, thanks :)
The "namespaces" doc seem to describe what you are after, specifically:
Namespaces can also import regular Python functions from modules. These callables need to take at least one argument,
context
, an instance ofContext
. A module file some/module.py might contain the callable:def my_tag(context): context.write("hello world") return ''
A template can use this module via:
<%namespace name="hw" module="some.module"/> ${hw.my_tag()}
..and:
The "custom tag" format is intended mainly for namespace functions which recognize body content, which in Mako is known as a “def with embedded content”:
<%mynamespace:somefunction arg1="some argument" args="x, y"> Some record: ${x}, ${y} </%mynamespace:somefunction>