[Zope] Dynamically generating JavaScript

Andy Yates andy at nnu.com
Mon Feb 14 09:53:33 EST 2005


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 at zope.org [mailto:zope-bounces at zope.org] On Behalf
Of Hong Yaun
> Sent: Monday, February 14, 2005 8:31 AM
> To: 'zope at 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 at 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 )


More information about the Zope mailing list