在模板或模块嵌入Views2的示范代码

官方文档:https://drupal.org/node/1655012

专门的模块:Embed Views Display

--------- last updated at 2013-8-8

 

最新views 3.x(drupal 7)的变动

例子: 不显示pager,仅显示6条记录,如下:

        $view->display_handler->set_option('pager', array('type'=>'some','options' => array('items_per_page' => 6)));

========================================================================

  $view = views_get_view('views_name');
  $view->set_display('default');
  $view->display_handler->set_option('items_per_page', 3);
  $view->display_handler->set_option('use_pager', FALSE);
  print $view->preview(NULL, array($arg));

要返回查询的行数,可调用:print $view->total_rows;

(要注意的是:当views使用pager才自动计算总行数;如果use_pager=FALSE,需强制指定:$view->get_total_rows = TRUE

 

 

其它的一些写法:

官方推荐的:print views_embed_view('view_name','default',arg(1));

单独指定Arguments:$view->set_arguments(array(arg1,arg2));

又一种写法:

$view = views_get_view('video');
print $view->execute_display('default', $args);

补充:views参数可另外指定:
$view->set_arguments(array($tid));
$view->preview();

文章分类: 
文章标签: 

评论

有个教学录像,老外就喜欢看录像~

http://gotdrupal.com/videos/drupal-views-arguments

聚沙成塔,聚流成河。

simon的头像


<?php
$v = views_get_view('moreNews');
$v->init_display('default');
$v->display_handler->set_option('items_per_page', 0);
$v->display_handler->set_option('offset', 2);
echo $v->execute_display();
?>

How do you turn this on?

Views导出的代码,可以进一步深入研究:

$view = new stdClass();
$view->name = 'Archive';
$view->descrīption = 'Monthly Archive';
$view->access = array (
0 => '1',
1 => '2',
);
$view->view_args_php = '';
$view->page = TRUE;
$view->page_title = '按月归档';
$view->page_header = '';
$view->page_header_format = '4';
$view->page_footer = '';
$view->page_footer_format = '4';
$view->page_empty = '';
$view->page_empty_format = '4';
$view->page_type = 'teaser';
$view->url = 'archive';
$view->use_pager = TRUE;
$view->nodes_per_page = '50';
$view->block = TRUE;
$view->block_title = 'Blog 归档';
$view->block_header = '';
$view->block_header_format = '4';
$view->block_footer = '';
$view->block_footer_format = '4';
$view->block_empty = '';
$view->block_empty_format = '4';
$view->block_type = 'list';
$view->nodes_per_block = '5';
$view->block_more = TRUE;
$view->block_use_page_header = FALSE;
$view->block_use_page_footer = FALSE;
$view->block_use_page_empty = FALSE;
$view->sort = array (
array (
'tablename' => 'node',
'field' => 'created',
'sortorder' => 'DESC',
'options' => 'normal',
),
);
$view->argument = array (
array (
'type' => 'monthyear',
'argdefault' => '5',
'title' => '按月归档',
'options' => '',
'wildcard' => '',
'wildcard_substitution' => '',
),
);
$view->field = array (
array (
'tablename' => 'node',
'field' => 'title',
'label' => '',
'handler' => 'views_handler_field_nodelink',
'options' => 'link',
),
);
$view->filter = array (
array (
'tablename' => 'node',
'field' => 'status',
'operator' => '=',
'options' => '',
'value' => '1',
),
array (
'tablename' => 'node',
'field' => 'type',
'operator' => 'OR',
'options' => '',
'value' => array (
0 => 'blog',
),
),
);
$view->exposed_filter = array (
);
$view->requires = array(node);
$views[$view->name] = $view;

simon的头像

<?php

    $view = views_get_view('Your_View_Name_HERE') or die ('no such view');
    $view->set_display('page_1');
    $view->execute('page_1');
    $plugin = $view->display_handler->get_cache_plugin();
    $cache = cache_get($plugin->get_output_key(), $plugin->table);
    $timestamp = $cache->created;
    $show_date = format_date($timestamp, 'custom', 'D M j Y G:i');
?>

<div style="margin: 10px 0pt; text-align: center; font-size: 90%;">
This page last updated:<br> <?=$show_date;?>
</div>

How do you turn this on?

simon的头像