Accessing Zope variables with Javascript

ZPT totally ignores the contents of <script> tags, which makes it kind of a hassle to use values from the server in your Javascript. Short of writing a whole integration framework, the best way I've found to pull variables from page templates into Javascript is like this:

<tal:block tal:define="foo accessor/for/myvar">
<script tal:content="string:
var foo='${foo}';
"></script>
</tal:block>


I generally have one of these up at the top of the template, storing portal_url and whatever else I might need in a namespaced array. You can run into problems with nested quotes, but if you don't have the time to write more complex back-end methods, it's a handy quick-and-dirty hack.

0 comments

Post a Comment