Drupal and smarty templating
Tue, 10/06/2009 - 20:52
Drupal comes with a default “templating” system called php. It’s handy to use php in the templates but i thinks it’s ugly. So i started using the smarty engine and it took me a while to get used to it.
To get you started i will add some ideas to make templating with the smarty engine allot simpler.
In template.php you can add the function smarty_engine_preprocess(&$vars) you can use this function to add variables to use in the smarty templates.
For example: $vars[‘page’] = arg(1);
Now in your page.tpl you can test on this var array with smarty:
{if $page==’lightbox’}
{include file="$directory/maintpl/contact.tpl"}
{/if}
This way you have more control over your templates, the next thing is a bit of a dirty hack (may be there is a better way please tell me) but in some situations this can be very handy. Let say you have an module that needs to set some variables in the MENU_CALLBACK function. I don’t know why but this cannot be done the official way. Drupal renders the modules first and after that it generates the templates. So to go arround this you add the function.
function yourmodule_preprocess(&$variables){
global $vars, $template;
$variables['template'] = $template;
$variables['params'] = $vars;
}
In your MENU_CALLBACK function in this case a news detail page (news) you open with the global $vars, now you can add unlimited info to this var.
Example:
function news(){
global $vars;
$vars[‘test’] = array(‘test’,’this’,’value’);
$vars[‘title’] = t(‘Some translatable title’); // can also be done in the template
}
After this you can use this variables in your template like this:
{$params.test|print_r} and {$params.title}
the last one is a bit useless, because you can also do this in the template like this:
{‘Some tranlatable title’|t}
Well this is it for now more tips will follow.
Security CCTV Camera | SEO Service India| SEO Friendly Directory | Leather Bags | Navi Mumbai Real Estate
Generic Viagra | Cheap Generic Viagra | Generic Levitra | Generic Viagra | Cheap Generic Viagra | Kamagra | Cheap Generic Viagra | Finpecia | Penegra | Generic Viagra and Kamagra | Generic Viagra
Post new comment