Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Template Class Reference
Inheritance diagram for Template:
Zend_Filter_Interface Filter Filter Filter Filter FilterEmulate Filter

Public Member Functions

 __construct (\Magento\Framework\Stdlib\StringUtils $string, $variables=[])
 
 setVariables (array $variables)
 
 setTemplateProcessor (callable $callback)
 
 getTemplateProcessor ()
 
 filter ($value)
 
 addAfterFilterCallback (callable $afterFilterCallback)
 
 varDirective ($construction)
 
 templateDirective ($construction)
 
 dependDirective ($construction)
 
 ifDirective ($construction)
 

Data Fields

const CONSTRUCTION_PATTERN = '/{{([a-z]{0,10})(.*?)}}/si'
 
const CONSTRUCTION_DEPEND_PATTERN = '/{{depend\s*(.*?)}}(.*?){{\\/depend\s*}}/si'
 
const CONSTRUCTION_IF_PATTERN = '/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/if\s*}}/si'
 
const CONSTRUCTION_TEMPLATE_PATTERN = '/{{(template)(.*?)}}/si'
 
const LOOP_PATTERN = '/{{for(?P<loopItem>.*? )(in)(?P<loopData>.*?)}}(?P<loopBody>.*?){{\/for}}/si'
 

Protected Member Functions

 afterFilter ($value)
 
 resetAfterFilterCallbacks ()
 
 getParameters ($value)
 
 getVariable ($value, $default='{no_value_defined}')
 
 getStackArgs ($stack)
 

Protected Attributes

 $templateVars = []
 
 $templateProcessor = null
 
 $string
 

Detailed Description

@api

Since
100.0.2

Definition at line 16 of file Template.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Stdlib\StringUtils  $string,
  $variables = [] 
)
Parameters
\Magento\Framework\Stdlib\StringUtils$string
array$variables

Definition at line 69 of file Template.php.

70  {
71  $this->string = $string;
72  $this->setVariables($variables);
73  }
setVariables(array $variables)
Definition: Template.php:81

Member Function Documentation

◆ addAfterFilterCallback()

addAfterFilterCallback ( callable  $afterFilterCallback)

Adds a callback to run after main filtering has happened. Callback must accept a single argument and return a string of the processed value.

Parameters
callable$afterFilterCallback
Returns
$this

Definition at line 238 of file Template.php.

239  {
240  // Only add callback if it doesn't already exist
241  if (in_array($afterFilterCallback, $this->afterFilterCallbacks)) {
242  return $this;
243  }
244 
245  $this->afterFilterCallbacks[] = $afterFilterCallback;
246  return $this;
247  }

◆ afterFilter()

afterFilter (   $value)
protected

Runs callbacks that have been added to filter content after directive processing is finished.

Parameters
string$value
Returns
string

Definition at line 220 of file Template.php.

221  {
222  foreach ($this->afterFilterCallbacks as $callback) {
223  $value = call_user_func($callback, $value);
224  }
225  // Since a single instance of this class can be used to filter content multiple times, reset callbacks to
226  // prevent callbacks running for unrelated content (e.g., email subject and email body)
227  $this->resetAfterFilterCallbacks();
228  return $value;
229  }
$value
Definition: gender.phtml:16

◆ dependDirective()

dependDirective (   $construction)
Parameters
string[]$construction
Returns
string

Definition at line 309 of file Template.php.

310  {
311  if (count($this->templateVars) == 0) {
312  // If template processing
313  return $construction[0];
314  }
315 
316  if ($this->getVariable($construction[1], '') == '') {
317  return '';
318  } else {
319  return $construction[2];
320  }
321  }
getVariable($value, $default='{no_value_defined}')
Definition: Template.php:370

◆ filter()

filter (   $value)

Filter the string as template.

Parameters
string$value
Returns
string
Exceptions

Implements Zend_Filter_Interface.

Definition at line 119 of file Template.php.

120  {
121  // "depend", "if", and "template" directives should be first
122  foreach ([
123  self::CONSTRUCTION_DEPEND_PATTERN => 'dependDirective',
124  self::CONSTRUCTION_IF_PATTERN => 'ifDirective',
125  self::CONSTRUCTION_TEMPLATE_PATTERN => 'templateDirective',
126  ] as $pattern => $directive) {
127  if (preg_match_all($pattern, $value, $constructions, PREG_SET_ORDER)) {
128  foreach ($constructions as $construction) {
129  $callback = [$this, $directive];
130  if (!is_callable($callback)) {
131  continue;
132  }
133  try {
134  $replacedValue = call_user_func($callback, $construction);
135  } catch (\Exception $e) {
136  throw $e;
137  }
138  $value = str_replace($construction[0], $replacedValue, $value);
139  }
140  }
141  }
142 
143  $value = $this->filterFor($value);
144 
145  if (preg_match_all(self::CONSTRUCTION_PATTERN, $value, $constructions, PREG_SET_ORDER)) {
146  foreach ($constructions as $construction) {
147  $callback = [$this, $construction[1] . 'Directive'];
148  if (!is_callable($callback)) {
149  continue;
150  }
151  try {
152  $replacedValue = call_user_func($callback, $construction);
153  } catch (\Exception $e) {
154  throw $e;
155  }
156  $value = str_replace($construction[0], $replacedValue, $value);
157  }
158  }
159 
160  $value = $this->afterFilter($value);
161  return $value;
162  }
$pattern
Definition: website.php:22
$value
Definition: gender.phtml:16

◆ getParameters()

getParameters (   $value)
protected

Return associative array of parameters.

Parameters
string$valueraw parameters
Returns
array

Definition at line 349 of file Template.php.

350  {
351  $tokenizer = new Template\Tokenizer\Parameter();
352  $tokenizer->setString($value);
353  $params = $tokenizer->tokenize();
354  foreach ($params as $key => $value) {
355  if (substr($value, 0, 1) === '$') {
356  $params[$key] = $this->getVariable(substr($value, 1), null);
357  }
358  }
359  return $params;
360  }
$value
Definition: gender.phtml:16
getVariable($value, $default='{no_value_defined}')
Definition: Template.php:370
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ getStackArgs()

getStackArgs (   $stack)
protected

Loops over a set of stack args to process variables into array argument values

Parameters
array$stack
Returns
array

Definition at line 434 of file Template.php.

435  {
436  foreach ($stack as $i => $value) {
437  if (is_array($value)) {
438  $stack[$i] = $this->getStackArgs($value);
439  } elseif (substr($value, 0, 1) === '$') {
440  $stack[$i] = $this->getVariable(substr($value, 1), null);
441  }
442  }
443  return $stack;
444  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$value
Definition: gender.phtml:16
getVariable($value, $default='{no_value_defined}')
Definition: Template.php:370
$i
Definition: gallery.phtml:31

◆ getTemplateProcessor()

getTemplateProcessor ( )

Sets the processor for template directive.

Returns
callable|null

Definition at line 106 of file Template.php.

107  {
108  return is_callable($this->templateProcessor) ? $this->templateProcessor : null;
109  }

◆ getVariable()

getVariable (   $value,
  $default = '{no_value_defined}' 
)
protected

Return variable value for var construction

Parameters
string$valueraw parameters
string$defaultdefault value
Returns
string @SuppressWarnings(PHPMD.CyclomaticComplexity)

Definition at line 370 of file Template.php.

370  {no_value_defined}')
371  {
372  \Magento\Framework\Profiler::start('email_template_processing_variables');
373  $tokenizer = new Template\Tokenizer\Variable();
374  $tokenizer->setString($value);
375  $stackVars = $tokenizer->tokenize();
376  $result = $default;
377  $last = 0;
378  for ($i = 0; $i < count($stackVars); $i++) {
379  if ($i == 0 && isset($this->templateVars[$stackVars[$i]['name']])) {
380  // Getting of template value
381  $stackVars[$i]['variable'] = & $this->templateVars[$stackVars[$i]['name']];
382  } elseif (isset($stackVars[$i - 1]['variable'])
383  && $stackVars[$i - 1]['variable'] instanceof \Magento\Framework\DataObject
384  ) {
385  // If data object calling methods or getting properties
386  if ($stackVars[$i]['type'] == 'property') {
387  $caller = 'get' . $this->string->upperCaseWords($stackVars[$i]['name'], '_', '');
388  $stackVars[$i]['variable'] = method_exists(
389  $stackVars[$i - 1]['variable'],
390  $caller
391  ) ? $stackVars[$i - 1]['variable']->{$caller}() : $stackVars[$i - 1]['variable']->getData(
392  $stackVars[$i]['name']
393  );
394  } elseif ($stackVars[$i]['type'] == 'method') {
395  // Calling of data object method
396  if (method_exists($stackVars[$i - 1]['variable'], $stackVars[$i]['name'])
397  || substr($stackVars[$i]['name'], 0, 3) == 'get'
398  ) {
399  $stackVars[$i]['args'] = $this->getStackArgs($stackVars[$i]['args']);
400  $stackVars[$i]['variable'] = call_user_func_array(
401  [$stackVars[$i - 1]['variable'], $stackVars[$i]['name']],
402  $stackVars[$i]['args']
403  );
404  }
405  }
406  $last = $i;
407  } elseif (isset($stackVars[$i - 1]['variable']) && $stackVars[$i]['type'] == 'method') {
408  // Calling object methods
409  if (method_exists($stackVars[$i - 1]['variable'], $stackVars[$i]['name'])) {
410  $stackVars[$i]['args'] = $this->getStackArgs($stackVars[$i]['args']);
411  $stackVars[$i]['variable'] = call_user_func_array(
412  [$stackVars[$i - 1]['variable'], $stackVars[$i]['name']],
413  $stackVars[$i]['args']
414  );
415  }
416  $last = $i;
417  }
418  }
419 
420  if (isset($stackVars[$last]['variable'])) {
421  // If value for construction exists set it
422  $result = $stackVars[$last]['variable'];
423  }
424  \Magento\Framework\Profiler::stop('email_template_processing_variables');
425  return $result;
426  }

◆ ifDirective()

ifDirective (   $construction)
Parameters
string[]$construction
Returns
string

Definition at line 327 of file Template.php.

328  {
329  if (count($this->templateVars) == 0) {
330  return $construction[0];
331  }
332 
333  if ($this->getVariable($construction[1], '') == '') {
334  if (isset($construction[3]) && isset($construction[4])) {
335  return $construction[4];
336  }
337  return '';
338  } else {
339  return $construction[2];
340  }
341  }
getVariable($value, $default='{no_value_defined}')
Definition: Template.php:370

◆ resetAfterFilterCallbacks()

resetAfterFilterCallbacks ( )
protected

Resets the after filter callbacks

Returns
$this

Definition at line 254 of file Template.php.

255  {
256  $this->afterFilterCallbacks = [];
257  return $this;
258  }

◆ setTemplateProcessor()

setTemplateProcessor ( callable  $callback)

Sets the processor for template directive.

Parameters
callable$callbackit must return string
Returns
$this

Definition at line 95 of file Template.php.

96  {
97  $this->templateProcessor = $callback;
98  return $this;
99  }

◆ setVariables()

setVariables ( array  $variables)

Sets template variables that's can be called through {var ...} statement

Parameters
array$variables
Returns
\Magento\Framework\Filter\Template

Definition at line 81 of file Template.php.

82  {
83  foreach ($variables as $name => $value) {
84  $this->templateVars[$name] = $value;
85  }
86  return $this;
87  }
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ templateDirective()

templateDirective (   $construction)

Allows templates to be included inside other templates

Usage:

{{template config_path="<PATH>"}}

<PATH> equals the XPATH to the system configuration value that contains the value of the template. This directive is useful to include things like a global header/footer.

Parameters
string[]$construction
Returns
mixed

Definition at line 288 of file Template.php.

289  {
290  // Processing of {template config_path=... [...]} statement
291  $templateParameters = $this->getParameters($construction[2]);
292  if (!isset($templateParameters['config_path']) or !$this->getTemplateProcessor()) {
293  // Not specified template or not set include processor
294  $replacedValue = '{Error in template processing}';
295  } else {
296  // Including of template
297  $configPath = $templateParameters['config_path'];
298  unset($templateParameters['config_path']);
299  $templateParameters = array_merge_recursive($templateParameters, $this->templateVars);
300  $replacedValue = call_user_func($this->getTemplateProcessor(), $configPath, $templateParameters);
301  }
302  return $replacedValue;
303  }

◆ varDirective()

varDirective (   $construction)
Parameters
string[]$construction
Returns
string

Definition at line 264 of file Template.php.

265  {
266  if (count($this->templateVars) == 0) {
267  // If template prepossessing
268  return $construction[0];
269  }
270 
271  $replacedValue = $this->getVariable($construction[2], '');
272  return $replacedValue;
273  }
getVariable($value, $default='{no_value_defined}')
Definition: Template.php:370

Field Documentation

◆ $string

$string
protected

Definition at line 63 of file Template.php.

◆ $templateProcessor

$templateProcessor = null
protected

Definition at line 58 of file Template.php.

◆ $templateVars

$templateVars = []
protected

Definition at line 51 of file Template.php.

◆ CONSTRUCTION_DEPEND_PATTERN

const CONSTRUCTION_DEPEND_PATTERN = '/{{depend\s*(.*?)}}(.*?){{\\/depend\s*}}/si'

Construction depend regular expression

Definition at line 26 of file Template.php.

◆ CONSTRUCTION_IF_PATTERN

const CONSTRUCTION_IF_PATTERN = '/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/if\s*}}/si'

Construction if regular expression

Definition at line 31 of file Template.php.

◆ CONSTRUCTION_PATTERN

const CONSTRUCTION_PATTERN = '/{{([a-z]{0,10})(.*?)}}/si'

Construction regular expression

Definition at line 21 of file Template.php.

◆ CONSTRUCTION_TEMPLATE_PATTERN

const CONSTRUCTION_TEMPLATE_PATTERN = '/{{(template)(.*?)}}/si'

Construction template regular expression

Definition at line 36 of file Template.php.

◆ LOOP_PATTERN

const LOOP_PATTERN = '/{{for(?P<loopItem>.*? )(in)(?P<loopData>.*?)}}(?P<loopBody>.*?){{\/for}}/si'

Construction for regular expression

Definition at line 41 of file Template.php.


The documentation for this class was generated from the following file: