I've got a block view defined. I'm embedding it on the profile page thus:

print(views_embed_view('my_view', 'block_1'));
?>

It's working great, except I'm not getting the view's title. Is that the intended behavior? I could put that in manually, of course, but it seems like the view should write it out for me.

Comments

sprugman’s picture

oops. For some reason it didn't like my code, and I forgot to preview. Here's what I wrote in between the two paragraphs:

print(views_embed_view('prospects', 'block_1'));
merlinofchaos’s picture

That's right, embedding a view doesn't show the title. This is because the titles tend to be displayed separately from the content, which means you have to do that kind of thing yourself. Embed only prints the body of the view.

merlinofchaos’s picture

Status: Active » Closed (works as designed)
sprugman’s picture

Is there, by chance, a handy function to print out the view's title, header, and/or footer? Those can change based on the view content, and it would be nice not to have to duplicate that logic in my module....

merlinofchaos’s picture

You have to take apart views_embed_view() (it's short, really) and you can use $view->get_title() and the slightly longer $view->display_handler->render_header() (as well as the complementary render_footer()).

Note that the header & footer WILL appear in a view, so you shouldn't need to render them separately anyhow. They're part of the views-view template.

Which leads to another option: You could have the title embed itself in the views-view template, but if you do that then the title will double up in the block. So that's not necessarily a terrific solution.

sprugman’s picture

Got it, thanks.

VTM’s picture

Can you please help me with implementation of the command: $view->get_title()

The script that works for me is:

<?php
print views_embed_view("node_ref_shop_list","block_2");
?>

When using the following script, I get the text "Array":

<?php
$view = views_get_view('node_ref_shop_list');
print $view->execute_display('block_2');
?>

Many thanks

merlinofchaos’s picture

Use $view->preview() instead of $view->execute_display() -- execute_display() returns what that display type thinks it needs. In this case, it's the array returned by hook_block($op = 'view');

VTM’s picture

preview did the change. The block content is displayed correctly. Thanks.

It seems that the $view->get-title() is not working for me.

this is the code I use, and see no title:

<?php
$view = views_get_view('node_ref_shop_list');
print $view->get_title('block_2');
print $view->preview('block_2');
?>
pcambra’s picture

If you want the title of a specific display, you have to set the display first, as get_title doesn't have parameters.

$view = views_get_view('node_ref_shop_list');
print $view->set_display('block_2');
print $view->get_title();
print $view->preview('block_2');
anou’s picture

It should be :

$view = views_get_view('node_ref_shop_list');
$view->set_display('block_2');
print $view->get_title();
print $view->preview('block_2');

If you put "print" on each line, you'll get a "1" before the title :-)

clecidor’s picture

In Drupal 7:

$name = 'node_ref_shop_list';
$display_id = 'block_2';
if ($view = views_get_view($name)) {
  if ($view->access($display_id)) {
	$block = $view->execute_display($display_id);
	$block['module'] = 'views';
	$block['delta'] = $name . '-' . $display_id;
	$block['region'] = NULL;
	
	// Before returning the block output, convert it to a renderable array
	// with contextual links.
	views_add_block_contextual_links($block, $view, $display_id);
	$renderable_block= _block_get_renderable_array(array((object) $block));
	print drupal_render($renderable_block);
  }
}
zunaeid’s picture

To render views block Drupal 7 I thing this is better


$block = block_load('MODULE_NAME', 'DELTA'); 
print drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))));

For views here
MODULE_NAME = "views";
DELTA = "VIEWS_NAME-DISPLAY_ID"

$block = block_load('views', 'related_courses-block_1');
print drupal_render(_block_get_renderable_array(_block_render_blocks(array($block))))