Prestashop PHP Include in a .tpl does not work
I am creating a storefront for a client using Prestashop. Prestashop uses Smarty .TPL files. I read through smarty documentation and scoured the web but all of the suggestions are not working.
I first made a site using regular .php pages and I am including the header.php on every page.
I then made a directory for prestashop and got it set up. I edited the header.tpl file and was able to hard code in the header.php code. The problem with this is; when I want to edit the header (nav bar, images, social media), I will have to edit it in two different places. So I tried to "Include" my header.php file.
Though, when I try using smarty's {include_PHP "file.php"} and/or {PHP}include...{PHP}, Prestashop errors out and gives me a blank white page - no errors are given - (in chrome it gives me a "server error") until I take out the includes.
I tried replacing the entire header.tpl code with a smarty include and another piece of code that had a header hook, but none of these worked. Any suggestions? I just want one header where I only have to edit it once to make changes.
Using Prestashop v 1.4.4.0
Edit: I changed allow_php to true from fals开发者_高级运维e. Now it's trying to add the file, though it says it can't find the file. I placed it next to header.tpl and just used:
{php}
include('navBar.php');
{/php}
ANSWERED!
When using Smarty .TPL files, when you include something, you are not including from the path of the file you are working on. You are including from where the index is.
Example:
I am working on header.tpl, this is located in: siteroot/smartyinstall/themes/themename/header.tpl
When the include is looking for the file, it is actually looking for it in the smarty root folder because the header.tpl is being pulled into the index.html page that is in the smartyinstall folder.
So, you have to go from there. In my case, the header that I was trying to include was in: siteroot/includes/navBar.php
so, I had to write include('../includes/navBar.php');, only going up one directory, instead of four.
I hope this helps everyone that has a problem like this!
It's considered very bad practice to include php in smarty .tpl files so I would strongly recommend you don't add code this way. One of the main reasons for disabling the {php}
tag is to help prevent code injection attacks. eCommerce sites are by their very nature natural targets for exploits.
A better approach would be to override the FrontController
class to assign your custom code to a smarty variable - this could then be inserted into the header.tpl without resorting to using a php include()
.
With the class and controller overrides available in Prestashop 1.4.x there's no real reason you should need to resort to hacks and/or modifications to the core distribution.
Paul
精彩评论