Making additional variables available to your templates

As the Smarty Theme Engine is very closely ported from phptemplate most solutions are of the same nature.

For simple introduction of additional variables you implement a _smarty_variables function in the smartytemplate.php file within your theme directory (see http://drupal.org/node/70247 : smartytemplate.php: Your theme's powerhouse)

For example:
If your theme were box_grey_smarty:
Adding a smartytemplate.php file at themes/box_grey_smarty/smartytemplate.php with content of (file needs php tags):

<?php
function  _smarty_variables($hook, $variables) {
  switch(
$hook) {
    case
'comment' :
       
$variables['newvar'] = 'new variable';
       
$variables['title'] = 'new title';
        break;
  }
  return
$variables;
}
?>

Then within your comment.tpl you could then write New Variable: {$newvar} to print 'new variable'.

See the phptemplate page on template variable addition for more examples, just remember to replace _phptemplate_variables with _smarty_variables.

Drupal 6 smarty

b83s - September 23, 2009 - 08:02

There are some updates concerning Drupal 6, the file smartytemplate.php should be template.php and to add variables use this function.

<?php
function smarty_preprocess_page(&$vars) {

   
$vars['date'] = date("Y-m-d");
   
$vars['name'] = "Your name";
   
//etc
}
?>

How to use the t() modifier

b83s - October 1, 2009 - 14:04

If you have some text that you need to translate buttons, titles etc use the smarty modifier t() this is the way you can use it.

{'Paste your tekst here'|t}

 
 

Drupal is a registered trademark of Dries Buytaert.