Making new variables for use in templates file

  • 23 October 2012
  • mohit.aghera

 

The variables process functions also allow you to define additional variables in your theme.
 
To create a new variable, you must declare the function in the template. php file. 
 
In order for your theme to have its preprocessors recognized, the template associated with the hook must exist inside the theme.
 
If the template does not exist in your theme, copy (or create) one and place it in the theme directory. 
 
The syntax is the same as that just used for intercepting and overriding a variable, as seen above. The ability to add new variables to the system is a powerful tool and gives you the ability to add more complex logic to your theme
 
By way of example, let's assume you want to add a This is default text. This is default text' statement at somewhere on your site. First, let's make the variable available to your templates. To do this, add the following to your template.php file: 
<?php 
function bartik_preprocess(&$vars){ 
$vars['defaulttextvariable'] = t(‘This is default text. This is default text'); 
}
?> 
You now have a new variable named 'defaulttextvariable'. 
Here we have included t() function to provide for translation of the new string. This is exactly the sort of processing you want to place outside the templates themselves. 
Next, to get the disclaimer statement to appear on your comments, open the any template file and add the following where do you want to print the text: 
<p><?php print $defaulttextvariable; ?></p> 
Flush your caches and then load a page. You will find the relevant text.
 
Tags: 

Comments

quest bars's picture

It's very effortless to find out any matter on web as compared to books, as I found this post at this
web site.

Add new comment