Drupal Answers is a question and answer site for Drupal developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Some where I have read that we should use views template xxxxx.tpl.php file to not implement complex logic. I want to show a search results message to the user. The message is not made of mere row count but it has little bit of logic depending on what options he has selected.

I have implemented this logic in my view mymodule-view-grid--apartments--listing-row-filtered.tpltemplate.tpl.

I don't think this is complicated logic that's why I went on and implemented this logic but a question always bothers me is that where should I implement this logic if at all I have to implement it outside and how to pass this variable to this template.


  $view = views_get_current_view();
  $rowCount = $view->total_rows; 

  isset($view->exposed_input['field_location_tid'])?$ltid = $view->exposed_input['field_location_tid']:$ltid = 'All';

  //force to all if landing directly
  $city = taxonomy_term_load($view->exposed_input['field_city_tr_tid'])->name;
  $locationName = $ltid == 'All'?'':' in ' . taxonomy_term_load($view->exposed_input['field_location_tid'])->name;
  $resultText = "Properties in $city: Your search criteria matched $rowCount properties$locationName";


<h3><?php print($resultText);?></h3>
... and rest of the template follows unchanged
share|improve this question
up vote 3 down vote accepted

Templates are for displaying content, not building it, so you're right to want to avoid this.

Drupal allows you to preprocess any theme function to add variables. I guess in your case it would be something like

function MYTHEME_preprocess_views_view_grid(&$vars) {
  if ($vars['view']->name == 'some_view') {
    $vars['foo'] = markup_building_function();

Then back in the template file you can output that var like so:

<?php echo $foo; ?>
share|improve this answer
Clive, I think you mean mytheme_preprocess_views_view? From my debug output it looks like the template is processed before the preprocessor. I can see the new variable in the $variables but not in the template. What I am missing? – Dudepal Mar 3 '14 at 10:59
Yeah I think you're right, does the update answer work? – Clive Mar 3 '14 at 11:12
No cleave, Just before your answer I read an article and tried these hooks aswell preprocess_views_grid, preprocess_views_list, preprocess_views_realia_grid I got realia grid from the export code which is this $handler->display->display_options['style_plugin'] = 'realia_grid'; These are not even calling the hook – Dudepal Mar 3 '14 at 11:23
Hmm, that should work if memory serves but I guess something's changed or I've just got it horribly wrong. You can always use MYTHEME_preprocess_views_views and check $view->name and $view->current_display, that will definitely work – Clive Mar 3 '14 at 11:41
yes its working but it looks like this hook is called after the template is processed. I have placed a debug code to display the variable in both hook and template. Its printing first empty in template and second the value from the hook – Dudepal Mar 3 '14 at 11:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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