I've picked up PHPTAL (after coming from PHPMustache), and I'm trying to inject a ViewModel class Mustache-style into my template. Let me explain ..
controller
$viewmodel = new \Viewmodel\Home();
$template = new \PHPTAL('application/views/home.html');
$template->viewmodel = $viewmodel;
echo $template->execute();
home.html <- template
<p>Hello <strong>${viewmodel/test}</strong>.</p>
Home.php <- viewmodel class
namespace Viewmodel;
class Home {
function test() {
return "world";
}
}
The above works, but how do I avoid prefixing every variable reference with "viewmodel/"?
With Mustache, your variable context could be a single class. eg. echo $template->render($html, $viewmodel);
. Prefixing is not necessary. All the meat is inside a loosely coupled viewmodel. I'd like to enforce this behaviour with PHPTAL without having to explicitly assign the whole class to a variable name.