2

I need an advice. Is there any downsides when embedding views in the page/node tpl files like the following:

<?php print views_embed_view('course_videos', 'block_1'); ?> and this <?php $key = views_get_view_result('course_videos', 'block_2'); ?> in the node.tpl files

I am little bit concerned cause I've read several articles saying you must not query the db or use a complex php logic in the tpl files. Everything's working like a charm now. The site is still not in production though...

Thank you in advance!

6

Including views in tpl is going backwards. Drupal allows you to use views as pages and as blocks. You can apply templates to that pages and blocks, and you can put blocks in regions. By calling views from template, you are messing with theme registry and cache logic. It might be not an issue if site is small, and both read and write volumes are low, but it will result in pages being out of sync on heavy loaded site.

Instead of outputting view directly, make a region, output it in template file, and put a views block inside.

Using views_embed_view in a preprocess function in template.php might be OK. Opinions and experience with it can vary, and I would try to avoid it, but certainly it does not mess with cache or theme registry like using it in tpl would.

| improve this answer | |
  • ok, but what if I get a view in the template.php preprocess function and pass it to the tpl file like a variable, like the following: In the template.php $variables['views_course_videos'] = views_embed_view('course_videos', 'block'); In the tpl file: print $'views_course_videos'; Is this way ok? – Ivan Ivanov Sep 11 '13 at 12:08
  • 5
    @Mołot Gotta say I disagree - using views_embed_view in a preprocess function in template.php is pretty standard (obviously not if you're trying to build a totally de-coupled theme, but for custom project-specific themes I've seen it quite a bit) – Clive Sep 11 '13 at 12:14
  • 2
    I'll second that in preprocess, setting a variable to the output of a views hook is pretty widespread as well but totally agree it should be kept out of the actual tpls. – Jimajamma Sep 11 '13 at 12:18
  • 1
    I hope @IvanIvanov will read your comments too, and make his own decision as consciously as possible. Maybe one day I'll feel a need to try it again and change my mind? Not happening soon, but who knows ;) – Mołot Sep 11 '13 at 12:24
  • 1
    @Clive updated my answer. I hope it's as factual as feasible now, and opinions may sit in comments. I've seen it work OK and I've spent long hours when it didn't. In ideal world... ;) – Mołot Sep 11 '13 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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