|
1 | | -<?php |
2 | | -/** |
3 | | - * Slim - a micro PHP 5 framework |
4 | | - * |
5 | | - * @author Josh Lockhart |
6 | | - * @author Andrew Smith |
7 | | - * @link http://www.slimframework.com |
8 | | - * @copyright 2013 Josh Lockhart |
9 | | - * @version 0.1.2 |
10 | | - * @package SlimViews |
11 | | - * |
12 | | - * MIT LICENSE |
13 | | - * |
14 | | - * Permission is hereby granted, free of charge, to any person obtaining |
15 | | - * a copy of this software and associated documentation files (the |
16 | | - * "Software"), to deal in the Software without restriction, including |
17 | | - * without limitation the rights to use, copy, modify, merge, publish, |
18 | | - * distribute, sublicense, and/or sell copies of the Software, and to |
19 | | - * permit persons to whom the Software is furnished to do so, subject to |
20 | | - * the following conditions: |
21 | | - * |
22 | | - * The above copyright notice and this permission notice shall be |
23 | | - * included in all copies or substantial portions of the Software. |
24 | | - * |
25 | | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
26 | | - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
27 | | - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
28 | | - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
29 | | - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
30 | | - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
31 | | - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
32 | | - */ |
33 | | -namespace Slim\Views; |
34 | | - |
35 | | -/** |
36 | | - * SmartyView |
37 | | - * |
38 | | - * The SmartyView is a custom View class that renders templates using the Smarty |
39 | | - * template language (http://www.smarty.net). |
40 | | - * |
41 | | - * Two fields that you, the developer, will need to change are: |
42 | | - * - parserDirectory |
43 | | - * - parserCompileDirectory |
44 | | - * - parserCacheDirectory |
45 | | - * |
46 | | - * @package Slim |
47 | | - * @author Jose da Silva <http://josedasilva.net> |
48 | | - */ |
49 | | -class Smarty extends \Slim\View |
50 | | -{ |
51 | | - /** |
52 | | - * @var string The path to the Smarty code directory WITHOUT the trailing slash |
53 | | - */ |
54 | | - public $parserDirectory = null; |
55 | | - |
56 | | - /** |
57 | | - * @var string The path to the Smarty compiled templates folder WITHOUT the trailing slash |
58 | | - */ |
59 | | - public $parserCompileDirectory = null; |
60 | | - |
61 | | - /** |
62 | | - * @var string The path to the Smarty cache folder WITHOUT the trailing slash |
63 | | - */ |
64 | | - public $parserCacheDirectory = null; |
65 | | - |
66 | | - /** |
67 | | - * @var SmartyExtensions The Smarty extensions directory you want to load plugins from |
68 | | - */ |
69 | | - public $parserExtensions = array(); |
70 | | - |
71 | | - /** |
72 | | - * @var parserInstance persistent instance of the Parser object. |
73 | | - */ |
74 | | - private $parserInstance = null; |
75 | | - |
76 | | - /** |
77 | | - * Render Template |
78 | | - * |
79 | | - * This method will output the rendered template content |
80 | | - * |
81 | | - * @param string $template The path to the template, relative to the templates directory. |
82 | | - * @param null $data |
83 | | - * @return string |
84 | | - */ |
85 | | - public function render($template, $data = null) |
86 | | - { |
87 | | - $parser = $this->getInstance(); |
88 | | - $parser->assign($this->all()); |
89 | | - |
90 | | - return $parser->fetch($template, $data); |
91 | | - } |
92 | | - |
93 | | - /** |
94 | | - * Creates new Smarty object instance if it doesn't already exist, and returns it. |
95 | | - * |
96 | | - * @throws \RuntimeException If Smarty lib directory does not exist |
97 | | - * @return \Smarty Instance |
98 | | - */ |
99 | | - public function getInstance() |
100 | | - { |
101 | | - if (! ($this->parserInstance instanceof \Smarty)) { |
102 | | - if (!class_exists('\Smarty')) { |
103 | | - if (!is_dir($this->parserDirectory)) { |
104 | | - throw new \RuntimeException('Cannot set the Smarty lib directory : ' . $this->parserDirectory . '. Directory does not exist.'); |
105 | | - } |
106 | | - require_once $this->parserDirectory . '/Smarty.class.php'; |
107 | | - } |
108 | | - |
109 | | - $this->parserInstance = new \Smarty(); |
110 | | - $this->parserInstance->template_dir = $this->getTemplatesDirectory(); |
111 | | - if ($this->parserExtensions) { |
112 | | - $this->parserInstance->addPluginsDir($this->parserExtensions); |
113 | | - } |
114 | | - if ($this->parserCompileDirectory) { |
115 | | - $this->parserInstance->compile_dir = $this->parserCompileDirectory; |
116 | | - } |
117 | | - if ($this->parserCacheDirectory) { |
118 | | - $this->parserInstance->cache_dir = $this->parserCacheDirectory; |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - return $this->parserInstance; |
123 | | - } |
124 | | -} |
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Slim - a micro PHP 5 framework |
| 4 | + * |
| 5 | + * @author Josh Lockhart |
| 6 | + * @author Andrew Smith |
| 7 | + * @link http://www.slimframework.com |
| 8 | + * @copyright 2013 Josh Lockhart |
| 9 | + * @version 0.1.3 |
| 10 | + * @package SlimViews |
| 11 | + * |
| 12 | + * MIT LICENSE |
| 13 | + * |
| 14 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 15 | + * a copy of this software and associated documentation files (the |
| 16 | + * "Software"), to deal in the Software without restriction, including |
| 17 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 18 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 19 | + * permit persons to whom the Software is furnished to do so, subject to |
| 20 | + * the following conditions: |
| 21 | + * |
| 22 | + * The above copyright notice and this permission notice shall be |
| 23 | + * included in all copies or substantial portions of the Software. |
| 24 | + * |
| 25 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 26 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 28 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 29 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 30 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 31 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 32 | + */ |
| 33 | +namespace Slim\Views; |
| 34 | + |
| 35 | +/** |
| 36 | + * SmartyView |
| 37 | + * |
| 38 | + * The SmartyView is a custom View class that renders templates using the Smarty |
| 39 | + * template language (http://www.smarty.net). |
| 40 | + * |
| 41 | + * Two fields that you, the developer, will need to change are: |
| 42 | + * - parserDirectory |
| 43 | + * - parserCompileDirectory |
| 44 | + * - parserCacheDirectory |
| 45 | + * |
| 46 | + * @package Slim |
| 47 | + * @author Jose da Silva <http://josedasilva.net> |
| 48 | + */ |
| 49 | +class Smarty extends \Slim\View |
| 50 | +{ |
| 51 | + /** |
| 52 | + * @var string The path to the Smarty code directory WITHOUT the trailing slash |
| 53 | + */ |
| 54 | + public $parserDirectory = null; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var string The path to the Smarty compiled templates folder WITHOUT the trailing slash |
| 58 | + */ |
| 59 | + public $parserCompileDirectory = null; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var string The path to the Smarty cache folder WITHOUT the trailing slash |
| 63 | + */ |
| 64 | + public $parserCacheDirectory = null; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var SmartyExtensions The Smarty extensions directory you want to load plugins from |
| 68 | + */ |
| 69 | + public $parserExtensions = array(); |
| 70 | + |
| 71 | + /** |
| 72 | + * @var parserInstance persistent instance of the Parser object. |
| 73 | + */ |
| 74 | + private $parserInstance = null; |
| 75 | + |
| 76 | + /** |
| 77 | + * Render Template |
| 78 | + * |
| 79 | + * This method will output the rendered template content |
| 80 | + * |
| 81 | + * @param string $template The path to the template, relative to the templates directory. |
| 82 | + * @param null $data |
| 83 | + * @return string |
| 84 | + */ |
| 85 | + public function render($template, $data = null) |
| 86 | + { |
| 87 | + $parser = $this->getInstance(); |
| 88 | + $parser->assign($this->all()); |
| 89 | + |
| 90 | + return $parser->fetch($template, $data); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Creates new Smarty object instance if it doesn't already exist, and returns it. |
| 95 | + * |
| 96 | + * @throws \RuntimeException If Smarty lib directory does not exist |
| 97 | + * @return \Smarty Instance |
| 98 | + */ |
| 99 | + public function getInstance() |
| 100 | + { |
| 101 | + if (!($this->parserInstance instanceof \Smarty)) { |
| 102 | + if (!class_exists('\Smarty')) { |
| 103 | + if (!is_dir($this->parserDirectory)) { |
| 104 | + throw new \RuntimeException('Cannot set the Smarty lib directory : ' . $this->parserDirectory . '. Directory does not exist.'); |
| 105 | + } |
| 106 | + require_once $this->parserDirectory . '/Smarty.class.php'; |
| 107 | + } |
| 108 | + |
| 109 | + $this->parserInstance = new \Smarty(); |
| 110 | + $this->parserInstance->template_dir = $this->getTemplatesDirectory(); |
| 111 | + if ($this->parserExtensions) { |
| 112 | + $this->parserInstance->addPluginsDir($this->parserExtensions); |
| 113 | + } |
| 114 | + if ($this->parserCompileDirectory) { |
| 115 | + $this->parserInstance->compile_dir = $this->parserCompileDirectory; |
| 116 | + } |
| 117 | + if ($this->parserCacheDirectory) { |
| 118 | + $this->parserInstance->cache_dir = $this->parserCacheDirectory; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return $this->parserInstance; |
| 123 | + } |
| 124 | +} |
0 commit comments