2011-10-07 111 views
2

我意識到這個問題已被問到,但我要麼根本不明白或以前的答案不適用(或者我不明白如何應用它們)對我的情況。這裏所說:Drupal:從自定義模塊傳遞自定義變量到我的模板

我有一個名爲自定義模塊:

中/網站/所有/模塊/自定義/ my_module 「my_module」

我有一個模塊文件:

/網站/所有/modules/custom/my_module/my_module.module

我有一個頁面模板名稱「頁,我的空間」,這是不是我的模塊中:

/網站/所有/主題/ 的MyTheme /頁 /page-mypath-mypage.tpl.php

我給這個鉤子菜單:

$items['mypath/mypage'] = array(
    'title'   => 'My Page!', 
    'page callback'   => 'my_module_mypage', 
    'page arguments' => array(1,2), 
    'access callback' => true, 
    'type'   => MENU_CALLBACK, 
); 

在功能方面,我建立了一些內容,像這樣:

function my_module_mypage($x, $y) { 
    $output = "foo AND bar!"; 
    return $output; 
} 

在模板(再次,而不是在我的模塊文件夾,但在主題子文件夾「頁」,我有:

<?php print $content ?> 

當我去http://mysite/mypath/mypage我得到「富和酒吧!」

現在的問題。我想要一個名爲'$ moar_content'的my_module_mypage()中定義的新變量。我想在我的頁面輸出$ moar_content-mypath-mypage.tpl.php。我只需要爲此模塊和此模板執行此操作。我不需要它在主題範圍內,所以我不認爲使用mytheme的'template.php'是適當的。

我想我需要使用某種預處理,但我嘗試的所有事情都失敗了,而且我讀的所有東西似乎都缺少某種神奇的成分。

我的想法是:

function my_module_preprocess_page_mypath_mypage(&$variables) { 
    $variables['moar_content'] = 'OATMEAL'; 
} 

function my_module_preprocess_my_module_mypage(&$variables) { 
    $variables['moar_content'] = 'OATMEAL'; 
} 

什麼的。我很確定自己走在正確的軌道上,但是我正在打磚牆。

+0

巨大取決於你使用的Drupal的版本 - 6或7? – Clive

+0

Drupal 6.對不起,我應該提前說過。 – Spanky

回答

1

做的工作,你必須遵循Drupal的最佳實踐,假定你正在使用D6,讓您可以插入一些變量到您的模板是這樣的:

// You menu path is good 
$items['mypath/mypage'] = array(
    'title' => 'My Page!', 
    'page callback' => 'my_module_mypage', 
    'page arguments' => array(1,2), 
    'access callback' => true, 
    'type' => MENU_CALLBACK, 
); 

第二件事,我們定義爲主題掛鉤我們的頁面

// We define here a new theme file for your your page 
// Your theme file must be located in your module's folder 
// you can use a subfolder to group all your module's theme files 
// E.g : themes/my-module-theme.tpl.php 
// Note that in theme files, we change _ by - 
function my_module_theme() { 
    return array(
     'my_module_theme' => array(// Keep that name in your mind 
      'template' => 'my_module_theme', 
       'arguments' => array(
       'my_var' => NULL, 
       'my_var2' => NULL, 
      ), 
     ) 
    ); 
} 

現在我們可以創建我們的模塊的根文件夾中的文件「我的模塊,theme.tpl.php」,並貼上類似「foo和吧!」 回到我們的my_module。模塊,回調應該是這樣的:

function my_module_mypage($x, $y) { 
    // $x and $y are optionnal, so this is the first manner 
    // to inject variables into your theme's file 
    $output = theme("my_module_theme", $x, $y); 
    return $output; 
} 

您也可以使用預處理掛鉤插入變量

// The hook must be named like this : template_preprocess_NAME_OF_THEME_FILE 
// where NAME_OF_THEME_FILE is the name that you kept in your mind ;) 
function template_preprocess_my_module_theme(&$variables) { 
    // Do some job 
    $var1 = 'Foobar'; 

    // So in "my-module-theme.tpl.php", $my_var1 will print Foobar 
    $variables['my_var1'] = $var1; 
} 
+0

我的頁面模板不在我的模塊目錄中。他們在主題/ mytheme /頁面。其中很多。不幸的是,這就是交給我的東西,所以我必須與之合作。 – Spanky

+0

對不起,但這個答案不回答我的問題。這是我能在其他地方找到的答案。如果我按照這些說明操作,那麼我的模塊文件夾中會有一個模板,但我仍然無法在/ sites/all/themes/mytheme/pages/page-mypage中的頁面模板上下文中呈現該模板。 tpl.php - 所以我仍然陷入困境。謝謝,雖然:) – Spanky