簡體   English   中英

無法在我的自定義 wordpress 主題中使用 smarty 模板引擎

[英]Can't use smarty templating engine in my custom wordpress theme

我試圖在我的自定義 wordpress 主題中使用 smarty 模板引擎,但我最終得到了以下錯誤:

致命錯誤:未捕獲 --> Smarty:無法加載模板 'file:index.tpl' <-- 在第 195 行的 wordpresspath\\inc\\sysplugins\\smarty_internal_template.php 中拋出

對於好奇的人,這里是 smarty_internal_template.php 的第 195 行:

  /**
     * 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;

在我的情況下調試錯誤后來自 smarty_internal_templatebase.php 的這個函數第 134 行:

    /**
 * 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/Inc :包含 smarty 所需文件的文件夾
mythemefolder/templates : 包含 smarty 模板文件的文件夾
mythemefolder/index.php wordpress 主索引文件

所以在 index.file 中,我嘗試使用以下 php 代碼執行 smarty 模板:

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

從上面的錯誤中,我得出的結論是 Smarty 找不到我的模板,因此我嘗試使用以下方法設置模板目錄:

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

不幸的是它沒有用,任何想法?

所以我沒有在我的工作中使用正確的絕對路徑 基本上不是使用給我主題 URL 的 Worpress API get_template_directory_uri()(對於外部內容而不是內部 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 );
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM