PluginProbe
Instapage Plugin / 3.4.2
Instapage Plugin v3.4.2
trunk 3.2.11 3.2.12 3.2.13 3.2.14 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.10 3.5.11 3.5.12 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 All 28 releases
instapage / models / InstapageCmsPluginViewModel.php

InstapageCmsPluginViewModel.php in Instapage Plugin 3.4.2, at models/InstapageCmsPluginViewModel.php

175 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class responsible for displaying a template files.
5 */
6 class InstapageCmsPluginViewModel {
7
8 /**
9 * @var object Class instance.
10 */
11 private static $viewModel = null;
12
13 /**
14 * @var array Data that can be used inside of a template.
15 */
16 protected $templateData = array();
17
18 /**
19 * @var array Templates to fetch.
20 */
21 protected $templates = null;
22
23 /**
24 * Gets the class instance.
25 */
26 public static function getInstance() {
27
28 if (self::$viewModel === null) {
29 self::$viewModel = new InstapageCmsPluginViewModel();
30 }
31
32 return self::$viewModel;
33 }
34
35 /**
36 * Class constructor.
37 *
38 * @param array $templates Templates to fetch.
39 * @param array $attributes Attributes to pass to the templates.
40 */
41 public function __construct($templates = null, $attributes = null) {
42 if ($attributes) {
43 foreach ($attributes as $key => $value) {
44 $this->templateData[$key] = $value;
45 }
46 }
47
48 $this->templates = $templates;
49 }
50
51 /**
52 * Initiates the templates. Sets the templates attributes.
53 *
54 * @param array $templates Templates to initiate.
55 * @param array $attributes Attributes to pass to the templates.
56 */
57 public function init($templates = null, $attributes = null) {
58 if ($attributes) {
59 foreach ($attributes as $key => $value) {
60 $this->templateData[$key] = $value;
61 }
62 }
63
64 $this->templates = $templates;
65 }
66
67 /**
68 * Magic method to set up a template attribute.
69 *
70 * @param string $name Name of the attribute.
71 * @param mixed $value Value of the attribute.
72 */
73 public function __set($name, $value) {
74 $this->templateData[$name] = $value;
75 }
76
77 /**
78 * Information to the user how to fetch a template properly.
79 *
80 * @return string A message to the class user.
81 */
82 public function __toString() {
83 return 'use $view->fetch() instead';
84 }
85
86 /**
87 * Assigns a value as a template attribute.
88 *
89 * @param string $key Name of the attribute.
90 * @param mixed $value Value of the attribute.
91 *
92 * @return object Template object.
93 */
94 public function assign($key, $value) {
95 $this->templateData[$key] = $value;
96
97 return $this;
98 }
99
100 /**
101 * Renders the templates.
102 *
103 * @param array $templates. List of templates to render. If it's null, last user template will be rendered.
104 *
105 * @throws Excetion If $templates is null and no templates were used before.
106 * @throws Exception If no template file is found.
107 *
108 * @return string Rendered template content.
109 */
110 public function fetch($templates = null) {
111 $templates = $templates ? $templates : $this->templates;
112
113 if (!$templates || empty($templates)) {
114 throw new Exception("Templates can not be null.");
115 }
116
117 if (!is_array($templates)) {
118 $templates = array($templates);
119 }
120
121 foreach ($templates as $template) {
122 if (!file_exists($template)) {
123 throw new Exception("Template {$template} not found.");
124 }
125
126 if ($this->templateData) {
127 foreach ($this->templateData as $variableName => $variableValue) {
128 $$variableName = $variableValue;
129 unset($variableName);
130 unset($variableValue);
131 }
132 }
133
134 ob_start();
135 include($template);
136 $contents = ob_get_contents();
137 ob_end_clean();
138 }
139
140 return $contents;
141 }
142
143 /**
144 * Sets variables for the selected template and renders it.
145 *
146 * @param string $template Template to render.
147 * @param array $variables Variables to be set as template attributes.
148 *
149 * @return string Rendered template content.
150 */
151 public static function get($template, $variables = null) {
152 $view = new View($template);
153
154 if ($variables) {
155 foreach ($variables as $key => $value) {
156 $view->$key = $value;
157 }
158 }
159
160 return $view->fetch();
161 }
162
163 /**
164 * Gets the template variable.
165 *
166 * @param string $template Template name.
167 * @param array $variables Variables to get.
168 *
169 * @return array Template variables.
170 */
171 public static function _($template, $variables = null) {
172 return self::get($template, $variables);
173 }
174 }
175