<FLEXY:TOJAVASCRIPT JSVAR="PHPVAR"> ...

<FLEXY:TOJAVASCRIPT JSVAR="PHPVAR"> ... – Provides a simple way to pass data from PHP to Javascript

Synopsis

Usage ( <flexy:tojavascript JSVAR="PHPVAR" ...> )

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

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>
Use a variable in the name for a flexy form element (Previous) HTML_Template_IT (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: willbickerstaff@mailshack.com
Unfortunately this page doesn't address the "WTF" effect of poor documentation. I'd help if I had the vaguest idea "WTF" this page is going on about
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.