3

I'm used to write this kind of code in .tpl.php files in order to display images (for instance) :

<?php echo $base_url .'/'. path_to_theme();?>

I wonder if it would be possible to concatenate $base_url and path_to_theme() into one single command/variable ?

1
  • Do you mean setting a variable as $path = $base_url . '/' . path_to_theme()?
    – apaderno
    Apr 14, 2011 at 14:09

2 Answers 2

4

In hook_preprocess($vars) create a new variable called path_to_theme in $vars by including the following (where $hook is page, node, block etc)

function mymodule_preprocess(&$vars, $hook) {
  $vars['path_to_theme'] = base_path().path_to_theme();
}

Now in virtually all of your theme's template you can use the variable $path_to_theme and it will be the contents of base_path().path_to_theme()

Note, that you can put this in a custom module, or replace mymodule with the name of your theme and you can place it in your theme's template.php

You can use node, page, and other "hook" specific preprocess functions if you only wish to add this variable to specific content of your site.

There are many options for preprocessing nodes, pages etc. which can be read up in: Setting up variables for use in a template (preprocess and process functions)

Also be sure to read up on the Default baseline variables such as:

$directory The theme path relative to the base install. example: "sites/all/themes/myTheme"

0

You can theme_preprocess_node() or theme_preprocess_page() in your template.php file in your theme and create your concatenated path there so it is accessible in template files.

function yourthemename_preprocess_node(&$vars) {
  $vars['theme_path'] = base_path() . path_to_theme();
}

function yourthemename_preprocess_page(&$vars) {
  $vars['theme_path'] = base_path() . path_to_theme();
}

Then $theme_path is always available in your template files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.