RE: [Zope] Dynamically generating JavaScript
When I've had to include dynamic javascript I write a python method that prints the whole script block and then call that from a tal:block. <tal:block tal:replace="structure python: here.myscript_js()" /> myscript_js would look like this print """<script> var1 = 1; var2 = 2; var3 = 3; </script>""" return printed Andy
-----Original Message----- From: zope-bounces@zope.org [mailto:zope-bounces@zope.org] On Behalf Of Hong Yaun Sent: Monday, February 14, 2005 8:31 AM To: 'zope@zope.org' Subject: [Zope] Dynamically generating JavaScript
I have a page template within with I would like to embed some dynamically generated JavaScript. Say the following sample snippet:
<script> var1 = 1; var2 = 2; var3 = 3; </script>
The 'obvious' approach I came up with is the follow zpt code:
<script> <tal:block tal:repeat="i python:range(1,4,1)"> var<span tal:replace="i"/> = <span tal:replace="i"/> </tal:block> </script>
However, this code doesn't even compile correctly, with the error:
TAL.HTMLTALParser.NestingError: Open tag <script> does not match close tag </tal:block>
If instead I replace the <script> and </script> tags with things like <html> and </html>, the code compiles. Obviously, I am missing a very important point about the way zpt works, but I can't find what. Maybe it is not suitable for this kind of task and I shall use DTML or PythonScript instead?
Any hint would be appreciated.
Best Regards Hong Yuan _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Andy Yates wrote:
When I've had to include dynamic javascript I write a python method that prints the whole script block and then call that from a tal:block.
<tal:block tal:replace="structure python: here.myscript_js()" />
myscript_js would look like this
print """<script> var1 = 1; var2 = 2; var3 = 3; </script>"""
return printed
Wouldn't it make more sense to do: <script type="text/javascript" tal:content="here/myscript_js"></script> and just have myscript_js print the Javascript without the enclosing script tags? for i in range(1,4): print "var%d = %d" % (i, i) return printed Or even better, if your script contents don't depend on values from the current request use: <script type="text/javascript" src="myscript_js"></script> which keeps the Javascript entirely outside the html so you don't have to worry about embedded angle brackets.
participants (2)
-
Andy Yates -
Duncan Booth