Is there a way to avoid implementing a logic in the view template? 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.

&lt;?php

  $view = views_get_current_view();
  $rowCount = $view-&gt;total_rows; 

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

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



?&gt;

&lt;h3&gt;&lt;?php print($resultText);?&gt;&lt;/h3&gt;
... and rest of the template follows unchanged
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(&amp;$vars) {
  if ($vars['view']-&gt;name == 'some_view') {
    $vars['foo'] = markup_building_function();
  }
}


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

&lt;?php echo $foo; ?&gt;