Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Template.php
Go to the documentation of this file.
1 <?php
7 
10 
32 class Template extends AbstractBlock
33 {
37  const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink';
38 
44  protected $_viewVars = [];
45 
51  protected $_baseUrl;
52 
58  protected $_jsUrl;
59 
65  protected $_allowSymlinks;
66 
72  protected $_filesystem;
73 
79  protected $_template;
80 
87 
93  protected $_storeManager;
94 
100  protected $_appState;
101 
107  protected $directory;
108 
114  private $mediaDirectory;
115 
121  protected $templateContext;
122 
126  protected $pageConfig;
127 
131  protected $resolver;
132 
136  protected $validator;
137 
144  public function __construct(Template\Context $context, array $data = [])
145  {
146  $this->validator = $context->getValidator();
147  $this->resolver = $context->getResolver();
148  $this->_filesystem = $context->getFilesystem();
149  $this->templateEnginePool = $context->getEnginePool();
150  $this->_storeManager = $context->getStoreManager();
151  $this->_appState = $context->getAppState();
152  $this->templateContext = $this;
153  $this->pageConfig = $context->getPageConfig();
154  parent::__construct($context, $data);
155  }
156 
164  {
165  $this->templateContext = $templateContext;
166  }
167 
172  protected function _construct()
173  {
174  parent::_construct();
175 
176  /*
177  * In case template was passed through constructor
178  * we assign it to block's property _template
179  * Mainly for those cases when block created
180  * not via \Magento\Framework\View\Model\LayoutInterface::addBlock()
181  */
182  if ($this->hasData('template')) {
183  $this->setTemplate($this->getData('template'));
184  }
185  }
186 
192  public function getTemplate()
193  {
194  return $this->_template;
195  }
196 
203  public function setTemplate($template)
204  {
205  $this->_template = $template;
206  return $this;
207  }
208 
215  public function getTemplateFile($template = null)
216  {
217  $params = ['module' => $this->getModuleName()];
218  $area = $this->getArea();
219  if ($area) {
220  $params['area'] = $area;
221  }
222  return $this->resolver->getTemplateFileName($template ?: $this->getTemplate(), $params);
223  }
224 
230  public function getArea()
231  {
232  return $this->_getData('area') ? $this->_getData('area') : $this->_appState->getAreaCode();
233  }
234 
242  public function assign($key, $value = null)
243  {
244  if (is_array($key)) {
245  foreach ($key as $subKey => $subValue) {
246  $this->assign($subKey, $subValue);
247  }
248  } else {
249  $this->_viewVars[$key] = $value;
250  }
251  return $this;
252  }
253 
260  public function fetchView($fileName)
261  {
262  $relativeFilePath = $this->getRootDirectory()->getRelativePath($fileName);
263  \Magento\Framework\Profiler::start(
264  'TEMPLATE:' . $fileName,
265  ['group' => 'TEMPLATE', 'file_name' => $relativeFilePath]
266  );
267 
268  if ($this->validator->isValid($fileName)) {
269  $extension = pathinfo($fileName, PATHINFO_EXTENSION);
270  $templateEngine = $this->templateEnginePool->get($extension);
271  $html = $templateEngine->render($this->templateContext, $fileName, $this->_viewVars);
272  } else {
273  $html = '';
274  $templatePath = $fileName ?: $this->getTemplate();
275  $errorMessage = "Invalid template file: '{$templatePath}' in module: '{$this->getModuleName()}'"
276  . " block's name: '{$this->getNameInLayout()}'";
277  if ($this->_appState->getMode() === \Magento\Framework\App\State::MODE_DEVELOPER) {
278  throw new \Magento\Framework\Exception\ValidatorException(
279  new \Magento\Framework\Phrase(
280  $errorMessage
281  )
282  );
283  }
284  $this->_logger->critical($errorMessage);
285  }
286 
287  \Magento\Framework\Profiler::stop('TEMPLATE:' . $fileName);
288  return $html;
289  }
290 
296  protected function _toHtml()
297  {
298  if (!$this->getTemplate()) {
299  return '';
300  }
301  return $this->fetchView($this->getTemplateFile());
302  }
303 
309  public function getBaseUrl()
310  {
311  if (!$this->_baseUrl) {
312  $this->_baseUrl = $this->_urlBuilder->getBaseUrl();
313  }
314  return $this->_baseUrl;
315  }
316 
324  public function getObjectData(\Magento\Framework\DataObject $object, $key)
325  {
326  return $object->getDataUsingMethod((string)$key);
327  }
328 
334  public function getCacheKeyInfo()
335  {
336  return [
337  'BLOCK_TPL',
338  $this->_storeManager->getStore()->getCode(),
339  $this->getTemplateFile(),
340  'base_url' => $this->getBaseUrl(),
341  'template' => $this->getTemplate()
342  ];
343  }
344 
350  protected function getRootDirectory()
351  {
352  if (null === $this->directory) {
353  $this->directory = $this->_filesystem->getDirectoryRead(DirectoryList::ROOT);
354  }
355 
356  return $this->directory;
357  }
358 
364  protected function getMediaDirectory()
365  {
366  if (!$this->mediaDirectory) {
367  $this->mediaDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA);
368  }
369  return $this->mediaDirectory;
370  }
371 }
getData($key='', $index=null)
Definition: DataObject.php:119
$fileName
Definition: translate.phtml:15
$value
Definition: gender.phtml:16
__construct(Template\Context $context, array $data=[])
Definition: Template.php:144
getObjectData(\Magento\Framework\DataObject $object, $key)
Definition: Template.php:324
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$template
Definition: export.php:12