Не могу использовать умный шаблонизатор в моей собственной теме WordPress - PullRequest
0 голосов
/ 01 марта 2020

Я пытаюсь использовать умный шаблонизатор в моей пользовательской теме WordPress, но в итоге получаю следующую ошибку:

Неустранимая ошибка: Uncaught -> Smarty: Невозможно загрузить шаблон 'file : index.tpl '<- добавляется в wordpresspath \ inc \ sysplugins \ smarty_internal_template. php в строке 195 </p>

Для любопытных здесь используется строка 195 из smarty_internal_template. php:

  /**
     * flag if compiled template is invalid and must be (re)compiled
     *
     * @var bool
     */
    public $mustCompile = null;

    /**
     * Template Id                   <--------- Line 195!!
     *
     * @var null|string
     */
    public $templateId = null;

После отладки ошибка в моем случае происходит из этой функции Строка 134 smarty_internal_templatebase. php:

    /**
 * displays a Smarty template
 *
 * @param string $template   the resource handle of the template file or template object
 * @param mixed  $cache_id   cache id to be used with this template
 * @param mixed  $compile_id compile id to be used with this template
 * @param object $parent     next higher level of Smarty variables
 *
 * @throws \Exception
 * @throws \SmartyException
 */
public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
{
    // display template
    $this->_execute($template, $cache_id, $compile_id, $parent, 1);
}

Так что моя иерархия папок тем проста У меня есть папка тем и внутри у меня есть:

mythemefolder / In c: папка, содержащая необходимые файлы для smarty
mythemefolder / templates: папка, содержащая файлы шаблонов smarty
mythemefolder / index. php wordpress main индексный файл

, поэтому внутри index.file я пытаюсь выполнить шаблон smarty, используя следующий код php:

require 'inc/Smarty.class.php';
$smarty = new Smarty;
$smarty->display('index.tpl'); //index.tpl is my template file inside the templates folder

From ошибка выше я как бы пришел к выводу, что Smarty не находит мой шаблон, поэтому я попытался установить каталог шаблонов, используя:

$smarty->setTemplateDir(get_template_directory_uri().'/templates');
// I also tried these lines
//$smarty->template_dir = get_template_directory_uri().'/templates';
//$smarty->setTemplateDir('./templates');

К сожалению, это не сработало, Есть идеи ??

1 Ответ

0 голосов
/ 01 марта 2020

Так что я не использовал правильный абсолютный путь в своей работе. По сути, вместо использования Worpress API get_template_directory_uri (), который давал мне URL-адрес темы (верно для внешнего материала, а не для внутреннего PHP сервера). Я должен использовать функцию get_theme_file_path () .

В итоге я написал этот код:

<?php
require 'inc/smarty/Smarty.class.php';
$smarty = new Smarty;


//$smarty->assign("Assign_your_vars","with something");
//..

$smarty->setTemplateDir(get_theme_file_path().'/templates/');
$smarty->setCompileDir(get_theme_file_path().'/templates_c');
$smarty->setCacheDir(get_theme_file_path().'/cache');
$smarty->setConfigDir(get_theme_file_path().'/configs');
$your_template = get_theme_file_path().'/templates/your_template.tpl';
$smarty->display($your_template );
?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...