Problem with undefined global vars

Skip Montanaro <[email protected]> Wed, 9 Jul 2014 10:14:50 -0500
Newsgroups gmane.comp.python.cheetah
Message-ID <CANc-5Uwt0GSnWA4=GY5R9gtZCVSdRKTxnyRzHo9r=ZvtnVYEbw@mail.gmail.com>
--===============0953553243891270471==
Content-Type: multipart/alternative; boundary=20cf304275a86a93e704fdc42b9a

--20cf304275a86a93e704fdc42b9a
Content-Type: text/plain; charset=UTF-8

I'm trying to simplify some overly complex Python+Excel+Cheetah
template code in preparation for some more substantial changes. I'm
confused by a NameError exception I'm receiving. I define my template
like so:

trd_template = Template.compile(file=template_file, cacheDirForModuleFiles=".")

then call/expand it like so:

tmpl = trd_template(searchList={"params": p})
output_file.write(str(tmpl))

where p is a dictionary of parameters I've collected from an Excel
spreadsheet.

When I run my config file generation program, Cheetah complains with
this traceback:

Traceback (most recent call last):
  File "scripts/generate_config_xml.py", line 282, in <module>
    sys.exit(main(sys.argv[1:]))
  File "scripts/generate_config_xml.py", line 224, in main
    output_file.write(str(tmpl))
  File "/opt/local/lib/python2.7/site-packages/Cheetah/Template.py",
line 1005, in __str__
    rc = getattr(self, mainMethName)()
  File "configs_trd_tmpl.py", line 97, in respond
NameError: global name 'params' is not defined

I've trimmed my template down to this:

${sorted(params["Accounts"].values())}

#for $c in sorted(params["TradableSymbols"].split(","))
<key>symbol</key>                 <string>${c}</string>
#end for

#for $acct in sorted(params["Accounts"].values())
<key>name</key>              <string>${acct}</string>
#end for

If I ${...}-wrap the uses of params the compilation step fails (with a
nice Cheetah traceback). As is, it complains about the missing global
name. What am I missing?

Thx,

Skip

--20cf304275a86a93e704fdc42b9a
Content-Type: text/html; charset=UTF-8

<div dir="ltr"><pre>I&#39;m trying to simplify some overly complex Python+Excel+Cheetah
template code in preparation for some more substantial changes. I&#39;m
confused by a NameError exception I&#39;m receiving. I define my template
like so:

trd_template = Template.compile(file=template_file, cacheDirForModuleFiles=&quot;.&quot;)

then call/expand it like so:

tmpl = trd_template(searchList={&quot;params&quot;: p})
output_file.write(str(tmpl))

where p is a dictionary of parameters I&#39;ve collected from an Excel
spreadsheet.

When I run my config file generation program, Cheetah complains with
this traceback:

Traceback (most recent call last):
  File &quot;scripts/generate_config_xml.py&quot;, line 282, in &lt;module&gt;
    sys.exit(main(sys.argv[1:]))
  File &quot;scripts/generate_config_xml.py&quot;, line 224, in main
    output_file.write(str(tmpl))
  File &quot;/opt/local/lib/python2.7/site-packages/Cheetah/Template.py&quot;, line 1005, in __str__
    rc = getattr(self, mainMethName)()
  File &quot;configs_trd_tmpl.py&quot;, line 97, in respond
NameError: global name &#39;params&#39; is not defined

I&#39;ve trimmed my template down to this:

${sorted(params[&quot;Accounts&quot;].values())}

#for $c in sorted(params[&quot;TradableSymbols&quot;].split(&quot;,&quot;))
&lt;key&gt;symbol&lt;/key&gt;                 &lt;string&gt;${c}&lt;/string&gt;
#end for

#for $acct in sorted(params[&quot;Accounts&quot;].values())
&lt;key&gt;name&lt;/key&gt;              &lt;string&gt;${acct}&lt;/string&gt;
#end for

If I ${...}-wrap the uses of params the compilation step fails (with a
nice Cheetah traceback). As is, it complains about the missing global
name. What am I missing?

Thx,

Skip


</pre></div>

--20cf304275a86a93e704fdc42b9a--


--===============0953553243891270471==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
--===============0953553243891270471==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Cheetahtemplate-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss

--===============0953553243891270471==--