Prestashop PHP Include in a .tpl does not work - php

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 false. 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

Related

php files inclusion structure in Joomla?

I am currently experimenting with Joomla (3.7) templating system for my company. I am not new in php and inclusion, but a beginner with Joomla.
I have been searching through the web and i haven't found any clear structure schema for the file inclusion in Joomla, something like what we can find here for wordpress for example: https://codex.wordpress.org/File:Template_Hierarchy_2015.png
For example, i stumbled upon this: while experimenting with template creation, i wanted to store a value that could be used in a layout, testing setUserState function. The layout i tested was the one used to render custom fields (which are new in 3.7). The result is that it seems that fields/render.php is actually executed before template's index.php (the value is only available after page reload when set in index.php, whereas it is immediate when set in render.php).
Did someone saw a clear schema of this inclusion structure? It is not only for the said example, but mostly for a better understanding of the framework.. sorry if already asken or if too vague for the site.
edit - note about my example: collecting info bits by bits, it seems that the template file index.php is executed first and buffered so that it can be parsed for jdoc statements. Intuitively, because an article content is in this case imported by a jdoc (<jdoc:include type="component" />, the main content in the article page), the custom fields render.php should be called after index.php. Could it be the buffering part that prevents the user state to be set in time?

Adding jQuery code in Joomla

I need to add this code to Joomla please
$(".item-204").append('<ul class="nav-child unstyled small"><li class="item-205"><a href="index.php?option=com_content&view=article&id=9" >رؤيتنا</a></li>');
what is the best way?
One way to do this would be to use Regular Labs ReReplacer which could be set up to replace the .item-204 code with .item-204 plus any additional code you require. I am assuming .item-204 is unique on the page.
The advantage of using this extension is that you don't have to find or update the core code. Updating core code is often considered bad practice as any future template or extension updates can undo your changes.
It's really just about adding this code to every page? Instead of adding another plugin so your site you can put your code in a script tag located in your templates main php file. This file is named index.php and is located here: /templates/[your template name]/index.php
If you want to do it even better, place this code in the templates JS file. Some of them have a file like /templates/[your template name]/js/custom.jswhich is a good place to add custom JS to.

including header.php and footer.php automatically

I have been trying to search a solution online and on stackoverflow but did not come across any satisfactory solution.
I wanted to know how can i include (if required) header.php and footer.php automatically in the new PHP pages that i create, at present whenever i create a new PHP file i manually add header.php on top and footer.php at the bottom, I dont really have a problem with doing this but i was wondering if this is how everyone does it and if not what is the best solution to this.
I know I could takeup any framework out there and use that because may be they include the header and footer automatically, but i am referring to custom PHP coding.
I will really appreciate views of experienced developers out there.
The usual approach is to use the front controller design pattern.
Use a single PHP file to load all the pages. Have it include the header and footer and use the URL (and whatever other information you like) to determine which content to include.
PHP developers often use mod_rewrite to map URLs onto a single script. You could also use the Apache alias directive.

How to use two Wordpress templates at once... or something similar?

I'm building a wordpress template with a lot of javascript, so I am also setting up a fallback version what requires different php in the index file of the template, and preferably, different header files.
The best way to do a fallback seems to be setting a javascript redirect on the "non-javascript site" to go to the "javascript version" of the site.
I am a bit confused on how the index.php file from the template folder trickles all the way down to the root directly. Yes, a understand php includes, but don't know how to jam a different file down the wordpress pipeline.
I was thinking I could do a redirect to mywebsite.com/index2.php but then I realized I didn't know how to get that to work.
Any ideas on how to do this?
It isn't the index.php that handles what template you use. Is a field in the database.
You would need to create a new theme that has both and switches them based on a session variable.
I am guessing this is nontrivial.

Wordpress and my own custom PHP stuff

Often times, when I try to add my own custom PHP source codes to the template files (sidebar.php, index.php, header.php, etc...), it generates errors. However, it works properly on it's own outside of Wordpress. Is there a way for me to use my custom PHP stuff in Wordpress' template files?
If you're talking about the sidebar.php, index.php and header.php in wp-content/themes/themename/, well, of course you can edit these files. They are meant to be edited. Only make sure that you don't overwrite existing PHP functions...
You can read about that on Wordpress' Docs
No plugin required to add your own php, but to maintain upgradability you should, as far as possible, avoid altering core WP files and place your code within your own theme's files or in your own plugins. If you're getting errors it's hard to guess at what they may be without details, but I've found that, apart from simple parse errors etc., the most likely causes are scoping errors.
With Wordpress you can extend Wordpress functionality by writing your own plugins. This is a fairly pain free process.
http://codex.wordpress.org/Writing_a_Plugin
You can also extend the functionality that templates have by putting functions into the functions.php template file.
http://codex.wordpress.org/Theme_Development#Theme_Functions_File
Adding custom php code to templates shouldn't generate errors. The only problem you might be having is that your code is conflicting with existing wordpress function names and variables.

Categories