-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.php
More file actions
64 lines (59 loc) · 2.27 KB
/
Copy pathView.php
File metadata and controls
64 lines (59 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace Slim\Views;
/**
* View
*
* This class is responsible for fetching and rendering a template with
* a given set of data. Although the `\Slim\View` class is itself
* capable of rendering PHP templates, it is highly recommended that you
* subclass `\Slim\View` for use with popular PHP templating libraries
* such as Twig, Smarty, or Mustache.
*
* If you do choose to create a subclass of `\Slim\View`, the subclass
* MUST override the `render` method with this exact signature:
*
* public render(string $template);
*
* The `render` method MUST return the rendered output for the template
* identified by the `$template` argument. The `$template` argument will
* contain the template file pathname *relative to* the templates base
* directory for the current view instance.
*
* The `Slim-Views` repository contains pre-made custom views for
* Twig and Smarty, two of the most popular PHP templating libraries.
*
* See: https://github.com/codeguy/Slim-Views
*
* Also, `\Slim\View` extends `\Slim\Container` so
* that you may use the convenient `\Slim\Container` interface just
* as you do with other Slim application data sets (e.g. HTTP headers,
* HTTP cookies, etc.)
*
* @package Slim
* @author Josh Lockhart
* @since 1.0.0
*/
class View extends \Slim\View
{
/**
* Render template
*
* @var string $template Pathname of template file relative to templates directory
* @return string The rendered template
* @throws \RuntimeException If resolved template pathname is not a valid file
*/
protected function render($template, array $data = array())
{
// Resolve and verify template file
$templatePathname = $this->templateDirectory . DIRECTORY_SEPARATOR . ltrim($template, DIRECTORY_SEPARATOR);
if (!is_file($templatePathname)) {
throw new \RuntimeException("Cannot render template `$templatePathname` because the template does not exist. Make sure your view's template directory is correct.");
}
$data = array_merge($this->all(), $data);
extract($data);
ob_start();
require $templatePathname;
// Return temporary output buffer content, destroy output buffer
return ob_get_clean();
}
}