anandkunal / ToroPHP

Toro is a PHP router for developing RESTful web applications and APIs.
http://toroweb.org
MIT License
1.17k stars 173 forks source link

Is there a better way to pass the base_url variable to mustache template file? #67

Closed phoenixg closed 11 years ago

phoenixg commented 11 years ago
class testHandler extends baseHandler {
    function get() {
        $tpl = $this->mustache->loadTemplate('test');
        echo $tpl->render(array('a'=>$a,
                                'base_url' => BASE_URL));
    }
}

I have dozens of xHandler, so pass the base_url every time seems not elegant way.

martinbean commented 11 years ago

I’ve not used the PHP implementation of Mustache (just the JavaScript version), but if you’re using a base handler class, maybe set some site-wide variables in there, and then add to them in your extending handler classes? Something like this:

abstract class BaseHandler {
    protected $templateVariables;

    public function __construct() {
        $this->templateVariables = array(
            'base_url' => BASE_URL
        );
    }
}

class TestHandler extends BaseHandler {
    public function get() {
        $tpl = $this->mustache->loadTemplate('test');

        $this->templateVariables['foo'] = 'bar';

        echo $tpl->render($this->templateVariables);
    }
}

Let me know how that works for you. I’m closing this issue as it’s more to do with Mustache and not ToroPHP itself.

phoenixg commented 10 years ago

Hi, this works, I use this method now, thanks!