<FLEXY:TOJAVASCRIPT JSVAR="PHPVAR"> ... -- Provides a simple way to pass data from PHP to Javascript
Description
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
Example
Example 47-1. 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>
?>
|
|
Example 47-2. compiled template
<?php
<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>
?>
|
|
Example 47-3. 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> |
|
|
flexy:nameuses="variable" (Previous)
|
(Next) HTML_Template_IT
|
|
|
Download Documentation
|
Last updated: Sun, 24 Aug 2008 |
|
Do you think that something on this page is wrong? Please file a bug report or add a note.
|
| User Notes: |
Note by: nomatter@php.org
Are you people serious? This is one of the worst pieces of documentation I've seen, to date.
What are the attributes for this flexy tag? How do you populate a javascript variable with a PHP one? What scope issues exist?
Can somebody elaborate how this actually works? The examples given are useless.
|
|