Drupal 7 - Как загрузить файл шаблона из модуля?
Я пытаюсь создать собственный модуль в Drupal 7.
Итак, я создал простой модуль под названием "moon"
function moon_menu() {
$items = array();
$items['moon'] = array(
'title' => '',
'description' => t('Detalle de un Programa'),
'page callback' => 'moon_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
return $items;
}
function moon_page(){
$id = 3;
$content = 'aa';
}
в функции moon_page(), мне нравится загружать пользовательский шаблон 'moon.tpl.php' из моего файла темы.
Это возможно?
Ответы
Ответ 1
Для ваших собственных вещей (не переопределяя шаблон из другого модуля)?
Конечно, вам нужно только:
$args - это массив, содержащий аргументы шаблона, указанные в вашей реализации hook_theme().
Ответ 2
/*
* 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
return theme('moon', $content); // use $content variable in moon.tpl.php template
}
Ответ 3
Для Drupal 7 это не сработало для меня. Я заменил строку в hook_theme
'file' => 'moon', by 'template' => 'moon'
и теперь он работает для меня.
Ответ 4
В drupal 7 я получал следующую ошибку при использовании:
return theme('moon', $content);
В результате появилась "Неустранимая ошибка: неподдерживаемые типы операндов в drupal_install\include\theme.inc в строке 1071"
Это было исправлено с помощью:
theme('moon', array('content' => $content));
Ответ 5
Вы можете использовать moon_menu, с hook_theme
<?php
/**
* Implementation of hook_menu().
*/
function os_menu() {
$items['vars'] = array(
'title' => 'desc information',
'page callback' => '_moon_page',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function _moon_page() {
$fields = [];
$fields['vars'] = 'var';
return theme('os', compact('fields'));
}
/**
* Implementation of hook_theme().
*/
function os_theme() {
$module_path = drupal_get_path('module', 'os');
return array(
'os' => array(
'template' => 'os',
'arguments' => 'fields',
'path' => $module_path . '/templates',
),
);
}