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