Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat can I do to get the return value from a %def?
    primarykey
    data
    text
    <p>How do I get the return value from a %def?</p> <p>makoT1.py:</p> <pre><code>import mako from mako.template import Template from mako.lookup import TemplateLookup print "Hi world.", mytemplate = Template(filename='makoT1.mako') print mytemplate.render(), </code></pre> <p>makoT1.mako</p> <pre><code>&lt;% index = 0 %&gt; &lt;%def name="a_def(counter)"&gt; Pre: ${counter} &lt;% counter += 1 %&gt; Post: ${counter} &lt;% return counter %&gt; &lt;/%def&gt; index: ${index} ${a_def(index)} index: ${index} </code></pre> <p>dalem@QnD:~$ python makoT1.py</p> <pre><code>Hi world. index: 0 Pre: 0 Post: 1 1 index: 0 </code></pre> <p>Notice that the second "index," above is still 0? That's what I'm trying to fix. I'd like it to increment.</p> <p>Perhaps something like this for makoT1.mako:</p> <pre><code>&lt;% index = 0 %&gt; &lt;%def name="a_def(counter)"&gt; Pre: ${counter} &lt;% counter += 1 %&gt; Post: ${counter} &lt;% return counter %&gt; &lt;/%def&gt; index: ${index} &lt;% index = ${a_def(index)} %&gt; index: ${index} </code></pre> <p>But that gets me this error:</p> <pre><code>dalem@QnD:~$ python makoT1.py Hi world. Traceback (most recent call last): File "makoT1.py", line 5, in &lt;module&gt; mytemplate = Template(filename='makoT1.mako') File "/usr/local/lib/python2.7/dist-packages/mako/template.py", line 291, in __init__ module = self._compile_from_file(path, filename) File "/usr/local/lib/python2.7/dist-packages/mako/template.py", line 368, in _compile_from_file filename) File "/usr/local/lib/python2.7/dist-packages/mako/template.py", line 615, in _compile_text generate_magic_comment=template.disable_unicode) File "/usr/local/lib/python2.7/dist-packages/mako/template.py", line 597, in _compile node = lexer.parse() File "/usr/local/lib/python2.7/dist-packages/mako/lexer.py", line 241, in parse if self.match_python_block(): File "/usr/local/lib/python2.7/dist-packages/mako/lexer.py", line 376, in match_python_block match.group(1)=='!', lineno=line, pos=pos) File "/usr/local/lib/python2.7/dist-packages/mako/lexer.py", line 131, in append_node node = nodecls(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/mako/parsetree.py", line 139, in __init__ self.code = ast.PythonCode(text, **self.exception_kwargs) File "/usr/local/lib/python2.7/dist-packages/mako/ast.py", line 37, in __init__ expr = pyparser.parse(code.lstrip(), "exec", **exception_kwargs) File "/usr/local/lib/python2.7/dist-packages/mako/pyparser.py", line 60, in parse ), **exception_kwargs) mako.exceptions.SyntaxException: (SyntaxError) invalid syntax (&lt;unknown&gt;, line 1) (u'index = ${a_def(index)} \n') in file 'makoT1.mako' at line: 11 char: 1 </code></pre> <p>It seems odd to me that line 11 would get the SyntaxError when this works fine for line 11:</p> <pre><code>&lt;% index = 0 %&gt; </code></pre> <p>Shouldn't ${a_def(index)} return the integer from a_def(counter)?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload