Drupal 8 custom templates tpl - php

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

Related

Prestashop 1.6 - php into admin override tpl

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

Creating plugin to call mybb header template in Smarty 3

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.

how can i change the entire template of the page according to module in drupal6

I have check lots of area in the web about theming in drupal6. All the tutorials in the web saying theme module, which means the theming a small area or theming a samll blobk etc. But in my case i need to change the entire template of a page when i access a module. Is this is possible in drupal6?
You can write if conditions in your theme's template.php function.
The function is template_preprocess_page(&$variables)
And in this function, write some if conditions.
For example:
if($some_condition) {
$suggestions[] = 'YOUR_CUSTOM_TEMPLATE';
}
$variables['template_files'] = $suggestions;
And you will have your template "YOUR_CUSTOM_TEMPLATE.tpl.php".
Which should be there in theme's templates directory.
Also, you can have a custom template for a content type.
e.g. For a content type student, the template will be:
page--student.tpl.php
Got from Drupal Answers ( https://drupal.stackexchange.com/a/18670 ) :
How can I create a custom page with a different template?
You can use a different template for a page by suggesting a new
template file that will be used.
This can be done in a theme, by writing its
template_preprocess_page(), or in a module by implementing
hook_preprocess_page().
In the case you are using it in a module, template_preprocess_page() needs to be
renamed mymodule_preprocess_page(), where "mymodule" is the short name of your module.
function mymodule_preprocess_page(&$variables) {
if (arg(0) == "node" && arg(1) == "1") {
array_unshift($variables['theme_hook_suggestions'], "page--custom-template");
}
}

Call Module in tpl file in prestashop

I want to use search module in my header.tpl file .
In that i used this code
{include file='./modules/blocksearch/blocksearch.tpl'}
But it's not working and by using this code my page gets blank.
Thanks in advance
Here you will find what you are looking for
http://www.ecartservice.net/prestashop-articles/1-4-plugins-revisited-part-1/
It is a great and easy way to call modules directly in template files, without the use of hooks. I tested it in both Prestashop 1.4 and 1.5 and it works.
For Prestashop 1.5, you will need to create 2 extra files in the "override" folder, as described in the link above:
Plugin.php in /override/classes/
FrontController.php in /override/classes/controller/
Then, you will be able to use in the template files the following code:
{plugin module='editorial' hook='displayHome'}
You need to use the hook's name (displayHome) and NOT the alias (home).
You cannot include a module template like this because the PHP code of te module is not called and the template needs it to set some Smarty variables.
The best you can do is to hook the blocksearch module to your header. Here we go :
Navigate to "admin > modules > positions"
Click on "Transplant a module" button
Select "Quick Search Block" for "Module"
Select ""Header of pages / displayHeader" for "Hook into"
Click "Save"
The "Quick Search module" is now hooked to your header.

Theming and layout in yii framework

I am a newbie in Yii Framework and creating a CRM which is module based.
Using different tutorials I am able to create my own theme, but now I am stucked at one point.
In my theme, the upper <nav> and left <nav> remains the same throughout the app, until user is logged in. That's why I made it a part of my main.php, but in the login page there are no buttons to show, just simple login form with 2 textfields.
How can I implement this form in my application using custom themes?
I have tried to define a layout in that particular action but not succeeded. Any help would be appreciated.
Using a custom layout for your view is the right way to go.
You can either set the layout in the controller action or in the view.
$this->layout = "//layouts/mylayout";
Note that the default layouts column1.php and column2.php also use the main.php layout file.
Try this step by step :
Create New theme
You can create a new theme and add this to the directory
Application_Root/themes.
Look at the themes/classic directory to get an an idea of the structure of the directory.
The important file (at this stage) is :-
Application_Root/themes/views/layouts/main.php
Customise your theme contents
Copy the css, image, js files etc to the correct directory and change the main.php file to your liking. For example, if your main.php says
<link href="css/mystyle.css" rel="stylesheet">
Then you will have a file
Application_Root/css/mystyle.css
Create the content placeholder.
Somewhere in your main.php, there will be a placeholder for dynamic text, which is specified by.
<?php echo $content; ?>
Tell yii to use the theme.
Change the file Application_Root/protected/config/main.php by adding the following line just before the last line (containing the closing bracket).
'theme'=>'surveyhub'
Create the layout placeholders.
Create an HTML segment that will be written into the $contents portion of main.php. Call it for example one_column.php. The file path will therefore be Application_Root/themes/views/layouts/one_column.php In that file, where you want the dynamic text to be placed, create a placeholder.
<?php echo $content; ?>
Tell Yii to use the layout.
In the file Application_Root/protected/components/Controller.php, add or modify the layout variable to read :
public $layout='//layouts/one_column.php';
Refresh the page

Categories