1 | <?php |
||
31 | class MiniView implements ViewRendererInterface, OwnerAwareInterface, RendererAwareInterface |
||
32 | { |
||
33 | |||
34 | use Traits\OwnerAwareTrait, |
||
35 | Traits\RendererAwareTrait; |
||
36 | |||
37 | public $renderers = [ |
||
38 | 'latte' => LatteRenderer::class, |
||
39 | 'php' => PhpRenderer::class, |
||
40 | 'twig' => TwigRenderer::class |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Current version |
||
45 | * @var string |
||
46 | */ |
||
47 | private static $version = null; |
||
48 | |||
49 | /** |
||
50 | * View path |
||
51 | * @var string |
||
52 | */ |
||
53 | private $path = ''; |
||
54 | |||
55 | /** |
||
56 | * View path. This is relative to base path. |
||
57 | * @var string |
||
58 | */ |
||
59 | private $viewsPath = 'views'; |
||
60 | |||
61 | /** |
||
62 | * Create MiniView instance. If path is not set, it will be based on location of owner class. |
||
63 | * @param object $owner |
||
64 | * @param string $path |
||
65 | */ |
||
66 | public function __construct($owner, $path = null) |
||
71 | |||
72 | /** |
||
73 | * Get current MiniView version |
||
74 | * @return string Version string |
||
75 | */ |
||
76 | public function getVersion() |
||
84 | |||
85 | /** |
||
86 | * Set views path. This is relative path for view resolving. |
||
87 | * By default it's `views` folder. |
||
88 | * @param string $path |
||
89 | * @return static |
||
90 | */ |
||
91 | public function setViewsPath($path) |
||
96 | |||
97 | /** |
||
98 | * Render view with data provided. |
||
99 | * View name may contain `php` extension. If no extension is passed or |
||
100 | * it not match extensions from renderer configuration, |
||
101 | * it will append extension based on current renderer. |
||
102 | * |
||
103 | * Example with default or previously set renderer: |
||
104 | * |
||
105 | * ```php |
||
106 | * $view->render('myView'); |
||
107 | * ``` |
||
108 | * |
||
109 | * Example with enforced php renderer: |
||
110 | * |
||
111 | * ```php |
||
112 | * $view->render('myView.php'); |
||
113 | * ``` |
||
114 | * |
||
115 | * Example with enforced latte renderer: |
||
116 | * |
||
117 | * ```php |
||
118 | * $view->render('myView.latte'); |
||
119 | * ``` |
||
120 | * |
||
121 | * @param string $view |
||
122 | * @param mixed[] $data |
||
123 | * @param bool $return |
||
124 | * @return string |
||
125 | */ |
||
126 | public function render($view, $data = null, $return = false) |
||
166 | |||
167 | /** |
||
168 | * Render file with data provided. |
||
169 | * @param string $file |
||
170 | * @param mixed[] $data |
||
171 | * @param bool $return |
||
172 | * @return string |
||
173 | */ |
||
174 | public function renderFile($file, $data = null, $return = false) |
||
178 | |||
179 | function getPath() |
||
188 | |||
189 | } |
||
190 |
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.
If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with
private
, and only raise it toprotected
if a sub-class needs to have access, orpublic
if an external class needs access.