I can tell you why it isn't working, but I do not have a clean solution at this point. Your template as given compiles into this Python code:
# -*- encoding:utf-8 -*-
from mako import runtime, filters, cache
UNDEFINED = runtime.UNDEFINED
__M_dict_builtin = dict
__M_locals_builtin = locals
_magic_number = 5
_modified_time = 1285968547.0498569
_template_filename='<snip>'
_template_uri='<snip>'
_template_cache=cache.Cache(__name__, _modified_time)
_source_encoding='utf-8'
_exports = ['function']
# SOURCE LINE 1
lookup = { 'key': function }
def render_body(context,**pageargs):
context.caller_stack._push_frame()
try:
__M_locals = __M_dict_builtin(pageargs=pageargs)
__M_writer = context.writer()
# SOURCE LINE 3
__M_writer(u'\n\n')
# SOURCE LINE 7
__M_writer(u'\n')
return ''
finally:
context.caller_stack._pop_frame()
def render_function(context):
context.caller_stack._push_frame()
try:
__M_writer = context.writer()
# SOURCE LINE 5
__M_writer(u'\n Output\n')
return ''
finally:
context.caller_stack._pop_frame()
As you can see your function
has actually been defined as render_function
. The Mako docs specify how to call defs from outside a template, but they do not indicate how to do this properly at runtime. The code that I have linked too simply does a lookup for "render_%s" % name
(see mako.template, line 217), so you might consider just doing that.