Embed Drupal views in content, template or in your module

This is how you can get the view embedded. Another nice little trick is to actually embed the view in the node content/body, template or a function in your module. This way you don't have to create a region just for the gallery system, and it is so simple that it would be a shame not to do it this way! The only thing you need to do is to edit the node template of your theme, and paste a little snippet of code that will tell drupal which view and which display to embed. The code is like this :

<?php print views_embed_view('view_name', $display_id = 'display_id');?>

Just replace the view_name by the name of your view (the name of your view is shown in italic characters in the title of the page when you edit it), and replace display_id by the id of your display. For example, if your display is the first block of the view, its ID is block_1. Go to views and hover over the tabs to desired display and you find the correct argument to this function in the URL in the status field.

So for my view, this is the snippet I had to paste in the node.tpl.php file of my theme :

<?php print views_embed_view('gallery', $display_id = 'block_1');?>

To add an argument, just append it to the function arguments. In this case $nid is going to be used as an argument to the view.

<?php print views_embed_view('gallery', $display_id = 'block_1', $nid);?>

To add a view in the body/content of a node/page just add PHP tags, like above, and change Input format to PHP code and save. If you are using a WYSIWYG editor its a good thing to turn it of on this node. Then you always see that its a code node and no <P> tags or other tags will mess up the content.

This is how you can use exposed filters when using views_embed_view().

= views_get_view('keyword_view', 'page_1', $project['id']);
$keyword_view->override_path = $_GET['q'];
$keyword_view_out = $keyword_view->preview();

Show view only if it has content

This will just make the output if the view is not empty, no title will show on empty list view. You can of course extend with more arguments if you like or use func_get_arg() if you want it dynamic. Call this function in the same way as views_embed_view: my_embed_viewer('my_view_name', 'block_1', $arg1, $arg2);

 * Custom views_embed_view function
 * @param unknown_type $view_name
 * @param unknown_type $display
 * @param unknown_type $arg1
 * @param unknown_type $arg2
 * @return unknown
function my_embed_viewer($view_name, $display, $arg1 = FALSE, $arg2 = FALSE){
$view = views_get_view($view_name, $display, $arg1, $arg2);
$view->args = array($arg1, $arg2);

    if (
$view->result) {
$out .=  $view->get_title();
$out .=  $view->preview($display);

Knowledge keywords: