[PHPTAL] JavaScript arrays using PHPTAL

Kornel Lesinski kornel at aardvarkmedia.co.uk
Mon Jan 21 12:04:06 CET 2008


On Mon, 21 Jan 2008 10:43:21 -0000, Anton Andrievskij <x.meglio at gmail.com>  
wrote:

> I have to fill JavaScript array using PHPTAL attributes.
>
> Whether I can use some <span ...tal-attributes here..></span> inside
> <script></script> construction?

I'm afraid that might be difficult with pure PHPTAL. To avoid  
compatibility problems with non-XHTML browsers (like Internet Explorer),  
you should use <![CDATA[ section in scripts, but by definition that also  
stops PHPTAL tags from being executed.

You may need to use plain PHP there. $ctx object allows you to access  
PHPTAL's variables:

<script type="text/javascript">/*<![CDATA[*/
   var items = <?php echo json_encode($ctx->items); ?>;
/*]]>*/</script>

This will create JavaScript variable items from PHPTAL's variable items  
(json_encode() automatically handles arrays, objects, etc.)

-- 
regards, Kornel




More information about the PHPTAL mailing list