phpwordpresssmartysmarty3

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


I am trying to use the smarty templating engine in my custom wordpress theme but I ended up getting the follosing error :

Fatal error: Uncaught --> Smarty: Unable to load template 'file:index.tpl' <-- thrown in wordpresspath\inc\sysplugins\smarty_internal_template.php on line 195

For the curious ones here is the line 195 of the 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;

After debugging the Error in my case is from this function Line 134 of 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);
}

So my theme folder hierarchy is simple I have the theme folder and inside I have :

mythemefolder/Inc : folder that contains required files for smarty
mythemefolder/templates : folder that contains smarty templates files
mythemefolder/index.php wordpress main index file

so inside the index.file I am trying to execute the smarty template by using the following php code :

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

From the error above I kind of got to the conclusion that Smarty doesnt find my template so I tried to set the template directory using:

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

Unfortunately it didn't work, Any ideas ??


Solution

  • So I wasn't using the correct absolute path in my work. Basically instead of using the Worpress API get_template_directory_uri() that was giving me the theme URL (correct for external stuff not an internal PHP server). I should use the get_theme_file_path() function.

    I ended up writing this code:

    <?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 );
    ?>