html能使用ajax吗,是否可以使用模板文件为AJAX调用返回HTML?

是的,可以。

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' => '

',

'#suffix' => '

',

);

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”)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值