![]() |
Can you use PHP code in Smarty templates?
I'm sure this is a question that's been asked before, but I can't find an answer.
Can you use PHP code inside a .tpl file, or at least include a php file? I tried <?php include("somefile.php") ?> and it didn't parse the PHP at all. I have some very specific mods I need to make that require custom PHP code. I have no time or desire to learn the Smarty syntax and was desperately hoping I could just use PHP. Frankly when I bought X-Cart I thought it would be mostly PHP-based, so all this Smarty code has thrown a wrench into my schedule. Thanks for any help. |
Hi AJ,
I joined this forum about a week after it was launched, and I have never seen this question asked. I have struggled to learn bits of smarty, and php to create most of my mods. I remember reading that you can include php in the templates, but it should be avoided, so I have done the opposite in my latest mods, I have put HTML in my own created PHP files, this way you reduce problems with future upgrades of xcart. My latest mod is more of a plugin to xcart, so 99% of the original xcart files, are still in their original state. Post if you need further info on this method. Here is the extract and the URL to it's source. Quote:
at http://smarty.php.net/manual/en/what.is.smarty.html Regards, |
Since I haven't seen anyone post a solution on this, I'll post a couple methods I'm using.
To include a php file in a Smarty .tpl file, just do this: {include_php file="/pathtofile/file.php"} This seems to work pretty well, assuming you don't need to use any variables from Smarty. You can also use actual PHP code too! Just do it like this: {php} php code goes here; {/php} For this to work, you have to set the $php_handling variable to SMARTY_PHP_ALLOW in /Smarty-2.1.1/Smarty.class.php. Hope that helps someone out there. Took me forever to figure it out. Now, if someone can tell me how to pass a Smarty-defined variable to an included PHP script, I'd love to hear it! |
Hi,
try Quote:
or Quote:
I have not tried this with an "include_php file", as I try to keep php out of the templates. Regards, |
Unfortunately, neither of those methods work.
|
Same issue...
I just posted the same question on the editing templates forum, did'nt realize it was here...
Did you find a solution? |
Quote:
Does anyone know if there is a comprehensive list of the variables Samrty uses? |
http://smarty.php.net is your friend.
With regards to this topic the following links are especially helpful: http://smarty.php.net/manual/en/variable.php.handling.php http://smarty.php.net/manual/en/language.function.fetch.php The fetch command allowed me to include a php file ! Joy :) |
Hi,
Can I use a smarty variable in the php file name when I am using include_php: Code:
{include_php file="http://www.mysite.com/d/{$product.productcode}.php"} It doesn't seem to work the way I have it set above. Any help is greatly appreciated. ATKOgirl |
I've also tried it this way and it didn't work:
Code:
{php} I get the following error: Parse error: parse error, expecting `'}'' in /home/bbdeals/public_html/templates_c/189/1894910960/0.php on line 119 ATKOgirl |
Solution
Same thing was driving me crazy for a while last night, the answer was staring me right in the face all the time but it was so simple I couldn't see it until the very last minute then it hit me.
In the include file from smarty level do this: Lets say you want to include the above in product.tpl: Put Code:
{include file="customer/main/whatever.tpl" productcode=$product.productcode} In product.tpl Notice I'm passing the productcode via smarty. Now in whatever.tpl Put Code:
{php} And your home and dry. Note: Include() is dependant on server PHP configuration, looks like you're trying to pull in a product page from another site so keep in mind PHP may be set up in such a way that it doesn't let you do remote includes. |
Groovico,
You are the X-cart KING!! You gave me the resolution to an issue I've been trying to solve for months! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! THANK YOU! :wink: ATKOgirl |
All times are GMT -8. The time now is 07:45 AM. |
Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.