Re: How to use a Smarty variable outside the template file ?
| From: | Luc Saint-Elie | Date: | Tue, 24 Jul 2001 07:09:37 +0000 |
| Subject: | Re: How to use a Smarty variable outside the template file ? | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-1269@lists.php.net to get a copy of this message | ||
At 08:53 24/07/01 +0200, Mike Krus wrote:
Hi, Luc Saint-Elie wrote:Hello Mike, No, its a regular XML file, the PHP header seems to be added by the fetch action, the first 3 lines I posted are not part of the original file that start by the classical : <?xml version="1.0" encoding="ISO-8859-1"?> but becomes after fetch <?php /* Smarty version 1.4.4, created on 2001-07-24 07:46:46 compiled from ../xml_files/cachecache/cachecache.xml */ ?> <?php echo '<?xml version="1.0" encoding="ISO-8859-1"?>'; ?> Tht is probably normal as files stored in templates_c are php filesWhen doing : $file = 'xml_files/'.$section.'/'.$file; $xml_data = $tpl->fetch('../'.$file); $xml = new XML($xml_data); What is piped into the XML parser, the stream containing the XML file read by $xml_data = $tpl->fetch('../'.$file); or the compiled result of this "fetch" action ? In the second case, the problem is perhaps that the file is no more recognized as a regular XML one. In templates_c the compiled xml file is this way : <?php /* Smarty version 1.4.4, created on 2001-07-24 07:46:46well, yeah, they would. It looks like the original file is actually a PHP script.compiled from ../xml_files/cachecache/cachecache.xml */ ?><?php echo '<?xml version="1.0" encoding="ISO-8859-1"?>'; ?>
So what you are trying to do is: - run a php script which generates XML data containing Smarty template commands - feed it to Smarty - feed the result to an XML parser. Is that correct?No What I try to do to is : - load a XML file in a classical php script - parse it (until this point its rather classical stuff) - generate arrays from this XML file - assign these array to a Smarty template All this works perfectly well and this way of manipulating file is very convenient for me to publish long text files. My problem occurs with a specific XML file that contains some variables (the displayed resulting HTML page displays the visitor IP and some other HTTP stuff) In the XML file they are Smarty placeholder (for example {$ip} for the IP) and I need to have Smarty replace these place holders by the actual values (as in a normal Smarty use by the way) In fact usually the template file contains.. the content and Smarty only replace placeholder {...} by assigned variables, in this case the template file is rather empty and Smarty has to to both, feed the template file with the content and replace the placeholder variable with their corresponding values. The solution Monte gave me seems the obvious logical one, have Smarty parse the XML stream them sent it to the XML parser (for that I use phpxml from www.phpxml.org) For instance my problem (that is probably more "my" problem than a "smarty" problem :- ) is that I fail to get a untouched XML stream after the "fetch"
Well ideally, you would do it another way ;-) If the only php code you have in there is <? echo ... ?>, just put the string directly in there....I have a complete php file, that instantiates smarty, that looks for environment variables, that call a php class for XML parsing , that parse the XML file, that assign and displays and so on....
But if your stuck doing it this way, you could : - write a smarty resource handler, say for resource "xml:" - in the handler: - load the XML script in a string - eval() that string to get the PHP executed (you probably need toI'll try this but I'm not sure it will work because "fetch" does work, in the outputed "fetch" result, the variable are replaced by their corresponding assigned values... Lucescape the $ in Smarty template code).- return the result to Smarty - feed the result of ->fetch("xml:$file") to your XML parser... That's if I get the exact nature of want you are trying to do ;-)