2016-10-03 50 views
1

我是新來的主持,我只是繼承了這段代碼。我需要控制在何處以及如何打印#output,但它始終顯示在頁面頂部,位於HTML標記上方。我查看了可渲染的數組API,但找不到特定於我的問題的任何內容。如何從內容數組屬性中輸出html?

在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; ?> 

但它被忽略。如何控制#輸出,以便我可以設計它?

+0

你能發佈更完整的代碼集,特別是my_theme.tpl.php嗎? – Matz

+0

不是真的:(但其他應該沒關係,因爲我只關心打印$輸出。 – user3052659

回答

0

不太確定,但您可能需要直接調用數組元素。喜歡的東西:

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

希望它可以幫助

+0

我試過<?php $ vars = get_defined_vars(); krumo($ vars);?>但$ output是空,即使它正在打印在頁面上,在DOCTYPE標籤上面....超級困惑。 – user3052659

0

使用哪個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', 
    ... 

記住事後清除緩存。