For Programmers: Free Programming Magazines  


Home > Archive > PHP Smarty Templates > June 2004 > RE: [SMARTY] Javascript Question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author RE: [SMARTY] Javascript Question
Geoffrey Hoffman

2004-06-30, 9:01 pm

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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com