Last active
November 12, 2020 21:02
-
-
Save Edarlingen/19a048eddfcf1bf641e8cc028b3f4120 to your computer and use it in GitHub Desktop.
Использование дополнительных шаблонов внутри компонента
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
// Имя дополнительного шаблона | |
$partial_template_name = 'Test'; | |
// Данные для передачи в дополнительный шаблон | |
$data = array('example' => 'string'); | |
// Переменная с набором свойств текущего шаблона | |
global $template_env; | |
// Функция определяет корневой родительский шаблон для указанного шаблона | |
// Это необходимо, поскольку дополнительные шаблоны можно создать только для корневых шаблонов | |
function get_root_parent_template_id($id = null) { | |
$nc_core = nc_core::get_object(); | |
$parent_template_id = $nc_core->db->get_var('SELECT Parent_Template_ID FROM Template WHERE Template_ID = ' . +$id); | |
return $parent_template_id ? get_root_parent_template_id($parent_template_id) : $id; | |
} | |
// ID родительского корневого шаблона | |
$template_id = get_root_parent_template_id($template_env['Template_ID']); | |
// Путь к папке с шаблонами на сервере | |
$root_template_path = $nc_core->TEMPLATE_FOLDER; | |
// Путь к папке родительского корневого шаблона | |
$template_path = $nc_core->db->get_var('SELECT File_Path FROM template WHERE Template_ID = ' . +$template_id); | |
// Инициализируем базовый класс для шаблонов | |
$template_view = new nc_tpl_template_view($root_template_path, $nc_core->db); | |
// Загружаем искомый родительский шаблон | |
$template_view->load_template($template_id, $template_path); | |
// Ищем внутри искомого родительского шаблона дополнительный шаблон с указанным именем | |
$partial_template_view = $template_view->partial($partial_template_name, $data); | |
// Выводим результат выполнения дополнительного шаблона с указанным именем | |
echo $partial_template_view->make(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Вариант покороче: