From 27b3e57eb9be39b91925dfb67cf0b8970396388c Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 7 Nov 2014 00:17:34 +0100 Subject: [PATCH] finished templating chapter --- 9-templating.md | 101 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/9-templating.md b/9-templating.md index 20a52ed..4c05168 100644 --- a/9-templating.md +++ b/9-templating.md @@ -31,10 +31,109 @@ In there create a new interface `Renderable.php` that looks like this: namespace Example\Template; -interface Renderable +interface Engine { public function render($template, $data = []); } ``` +Now that this is sorted out, let's create the mustache adapter class. In the same folder, create the file `MustacheEngineAdapter.php` with the following content: + +```php +engine = $engine; + } + + public function render($template, $data = []) + { + return $this->engine->render($template, $data); + } +} +``` + +As you can see the adapter is really simple. While the original class had a lot of methods, our adapter is really simple and only fulfills the interface. + +Of course we also have to add a definition in our `Dependencies.php` file because otherwise the injector won't know which implementation he has to inject when you hint for the interface. Add this line: + +`$injector->alias('Example\Template\Engine', 'Example\Template\MustacheEngineAdapter');` + +Now in your `HelloWorldPresenter`, add the new dependency like this: + +```php +request = $request; + $this->response = $response; + $this->templateEngine = $templateEngine; + } + +... +``` + +As you can see I imported the engine with an alias. Without the full namespace it would be relatively unclear what a class does if it is just referenced by `Engine`. Also, another part of the application might also have a class with the name `Engine`. So to avoid that I give it a short and descriptive alias. + +We also have to rewrite the `hello` method. Please note that while we are just passing in a simple array, Mustache also gives you the option to pass in a view context object. We will go over this later, for now let's keep it as simple as possible. + +```php + public function hello() + { + $data = [ + 'name' => $this->request->getParameter('name', 'stranger'), + ]; + $content = $this->templateEngine->render('Hello {{name}}', $data); + $this->response->setContent($content); + } +``` + +Now go check quickly in your browser if everything works. By default Mustache uses a simple string handler. But what we want is template files, so let's go back and change that. + +To make this change we need to pass an options array to the `Mustache_Engine` constructor. So let's go back to the `Dependencies.php` file and add the following code: + +```php +$injector->define('Mustache_Engine', [ + ':options' => [ + 'loader' => new Mustache_Loader_FilesystemLoader(dirname(__DIR__).'/templates'), + ], +]); +``` + +In your project root folder, create a `templates` folder. In there, create a folder `HelloWorld` and in there a file `Hello.mustache`. The content of the file should look like this: + +``` +

Hello World

+Hello {{ name }} +``` + +Now you can go back to your `HelloWorldPresenter` and change the render line to `$content = $this->templateEngine->render('HelloWorld/Hello', $data);` + +Navigate to the hello page in your browser to make sure everything works. And as always, don't forget to commit your changes. + [<< previous](8-dependency-injector.md) \ No newline at end of file