【问题标题】:How to output html from content array property?如何从内容数组属性输出 html?
【发布时间】:2016-10-03 20:47:50
【问题描述】:

我是主题的新手,我只是继承了这段代码。我需要控制打印#output 的位置和方式,但它总是显示在页面顶部的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 以便我可以设置样式?

【问题讨论】:

  • 您能否发布更完整的代码集,尤其是来自 my_theme.tpl.php 的代码集?
  • 不是真的 :( 但其余的都不重要,因为我现在只关心打印 $output。

标签: php html drupal drupal-7


【解决方案1】:

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

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

希望对你有帮助

【讨论】:

  • 我试过 但是 $output 是空的,即使它被打印在页面上,在 DOCTYPE 标记上方....超级困惑。
【解决方案2】:

您使用哪个 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

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

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

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

记得之后清除缓存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 2015-09-02
    • 2017-09-19
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    相关资源
    最近更新 更多