如何从内容数组属性输出html


How to output html from content array property?

我是新手,刚刚继承了这段代码。我需要控制在哪里以及如何打印#输出,但它总是显示在页面的顶部,上面的HTML标签。我看了可渲染数组API,但我找不到任何具体到我的问题。

在mytheme.module:

function mytheme_output_block() {
  $content = array(
    '#theme' => 'my_theme',
    '#output' => function_that_returns_JSON();
    ...
function mytheme_hook_theme() {
  return array(
    'my_theme' => array(
      'vars' => array(
      'output' => '',
    ...

在my_theme.tpl.php中我尝试:

<?php print $output; ?>

但是它被忽略了。我如何控制#output来设置样式?

不太确定,但您可能需要直接调用数组元素。比如:

$theme = mytheme_hook_theme();
echo $theme['output'];

希望有所帮助

您使用哪个Drupal ?我猜是D7。

查看hook_theme: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_theme/7.x的文档

  1. 不是vars,是variables
  2. 你在执行中错过了template

    template:如果指定,这个主题实现是一个模板,并且这是没有扩展名的模板文件。不要在这个文件中放入.tpl.php;该扩展将由默认渲染引擎(PHPTemplate)自动添加。如果上面指定了'path',模板也应该在这个路径中。

那么你的钩子应该是什么样子的呢:

function mytheme_hook_theme() {
  return array(
    'my_theme' => array(
      'variables' => array(
        'output' => '',
      ),
      'template' => 'my-theme',
    ...