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: * StringTemplate instance.
29: *
30: * @var \Cake\View\StringTemplate
31: */
32: protected $_templater;
33:
34: /**
35: * Sets templates to use.
36: *
37: * @param string[] $templates Templates to be added.
38: * @return $this
39: */
40: public function setTemplates(array $templates)
41: {
42: $this->templater()->add($templates);
43:
44: return $this;
45: }
46:
47: /**
48: * Gets templates to use or a specific template.
49: *
50: * @param string|null $template String for reading a specific template, null for all.
51: * @return string|array
52: */
53: public function getTemplates($template = null)
54: {
55: return $this->templater()->get($template);
56: }
57:
58: /**
59: * Gets/sets templates to use.
60: *
61: * @deprecated 3.4.0 Use setTemplates()/getTemplates() instead.
62: * @param string|array|null $templates null or string allow reading templates. An array
63: * allows templates to be added.
64: * @return $this|string|array
65: */
66: public function templates($templates = null)
67: {
68: deprecationWarning(
69: 'StringTemplateTrait::templates() is deprecated. ' .
70: 'Use setTemplates()/getTemplates() instead.'
71: );
72:
73: if ($templates === null || is_string($templates)) {
74: return $this->templater()->get($templates);
75: }
76:
77: $this->templater()->add($templates);
78:
79: return $this;
80: }
81:
82: /**
83: * Formats a template string with $data
84: *
85: * @param string $name The template name.
86: * @param array $data The data to insert.
87: * @return string
88: */
89: public function formatTemplate($name, $data)
90: {
91: return $this->templater()->format($name, $data);
92: }
93:
94: /**
95: * Returns the templater instance.
96: *
97: * @return \Cake\View\StringTemplate
98: */
99: public function templater()
100: {
101: if ($this->_templater === null) {
102: $class = $this->getConfig('templateClass') ?: 'Cake\View\StringTemplate';
103: $this->_templater = new $class();
104:
105: $templates = $this->getConfig('templates');
106: if ($templates) {
107: if (is_string($templates)) {
108: $this->_templater->add($this->_defaultConfig['templates']);
109: $this->_templater->load($templates);
110: } else {
111: $this->_templater->add($templates);
112: }
113: }
114: }
115:
116: return $this->_templater;
117: }
118: }
119: