From a99778b0495002f5102f791bc57ec368ad3f2755 Mon Sep 17 00:00:00 2001 From: Patrick Date: Wed, 14 Jan 2015 20:54:18 +0100 Subject: [PATCH] rename engine to renderer, solves #15 --- 09-templating.md | 28 +++++++++++++++------------- 10-dynamic-pages.md | 14 +++++++------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/09-templating.md b/09-templating.md index 4165ffe..68b895c 100644 --- a/09-templating.md +++ b/09-templating.md @@ -24,20 +24,20 @@ First let's define the interface that we want. Remember the [interface segregati So what does our template engine actually need to do? For now we really just need a simple `render` method. Create a new folder in your `src/` folder with the name `Template` where you can put all the template related things. -In there create a new interface `Engine.php` that looks like this: +In there create a new interface `Renderer.php` that looks like this: ```php alias('Example\Template\Engine', 'Example\Template\MustacheEngineAdapter');` +`$injector->alias('Example\Template\Renderer', 'Example\Template\MustacheRenderer');` Now in your `Homepage` controller, add the new dependency like this: @@ -77,29 +77,27 @@ namespace Example\Controllers; use Http\Request; use Http\Response; -use Example\Template\Engine as TemplateEngine; +use Example\Template\Renderer; class Homepage { private $request; private $response; - private $templateEngine; + private $renderer; public function __construct( Request $request, Response $response, - TemplateEngine $templateEngine + Renderer $renderer ) { $this->request = $request; $this->response = $response; - $this->templateEngine = $templateEngine; + $this->renderer = $renderer; } ... ``` -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 `show` 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 @@ -108,7 +106,7 @@ We also have to rewrite the `show` method. Please note that while we are just pa $data = [ 'name' => $this->request->getParameter('name', 'stranger'), ]; - $html = $this->templateEngine->render('Hello {{name}}', $data); + $html = $this->renderer->render('Hello {{name}}', $data); $this->response->setContent($html); } ``` @@ -132,7 +130,11 @@ In your project root folder, create a `templates` folder. In there, create a fil Hello {{ name }} ``` -Now you can go back to your `Homepage` controller and change the render line to `$content = $this->templateEngine->render('Homepage', $data);` +Now you can go back to your `Homepage` controller and change the render line to `$content = $this->renderer + + + +->render('Homepage', $data);` Navigate to the hello page in your browser to make sure everything works. And as always, don't forget to commit your changes. diff --git a/10-dynamic-pages.md b/10-dynamic-pages.md index 7a9ae70..7da2eb5 100644 --- a/10-dynamic-pages.md +++ b/10-dynamic-pages.md @@ -107,12 +107,12 @@ public function show($params) { $slug = $params['slug']; $data['content'] = $this->pageReader->getContentBySlug($slug); - $html = $this->templateEngine->render('Page', $data); + $html = $this->renderer->render('Page', $data); $this->response->setContent($html); } ``` -To make this work, we will need to inject a `Response`, `TemplateEngine` and a `PageReader`. I will leave this to you as an exercise. Remember to `use` all the proper namespaces. Use the `Homepage` controller as a reference. +To make this work, we will need to inject a `Response`, `Renderer` and a `PageReader`. I will leave this to you as an exercise. Remember to `use` all the proper namespaces. Use the `Homepage` controller as a reference. Did you get everything to work? @@ -124,22 +124,22 @@ If not, this is how the beginning of your controller should look now: namespace Example\Controllers; use Http\Response; -use Example\Template\Engine as TemplateEngine; +use Example\Template\Renderer; use Example\Page\PageReader; class Page { private $response; - private $templateEngine; + private $renderer; private $pageReader; public function __construct( Response $response, - TemplateEngine $templateEngine, + Renderer $renderer, PageReader $pageReader ) { $this->response = $response; - $this->templateEngine = $templateEngine; + $this->renderer = $renderer; $this->pageReader = $pageReader; } ... @@ -207,7 +207,7 @@ public function show($params) return $this->response->setContent('404 - Page not found'); } - $html = $this->templateEngine->render('Page', $data); + $html = $this->renderer->render('Page', $data); $this->response->setContent($html); } ```