I want to include a PHP script that outputs some HTML in productdetail-full.tpl file (Smarty / Prestashop 1.6.x)
I tried:
{php}
include('show-stock-pos.php');
{/php}
AND
{include_php 'show-stock-pos.php'}
But they are both deprecated. Any suggestions?
Thanks!
You should use SmartyBC - Backwards Compatibility Wrapper for this since it's not recommended using php code in the templates.
Instead of:
require_once('path/to/smarty/libs/Smarty.class.php');
$smarty = new Smarty();
Use:
require_once('path/to/smarty/libs/SmartyBC.class.php');
$smarty = new SmartyBC();
And you will be able to use PHP in your Smarty template files.
More info about this here:
https://www.smarty.net/docs/en/bc.tpl
Prestashop is a modular system that uses hooks to display information.
According to Prestashop standards and solutions, you should use hooks and module:
Generate new module with a custom hook (or Use the available hook in the Productdetail-full.tpl file)
Get content of your PHP file in your module (using curl for example)
Pass your content to smarty
display content in your Hook
Related
Just wanted to include a custom php file into an override template I've made but when I use:
{php} include('custom_code.php'); {/php}
or:
{include_php file='../../../../../../panel/update.php'}
The page crashes. Prestashop is so hard to modify.
The approach you are following to include the PHP file is absolutely wrong, you should include the file in the Class file responsible for rendering the TPL file.
Once you do that you can write the business logic in the class file and then pass the required data to TPL file using the following code:
$this->context->smarty->assign('any_var', $any_value);
By using this you can get the data in your Smarty file and use the same accordingly.
I have solved the issue creating a plugin for smarty in the directory /tools/smarty/plugins with the file name function.update_customer.php . then i pute the code in my template .tpl with this shortcode hook {update_customer} .
Hope that helps in the future...
Is there a way to integrate php instead of twig in Drupal8? Twig looks somewhat annoying. But in drupal 7 we could create tpl files with php syntaxes. Is there any way to create tpl files in Drupal 8 too. I am a beginner for drupal.
this is one of the major and finest change in D8 change template system in twig
in drupal7 no one recommend you to write php code like function or queries or any processing code in theme layer or template file
in drupal 8
HOOK_preprocess_page
and
HOOK_preprocess_node
are available same as in drupal 7
you have to write your code what you want in these fuctions and then send to the template what you want to print
like if you want print current user name in your template then do some thing like this
use HOOK function hook_preprocess_page
if you are writing code in your theme then write this code into your .theme file
if in module then in .module file
function themename_preprocess_page(&$variables) {
global $user;// current user object
$variables['customUserName'] = $user->getUsername();
}
change themename to your theme name if in module then replace with module name
in your twig template you will print this variable like this
{{ customUserName }}
we are overriding page template so your are able to get this variable in all you page templates
if you need this variable in node template then rename function to
function themename_preprocess_node(){}
hope this help you
thanks
I have created a basic template in Visual Composer which mostly defines a layout of the page. Now I can select this template from "My templates" in VC.
However, what I really need is to call the template from a single_page.php file. Ensuring that end users can't change the layout via VC.
Is there any way how to do so? Via some VC API function or shortcode? Something like [vc_template name="my_custom_layout"]?
Thanks for any help,
Jakub
Have you tried
echo do_shortcode( "[vc_template name='my_custom_layout']", false );
I want to include in prestashop (smarty) in the folder "/themes/my_template/modules/my_modul" a php file in the tpl.
example:
<?php
include('/tools/smarty/Smarty.class.php');
$smarty = new Smarty;
$vorname="Horst";
$nachname="Meyer";
$smarty->assign('vorname',$vorname);
$smarty->assign('nachname',$nachname);
// ausgabe
$smarty->display('my_template.tpl');
?>
but the variable {$vorname} is not displayed. what am I doing wrong?
Not sure, that your way will work.
You are working inside templates directory, so you have to create a *.tpl files there.
If you want to include php file, you can use include_php directive inside your template http://www.smarty.net/docs/en/language.function.include.php.tpl
If you want to display an variable inside template, you can use http://www.smarty.net/docs/en/language.function.assign.tpl
I am working with a Smarty 3 script and I need to call mybb header and footer templates. I normally just create a .php page and put the eval code directly in it but I can't do that with smarty I have to make a plugin. Is it possible to make a plugin that calls templates from mybb?
The normal code to call mybb templates would be like..
<?php
define('IN_MYBB', 1); require "./global.php";
add_breadcrumb("Title here", "somename.php");
eval("\$html = \"".$templates->get("template_name")."\";");
output_page($html);
?>
I have never worked with Smarty let alone making plugins for this?
If you want to assign anything to variable in PHP using Smarty, you could use fetch() method.
For example in PHP:
$html = $smarty->fetch('template.tpl');
In Smarty template file:
This is template.
And after running it, you will have assigned This is template text to $html variable.