Usage (
<flexy:tojavascript JSVAR="PHPVAR" ...>
)
To reduce the "WTF" effect of magically having javascript code that looks like flexy tags completely broken by the template engine, Flexy deliberately turns the parser OFF when dealing with script contents (see Configuration Options for details of PHP in script tags)
As a result of this, it is not possible to put any flexy tags within blocks of javascript code. the flexy:tojavascript tag solves this in a way that enables javascript to be tested seperatly from the application, and also serves to encourage better coding practices. (eg. seperating your code with distinct lines of communication)
This feature depends on PEAR's HTML_Javascript Library
A template with javascript and flexy:tojavasscript
<?php
<html><head>
<title>Example</title>
<flexy:toJavascript
flexy:prefix="test_abc_"
abcg="xyz"
abcd="xyz"
srcXxx="xyz"
>
<!--
We can use the inner contents for testing the template in a browser.
The template will remove these contents when it is compiled.
-->
<script type="text/javascript">
var test_abc_abcg = '123';
var test_abc_abcd = '123';
var test_abc_srcXxx = '123';
</script>
</flexy:toJavascript>
<flexy:toJavascript abcg="xyz">
<script type="text/javascript">
var xyz = '123';
</script>
</flexy:toJavascript>
<body>
<p>Example of flexy:toJavascript with default values.</p>
</body></html>
?>
compiled template
<html><head>
<title>Example</title>
<?php require_once 'HTML/Javascript/Convert.php';?>
<script type='text/javascript'>
<?php $__tmp = HTML_Javascript_Convert::convertVar($t->xyz,'test_abc_abcg',true);
echo (is_a($__tmp,"PEAR_Error")) ? ("<pre>".print_r($__tmp,true)."</pre>") : $__tmp;?>
<?php $__tmp = HTML_Javascript_Convert::convertVar($t->xyz,'test_abc_abcd',true);
echo (is_a($__tmp,"PEAR_Error")) ? ("<pre>".print_r($__tmp,true)."</pre>") : $__tmp;?>
<?php $__tmp = HTML_Javascript_Convert::convertVar($t->xyz,'test_abc_srcXxx',true);
echo (is_a($__tmp,"PEAR_Error")) ? ("<pre>".print_r($__tmp,true)."</pre>") : $__tmp;?>
</script>
?php require_once 'HTML/Javascript/Convert.php';?>
<script type='text/javascript'>
<?php $__tmp = HTML_Javascript_Convert::convertVar($t->xyz,'abcg',true);
echo (is_a($__tmp,"PEAR_Error")) ? ("<pre>".print_r($__tmp,true)."</pre>") : $__tmp;?>
</script>
<body>
<p>Example of flexy:toJavascript with default values.</p>
</body></html>
output from the Template (with no values set)
<html><head> <title>Example</title> <script type='text/javascript'> test_abc_abcg = null; test_abc_abcd = null; test_abc_srcXxx = null; </script> <script type='text/javascript'> abcg = null; </script> <body> <p>Example of flexy:toJavascript with default values.</p> </body></html>