0

I have this in my template.php:

function mytheme_preprocess_views_view(&$vars) {
$vars['test'] = 'Filtertest';
}

And this in my views-view-responsive-grid--taxonomy-term.tpl.php:

<?php print $test; ?>

... this does not work though. The template is called and works but my $test variable does not get printed.

What am I missing here? Any help is greatly appreciated.

5
  • 1
    it's better to check first on view name like if($vars['view']->name == 'your_view_name') { $vars['test'] = 'Filtertest'; } , and make sure that views-view-responsive-grid--taxonomy-term.tpl.php the right tpl for this view, and flush all caches
    – berramou
    Commented Jan 28, 2019 at 9:20
  • Yes, you are right, I have changed that now, The template is called and works, so i suppose it should be possible to pass variables to it?
    – TBJ
    Commented Jan 28, 2019 at 9:31
  • To variable to view tpl $vars['test'] = 'Filtertest'; should works, just be sure that you in the right tpl .
    – berramou
    Commented Jan 28, 2019 at 9:41
  • berramou: I finally got it to work by changing the template filename to a shorter name and using the exact same name in the preprocess function. See my answer below. Thank's for helping.
    – TBJ
    Commented Jan 28, 2019 at 9:51
  • great, you are welcome.
    – berramou
    Commented Jan 28, 2019 at 9:54

2 Answers 2

1

Replace this, with your function

public static function mytheme_preprocess_views_view(&$vars) {
  $vars['test'] = 'Filtertest';
  return $vars['test'] ;
}

Then use this short code on your twig view:

{{ test }}

or if wanna use it in something.php in your views:

dump(test )
0

In case anyone else have the same problem. Changing the name of the template file to views-view-responsive-grid.tpl.php and then using that same name in the preprocess function in template.php, like this:

function mytheme_preprocess_views_view_responsive_grid(&$vars) {
  if($vars['view']->name == 'taxonomy_term') {
    $vars['test'] = 'Filtertest';
  }
}

... fixed the problem. Now the $test variable is printed. Apparently the template's filename can't be too specific.

Your Answer

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

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