Hi,

I want to create a new variable to use in all my views template files.
In template.php I've created a new funtion:

function mytheme_preprocess(&$vars) {
$vars['myvar'] = 'this is my new var';
}

Now, I want to use this new variable in my views-view-fields--viewname--block.tpl.php files, but how can I do that?
I've tried following...

print render($page['prevmonth']); (when the function was called function mytheme_preprocess_page(&$vars) )
print render($views['prevmonth']); (when the function was called function mytheme_preprocess_views(&$vars) )
print render($block['prevmonth']); (when the function was called function mytheme_preprocess_block(&$vars) )
print $prevmonth;

... but nothing seems to work. What am I doing wrong?

Thanks!

Comments

imranweb’s picture

Hi,

You may try using module's hook.

e.g.

function MODULE_preprocess_views_view(&$vars) {
  $vars['custom_variable'] = 'foo bar';
}

You may also try by theme hook.

function THEME_preprocess_views_view(&$vars) {
  $vars['custom_variable'] = 'foo bar';
}

Note: Kindly replace "MODULE" with your module name and "THEME" with theme name.

-Imran

creathing’s picture

Thx for the quick reply!

However, I'm still having problems.
I've changed my function in template.php to:

function mytheme_preprocess_views_view(&$vars) {
$vars['myvar'] = 'this is a new variable';
}

And in my views template file I try to print the variable like this:
print $views_view['myvar'];
or print $views['myvar'];
Then I get an Undefined variable notice

or when I print it like this:
print $views['myvar'];
I get a fatal error: Cannot use object of type view as array in views-view-fields--viewname--block.tpl.php

Any thoughts?

Jeff Burnz’s picture

creathing’s picture

Hi Jeff,

Thanks for the quick reply!
indeed, correct, I've tried to print following:

print render($page['myvar']); (when the function was called function mytheme_preprocess_page(&$vars) )
print render($views['myvar']); (when the function was called function mytheme_preprocess_views(&$vars) )
print render($block['myvar']); (when the function was called function mytheme_preprocess_block(&$vars) )
even print $myvar;

But every time I get an undefined variable notice. Any ideas? Thx!

esavoie’s picture

I'm also trying to access a variable from a parent theme inside my subtheme and it gives me undefined variable error as well.

I am trying to put in OM Maximenu as my main menu and i can't find where to put in the suggested code

 print $main_menu_tree; 

.
I tried putting it in the region--menu.tpl.php in openpublish/themes/openpublish-themes/gazette/templates but it gives me the following error:

Notice: Undefined variable: main_menu_tree in include() (line 3 of /profiles/openpublish/themes/openpublish-themes/gazette/templates/region--menu.tpl.php).

If I put the exact same line of code in the openpublish/themes/omega/alpha/templates/page.tpl.php it works just fine and displays my menu. However it doesn't show it in the spot i want. I want it in the menu region. How can I do this?

Is there something I need to do to make the openpublish gazette theme recognize the $main_menu_tree variable? Do I need to include it somehow? It is available in both the omega alpha and omega omega themes, just not in the gazette theme...

Any help is appreciated.

Thanks,
Eric

esavoie’s picture

Ok after around 10 hours of fiddling with this and looking at random code I found all over the internet I finally figured out a solution to my problem.

In my sub-theme\preprocess folder I created a file called "preprocess-region.inc" and inside I wrote the following code:

    function gazette_alpha_preprocess_region(&$vars){
        $vars['main_menu_tree'] = alpha_get_theme()->page['main_menu_tree'];

So what I've achieved is simply grabbing the main_menu_tree variable from the alpha theme and put it in my sub_theme via the preprocess