在 Drupal 7 中呈现查询结果的正确方法是什么?

发布于 2024-11-01 04:12:29 字数 377 浏览 1 评论 0原文

我生成了一个查询,如下所示,并将结果格式化为链接:

$result = db_query("SELECT name FROM {taxonomy_term_data} WHERE vid = :val", array(':val' => '1'));
  $list = array();
  foreach ($result as $record) {
    $list[] = l($record->name, 'blog/' . $record->name);
  }

现在我想将此数组呈现为无序列表并将其返回到块。执行此操作的正确函数/语法是什么?

另外,与渲染相关的功能在哪里可以参考?

预先感谢您的任何帮助!

I've generated a query, as follows, and formatted the results as links:

$result = db_query("SELECT name FROM {taxonomy_term_data} WHERE vid = :val", array(':val' => '1'));
  $list = array();
  foreach ($result as $record) {
    $list[] = l($record->name, 'blog/' . $record->name);
  }

Now I would like to render this array as an unordered list and return it to a block. What's the proper function/syntax for doing this?

Also, where is a good reference for functions related to rendering?

Thanks in advance for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫雨熙 2024-11-08 04:12:29

请注意,“呈现查询结果的正确方法”并不存在,有很多方法。它们可以呈现为列表、表格和许多其他方式。您要求的是呈现链接列表的正确方法,这些链接来自数据库是不相关的。

请参阅http://api.drupal.org/api /drupal/includes--theme.inc/function/theme_links/7。您还可以使用所谓的可渲染数组,而不是直接调用 theme(),这是 Drupal 7 中的一项新功能,也是现在执行此操作的首选方法。

$result = db_query("SELECT name FROM {taxonomy_term_data} WHERE vid = :val", array(':val' => '1'));
// Prepare renderable array, define which theme function shall be used.
// The other properties match the arguments of that theme function.
$list = array(
  '#theme' => 'links',
  '#links' => array(),
);
foreach ($result as $record) {
  // Add each link to the array.
  $list['#links'][] = array('title' => $record->name, 'href' => 'blog/' . $record->name));
}
// Now you can call drupal_render() and return or print that result.
// If this is inside a block or page callback, you can also directly return 
// $list and Drupal will call drupal_render() automatically when the rest of 
// the page is rendered.
return drupal_render($list);

Note that "the proper way to render a query result" does not exist, there are many ways. They could be rendered as a list, as a table and many other ways. What you are asking for is the proper way to render a list of links, that these links are coming from the database is not relevant.

See http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_links/7. And instead of calling theme() directly, you can also use the so called renderable arrays which are a new feature in Drupal 7 and the preferred way to do this now.

$result = db_query("SELECT name FROM {taxonomy_term_data} WHERE vid = :val", array(':val' => '1'));
// Prepare renderable array, define which theme function shall be used.
// The other properties match the arguments of that theme function.
$list = array(
  '#theme' => 'links',
  '#links' => array(),
);
foreach ($result as $record) {
  // Add each link to the array.
  $list['#links'][] = array('title' => $record->name, 'href' => 'blog/' . $record->name));
}
// Now you can call drupal_render() and return or print that result.
// If this is inside a block or page callback, you can also directly return 
// $list and Drupal will call drupal_render() automatically when the rest of 
// the page is rendered.
return drupal_render($list);
寂寞花火° 2024-11-08 04:12:29

这是一种方法。构建 $vars 数组并将其传递给 theme_item_list($vars)

  $vars['items'] = $list;
  $vars['title'] = 'Sort entries by category';
  $vars['type'] = 'ul'; 
  $vars['attributes'] = array(
    'id' => 'blog-taxonomy-block',
  ); 

  $content = theme_item_list($vars);

  return $content;

http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_item_list/7

Here's one way to do it. Build $vars array and pass it to theme_item_list($vars):

  $vars['items'] = $list;
  $vars['title'] = 'Sort entries by category';
  $vars['type'] = 'ul'; 
  $vars['attributes'] = array(
    'id' => 'blog-taxonomy-block',
  ); 

  $content = theme_item_list($vars);

  return $content;

http://api.drupal.org/api/drupal/includes--theme.inc/function/theme_item_list/7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文