2011-03-15 5 views
0

그래서 .. "moon"이라는 모듈을 만들었습니다. 모듈에서 moon_menu를 사용하여 moon_page를 호출하는 메뉴를 지정하여 브라우저에 무엇인가를 표시합니다.Drupal 7 - 템플릿에 변수를 할당하는 방법은 무엇입니까?

function moon_page(){ 

$moonsvariable = 'hi this is a function'; 
return theme('moon_display',$moonsvariable); 

} 

그 다음 템플릿에 사용자 정의 변수를 할당 템플릿에서 사용할 수 있습니까?

다음과 같은 방식으로 알려드립니다.

function moon_page(){ 

    $custom_variable = "this is a custom variable"; 
    $moonsvariable = 'hi this is a function'; 
    return theme('moon_display',$moonsvariable); 

} 

그럼 내 테마에는 <? print $custom_variable ?>을 사용하고 싶습니다.

나는 variable_set을 시도했지만 작동하지 않습니다.

+0

나는 혼란 스럽다. 왜'$ custom_variable'을 테마 함수에 넘겨주지 않습니까? –

+0

@Karl Bielefeldt // then ... 내 테마가 출력했습니다. 나는 실제로 배열을 할당 한 다음 템플릿 파일에서 수동으로 반복하려고합니다. – Moon

답변

3
/* 
* Implementation of hook_theme(). 
*/ 
function moon_theme($existing, $type, $theme, $path){ 
    return array(
    'moon' => array(
     'variables' => array('content' => NULL), 
     'file' => 'moon', // place you file in 'theme' folder of you module folder 
     'path' => drupal_get_path('module', 'moon') .'/theme' 
    ) 
); 
} 

function moon_page(){ 

    // some code to generate $content variable as array 
    $content['data1'] = 'Lorem ipsum'; 
    $content['data2'] = 'Ipsum lorem'; 

    return theme('moon', $content); // use $content variable in moon.tpl.php template 

    // Or you can use few 'variables' in hook_theme function 
    // smth like this 'variables' => array('content1' => NULL, 'content2' => NULL) 
    // and return page as theme('moon', var1, var2) 
} 
관련 문제