[Zope] RE: Dynamically generating JavaScript

Duncan Booth duncan.booth at suttoncourtenay.org.uk
Mon Feb 14 10:03:35 EST 2005


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.



More information about the Zope mailing list