是否可以使用模板文件为 AJAX 调用返回 HTML?

发布于 2024-12-19 18:11:32 字数 492 浏览 3 评论 0原文

我正在开发的一个网站广泛使用 AJAX 来延迟加载页面数据并进行 Twitter 风格的分页。我真的很希望能够通过模板文件呈现 HTML,因为它比在 PHP 函数中构建 HTML 字符串更容易编码和维护。

有没有某种方法可以从数据库获取数据并将其传递给加载 tpl 文件的主题函数?


解决方案: 我如何在主题(' node', $node) 和 drupal_render($node->content) 用于编程 $node 输出

$node = node_load($nid);
$node_view = node_view($node);
echo drupal_render($node_view);

A site I'm working on uses AJAX extensively to lazy load page data and to do Twitter style paging. I'd really like to be able to render the HTML via a template file as it will be easier to code and maintain than building an HTML string in a PHP function.

Is there some way to get the data from the DB and pass it to a theme function that loads a tpl file?


Solution:
How do I decide between theme('node', $node) and drupal_render($node->content) for programmatic $node output

$node = node_load($nid);
$node_view = node_view($node);
echo drupal_render($node_view);

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

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

发布评论

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

评论(1

奶气 2024-12-26 18:11:32

是的,你可以。

Drupal 7 AJAX 需要一个回调,该回调需要返回已更新且需要返回到浏览器的表单元素,或者包含 HTML 的字符串或自定义 Ajax 命令的数组。

AJAX 命令之一是 ajax_command_html(),您可以使用它插入使用模板从主题函数返回的 HTML。

您可以使用类似于以下代码的代码:

function mymodule_ajax($form, &$form_state) {
  $form = array();
  $form['changethis'] = array(
    '#type' => 'select',
    '#options' => array(
      'one' => 'one',
      'two' => 'two',
      'three' => 'three',
    ),
    '#ajax' => array(
      'callback' => 'mymodule_ajax_callback',
      'wrapper' => 'replace_div',
     ),
  );

  // This entire form element will be replaced with an updated value.
  $form['html_div'] = array(
    '#type' => 'markup',
    '#prefix' => '<div id="replace_div">',
    '#suffix' => '</div>',
  );
  return $form;
}

function mymodule_ajax_callback($form, $form_state) {
  return theme('mymodule_ajax_output', array());
}

主题函数在 hook_theme() 中定义,如以下代码所示

function mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'mymodule_ajax_output' => array(
      'variables' => array(/* the variables that will be passed to the template file */), 
      'template' => 'mymodule-ajax-output',
    ),  
  );
}

要注意的是,模板文件名必须与主题函数的名称匹配;您可以在主题函数名称使用下划线的情况下使用连字符,但您不能拥有名为“foo”的主题函数,该函数使用“bar”作为模板文件的名称。
hook_theme() 报告的模板文件名称不包含在查找模板文件时从 Drupal 添加的扩展名(“.tpl.php”)。

Yes, you can.

Drupal 7 AJAX requires a callback that needs to return the form element that has been updated and needs to be returned to the browser, or alternatively, a string containing HTML, or an array of custom Ajax commands.

One of the AJAX commands is ajax_command_html(), which you can use to insert the HTML returned from a theme function using a template.

You could have code similar to the following one:

function mymodule_ajax($form, &$form_state) {
  $form = array();
  $form['changethis'] = array(
    '#type' => 'select',
    '#options' => array(
      'one' => 'one',
      'two' => 'two',
      'three' => 'three',
    ),
    '#ajax' => array(
      'callback' => 'mymodule_ajax_callback',
      'wrapper' => 'replace_div',
     ),
  );

  // This entire form element will be replaced with an updated value.
  $form['html_div'] = array(
    '#type' => 'markup',
    '#prefix' => '<div id="replace_div">',
    '#suffix' => '</div>',
  );
  return $form;
}

function mymodule_ajax_callback($form, $form_state) {
  return theme('mymodule_ajax_output', array());
}

The theme function is defined in hook_theme() as in the following code:

function mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'mymodule_ajax_output' => array(
      'variables' => array(/* the variables that will be passed to the template file */), 
      'template' => 'mymodule-ajax-output',
    ),  
  );
}

 

To notice that the template filename must match the name of the theme function; you can use hyphens where the theme function name uses underscores, but you cannot have a theme function named "foo" that uses "bar" as name of the template file.
The name of the template file reported from hook_theme() doesn't include the extension (".tpl.php") that is added from Drupal when looking for the template file.

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