1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\View;
16:
17: /**
18: * Adds string template functionality to any class by providing methods to
19: * load and parse string templates.
20: *
21: * This trait requires the implementing class to provide a `config()`
22: * method for reading/updating templates. An implementation of this method
23: * is provided by `Cake\Core\InstanceConfigTrait`
24: */
25: trait StringTemplateTrait
26: {
27:
28: /**
29: * StringTemplate instance.
30: *
31: * @var \Cake\View\StringTemplate
32: */
33: protected $_templater;
34:
35: /**
36: * Sets templates to use.
37: *
38: * @param array $templates Templates to be added.
39: * @return $this
40: */
41: public function setTemplates(array $templates)
42: {
43: $this->templater()->add($templates);
44:
45: return $this;
46: }
47:
48: /**
49: * Gets templates to use or a specific template.
50: *
51: * @param string|null $template String for reading a specific template, null for all.
52: * @return string|array
53: */
54: public function getTemplates($template = null)
55: {
56: return $this->templater()->get($template);
57: }
58:
59: /**
60: * Gets/sets templates to use.
61: *
62: * @deprecated 3.4.0 Use setTemplates()/getTemplates() instead.
63: * @param string|null|array $templates null or string allow reading templates. An array
64: * allows templates to be added.
65: * @return $this|string|array
66: */
67: public function templates($templates = null)
68: {
69: deprecationWarning(
70: 'StringTemplateTrait::templates() is deprecated. ' .
71: 'Use setTemplates()/getTemplates() instead.'
72: );
73:
74: if ($templates === null || is_string($templates)) {
75: return $this->templater()->get($templates);
76: }
77:
78: $this->templater()->add($templates);
79:
80: return $this;
81: }
82:
83: /**
84: * Formats a template string with $data
85: *
86: * @param string $name The template name.
87: * @param array $data The data to insert.
88: * @return string
89: */
90: public function formatTemplate($name, $data)
91: {
92: return $this->templater()->format($name, $data);
93: }
94:
95: /**
96: * Returns the templater instance.
97: *
98: * @return \Cake\View\StringTemplate
99: */
100: public function templater()
101: {
102: if ($this->_templater === null) {
103: $class = $this->getConfig('templateClass') ?: 'Cake\View\StringTemplate';
104: $this->_templater = new $class();
105:
106: $templates = $this->getConfig('templates');
107: if ($templates) {
108: if (is_string($templates)) {
109: $this->_templater->add($this->_defaultConfig['templates']);
110: $this->_templater->load($templates);
111: } else {
112: $this->_templater->add($templates);
113: }
114: }
115: }
116:
117: return $this->_templater;
118: }
119: }
120: