RE: [SMARTY] BIG PROBLEM - {literal} and parsing vars in JS

From: Date: Sun, 19 May 2002 08:22:33 +0000
Subject: RE: [SMARTY] BIG PROBLEM - {literal} and parsing vars in JS
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-4305@lists.php.net to get a copy of this message
I don't know if this is too over the top, but I build a library of javascripts in a single php document (you could also do it using a database) and give each unique function a variable name. Then for each page that requires javascript I make a variable with the name of the executing php script to identify it, and feed it the javascript functions it requires (example below). Then I make a generic php script that gets the name of the currently executing script, and allocates the needed javascript accordingly. After this, all you have to do is call the javascript as a normal javascript include from the smarty template, and specify the script name as a querystring variable (example below). EXAMPLE: ____________________________________________________________________________ _____ jslibrary.php - this is the library of javascripts <?php //-------------------------------------------------------------------------- ----- /* Check String for system-invalid characters */ // Encapsulation of an entire javascript function inside an approriately name variable: $jscheckInvalid = <<<EOD function checkInvalid(strng, FieldName) { var error = ""; var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ if (strng.match(illegalChars)) { error = "The " + FieldName + " field contains invalid characters.\\nPlease try again.\\n\\n"; } return error; } EOD; // lots more encapsulated, generic javascripts follow, then the page- // specific scripts with associated variable names: $some_page = $jscheckInvalid; $some_page .= <<<EOD function doValidate(some_form) { var why = ""; if (some_form.some_field.value != "") { why += checkInvalid(some_form.some_field.value, "Some Field"); } else { why += "The Some Field field needs to be filled.\\nPlease try again.\\n\\n"; } if (why != "") { alert(why); return false; } else { return true; } } EOD; // Then the code to display the correct javascript functions for the calling page: $this_code = $$_GET['page']; echo $this_code; ?> ____________________________________________________________________________ _____ calljs.php - this can be skipped if you include the javascript library inside your web root. <?PHP include('../repository/lib/jslibrary.php') ?> ____________________________________________________________________________ ____ included once in a config file... <? // To get the currently executing script I use the following (probably cumbersome but anyway...) $parts_of_location = explode('/', $_SERVER['SCRIPT_FILENAME']); $script_location = (sizeof($parts_of_location) - 1); $current_location = $parts_of_location[$script_location]; $parts_of_page = explode('.', $current_location); $current_script = $parts_of_page[0]; // Then assign current_script to smarty: require_once($phys_include_path . 'smarty/Smarty.class.php'); $tpl = new Smarty; $tpl->assign('this_script', $current_script); ?> ____________________________________________________________________________ ____ then in a header template (eg. "header.tpl") <html> <title>{$title}</title> <script language="Javascript" src="{$site_path}calljs/?page={$this_script}" type="text/javascript"></script> </head> ____________________________________________________________________________ ____ Then, in this example, in some_page.php's smarty template, you could have <form onsubmit="doValidate(this)" action="some_other_page.php" method="post"> and it would call the required validation javascript. You could include any amount of javascript functions for anywhere in the document inside a single encapsulating variable with the name of the currently executing script. ____________________________________________________________________________ ____ END OF EXAMPLE All this was actually a workaround for me because a W3C validation of XHTML rejects javascript within a document (prefers it be called from an external reference). Hope it helps somebody ;-) Shaun Johnston > >I have a JS script inside my tpl that Is needing to use variables from the php and smarty but unfortunatly since JS seems to require a {literal} {/literal} around it, its not parsing the data properly. I am not using the {literal} tags simply because i would need countless numbers of them due to the amount of variables we insert into our js. Is there any way around this? > > > >Thanks.

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