RE: [SMARTY] Javascript Question

From: Date: Wed, 30 Jun 2004 21:05:41 +0000
Subject: RE: [SMARTY] Javascript Question
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-11511@lists.php.net to get a copy of this message
Really challenging for PHP in general, but even more so for Smarty, due to the following: - Smarty doesn't have access to your client-side .js - PHP doesn't have direct access to screen.x or screen.y By design, you want to avoid placing PHP code in your .tpl files wherever possible. The following might help to illustrate, but I'm not sure if it would work (totally untested): in your php file: ------------------------------- if(isset($_COOKIE["screen_res"])){ $smarty->assign('screen_res_cookie_is_set', 'Y'); $smarty->assign('screen_res_cookie', $_COOKIE["screen_res"]); }else{ $smarty->assign('screen_res_cookie_is_set', 'N'); } Then in your .tpl file: ------------------------------- {if $screen_res_cookie_is_set eq 'N'} {literal} <script language="javascript"> function write_screen_res_cookie() { var today = new Date(); var exp_date = new Date("January 1, 2099"); var cookie_expire_date = exp_date.toGMTString(); var cookie = "screen_res="+ screen.width +"x"+ screen.height; var screen_res_cookie = cookie + ";expires=" + cookie_expire_date; document.cookie=screen_res_cookie } write_screen_res_cookie(); </script> {/literal} {else} {literal} <script language="javascript"> document.cookie={/literal}{$screen_res_cookie}{literal} </script> {/literal} {/if} Note the use of Smarty literal tags to surround JS code in a .tpl, and that if you place the js function right in the page without the external js include, it will be easier to deal with since you can write to it from PHP via Smarty this way. HTH Geoffrey

« previous php.smarty.general (#11511) next »