Right now this is my code:

$view = views_get_view('listing_comments');
print $view->preview();

using preview() does preserve paging, but it does not enable ajax paging. What is the correct approach to printing a view in a template file with ajax paging enabled?

Comments

dawehner’s picture

use can use this

<?php
print views_embed_view('list_comments', 'page_1', $args);
?>

where page_1 is your display id

jannalexx’s picture

print views_embed_view('yourview', 'page_1', $args);
same issue here

print views_embed_view does not enable ajax paging, is that true?
same view as a block or panel has ajax paging enabled in the same page (front page)..
using print views_embed_view in page-front.tpl.php does not enable ajax paging in those embedded views.

Any ideas?

jannalexx’s picture

Title: Embed view with AJAX paging » Disabled AJAX paging with embedded views when used in page.tpl.php
Version: 6.x-2.3 » 6.x-2.5

update

node.tpl.php is OK
we can have ajax paging with print views_embed_view

using it in..

page.tpl.php (or page-front.tpl.php) ajax paging is disabled

has anyone experienced this issue?

dawehner’s picture

Status: Active » Fixed

this is clear!

because $scripts is build there.

use the preprocess_page to add the output and add $vars['scripts'] = drupal_get_js(); at the end of the preprocess_page function

jannalexx’s picture

very well, in template.php (at the end)

function yourtheme_preprocess_pagefront(&$vars)
    {
	$vars['scripts'] = drupal_get_js();
	return $vars;
}

thats it, thanks

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

TimG1’s picture

Version: 6.x-2.5 » 6.x-2.11
Status: Closed (fixed) » Active

I'm using Views 6.x-2.11 is there something different I have to do for this version?

I have a page.tpl.php file that I'm trying to embed a view on...

print views_embed_view('Stories','block_2', $arg);

I've tried adding $vars['scripts'] = drupal_get_js(); like this...

function mytheme_preprocess_page(&$vars, $hook) {
$vars['scripts'] = drupal_get_js();
return $vars;
}

...in my template.php. I'm still not getting any ajaxy paging. Am I missing something?

Thanks for reading,
-Tim

merlinofchaos’s picture

Status: Active » Fixed

By doing this in page.tpl.php the problem is that the $scripts variable has already rendered, and so no new javascript is being output.

You could move the code to template.php, put it in your THEMENAME_preprocess_page() and stick it in a variable. Then, after, you've run your embed, you could reset the $scripts value (see template_preprocess_page() for the precise code that sets that up. I thikn it's just drupal_get_js()).

TimG1’s picture

Ah ha! Moving the view embed to the template.php was the piece I was missing, thanks!

Here's what my template_preprocess_page in my template.php looks like now...

function mytheme_preprocess_page(&$vars, $hook) {
$vars['view_stories'] = views_embed_view('Stories','block_2', $arg);
$vars['scripts'] = drupal_get_js();
return $vars;
}

Then in page.tpl.php print out view_stories...

print $view_stories;

Thanks again!
-Tim

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

mag2000’s picture

The following code is running and i do not have the ajax problem as well. But when i use the same code in the page which having the form for user to enter the company name and date selection and then view display on the screen. It display fine, but when i click the next button it is not working.The reason is that page button have the url like following. Any body have the clue.

?q=node/45&page=1&selcomp=abc&dfrom=2001/01/01&dto=2002/0/01&submit=submit1;
instead of
?q=node/45&page=1;

Code :

  $abc = array($comp) ;
   $view = views_get_view('myview;);

    $display_id = 'default';
    $view->set_display($display_id);
    //print $view->preview('default', $abc);

    $item = $view->get_item($display_id, 'filter', 'unix_trandate');
    $item['value'] = array('value'=>$dfrom);     
    $view->set_item($display_id, 'filter', 'unix_trandate', $item);

    $item1 = $view->get_item($display_id, 'filter', 'unix_trandate_1');
    $item1['value'] = array('value'=>$dto);     
    $view->set_item($display_id, 'filter', 'unix_trandate_1, $item1);

$view->set_arguments($abc);

  $viewsoutput = $view->render();
    print $viewsoutput;
mag2000’s picture

I moved the value of user selection to session variable and those pass to view and now it is working...

stefan81’s picture

Hi merlinofchaos,
Thank you for your explanation at #8, it work out!

An other performance related question:
By moving the views_embed_view to template.php, is the view executed on every page load, or only when the variable is calling for it?

deadcyclo’s picture

Forget it... I posted in the wrong thread.

fourmi4x’s picture

@stefan, if you make it conditional, like:

if($vars['node']->type == 'mytype') {
	$vars['template_files'][] = 'page-mytype';
	$vars['my_view'] = views_embed_view('my_view', 'block_1', $vars['node']->nid);
	$vars['scripts'] = 'drupal_get_js()';
	return $vars;
}

=> it will be loaded only when you want it.

About the general issue, I added something to http://drupalcontrib.org/api/drupal/contributions--views--views.module/f..., because I struggled a lot to track that down, all the more since I was investigating the problem because of the exposed filters and not the pager. But maybe this should be explained somewhere else in the documentation?

karljohann’s picture

How would I go about doing this if I wanted to embed a view in a module?