是的,可以。
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”)。