Question

I have a class that sets up my smarty instances:

class View {
    protected $templateEngine;
    protected $templateExtension = '.tpl';


    public function __construct(){
        global $ABS_PUBLIC_PATH;
        global $ABS_PUBLIC_URL;
        $this->templateEngine = new Smarty();
        $this->templateEngine->error_reporting = E_ALL & ~E_NOTICE;
        $this->templateEngine->setTemplateDir($ABS_PUBLIC_PATH . '/templates/');
        $this->templateEngine->setCompileDir($ABS_PUBLIC_PATH . '/templates_c/');
        $this->templateEngine->assign('ABS_PUBLIC_URL', $ABS_PUBLIC_URL);       

        if(isset($_SESSION['loggedIn'])){
            $this->assign('session', $_SESSION);
        }
    }

    public function assign($key, $value){
        $this->templateEngine->assign($key, $value);
    }

    public function display($templateName){
         $this->templateEngine->display($templateName . $this->templateExtension);
    }

    public function fetch($templateName){
         $this->templateEngine->fetch($templateName . $this->templateExtension);
    }
}

Then in my functions, I use the class like this:

public function showMeSomething()
    {
        $view = new View();
        $view->assign('session', $_SESSION);
        $view->display('header');
        $view->display('index');
        $view->display('footer');
    }

Now, I'm trying to fetch some data into a variable, in order to send emails from my template files as well. Unfortunately, this var_dumps below (both of them) output NULL - even though the template file referenced has a lot of HTML in it. Furthermore, changing the word fetch to display below will correctly display the template file. So, the problem certainly lies within the fetch command. I'm not sure what to do to continue debugging.

function emailPrep($data,){
    $mailView = new View();

    $emailHTML = $mailView->fetch('myEmail');   
    var_dump($mailView->fetch("myEmail"));
    var_dump($emailHTML);
}
Was it helpful?

Solution

Your code must be

public function fetch($templateName){
     return $this->templateEngine->fetch($templateName . $this->templateExtension);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top