Use TAL: defined variable in javascript

I am creating a page template for a plone based website. I defined some variables using the template attribute language:

<tal:macro metal:define-macro="sample" tal:define="var python: here.getThisVar();">

Now I would like to use varin the extern javascript file that I invoke by clicking the button inside my template. How can I pass my variable so that I can work with it in my javascript file?

+4
source share
3 answers

In the template, define the javascript variable by writing it using TAL, for example:

<script type="text/javascript" tal:content="string:var MY_VAR=${view/myVar};"></script>

Now MY_VAR should be available within your external js if you call it after the line above ...

+8
source

: HTML HTML 5 data::

<div id="myVar" tal:attributes="data-myVar python:here.getThisVar();">

, JAvaScript/jQuery::

$('#myVar').data('myVar');
+4

There are many ways to do this. All include creating Javascript code as if it were text, and then returning the result to be inserted into the page or rendered as a JS resource in the javascript registry.

If you need a reliable example that includes provisions for text forwarding and works with the JS resource registry, see the way Plone does it himself: https://github.com/plone/Products.CMFPlone/blob/4.2.7/ Products / CMFPlone / browser / jsvariables.py

+3
source

All Articles