StringTemplate.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since CakePHP(tm) v3.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\View;
  16. use Cake\Configure\Engine\PhpConfig;
  17. use Cake\Core\Plugin;
  18. use Cake\Error;
  19. /**
  20. * Provides a interface for registering and inserting
  21. * content into simple logic-less string templates.
  22. *
  23. * Used by several helpers to provide simple flexible templates
  24. * for generating HTML and other content.
  25. */
  26. class StringTemplate {
  27. /**
  28. * The templates this instance holds.
  29. *
  30. * @var array
  31. */
  32. protected $_templates = [];
  33. /**
  34. * Load a config file containing templates.
  35. *
  36. * Template files should define a `$config` variable containing
  37. * all the templates to load. Loaded templates will be merged with existing
  38. * templates.
  39. *
  40. * @param string $file The file to load
  41. * @return void
  42. */
  43. public function load($file) {
  44. list($plugin, $file) = pluginSplit($file);
  45. $path = APP . 'Config/';
  46. if ($plugin !== null) {
  47. $path = Plugin::path($plugin) . 'Config/';
  48. }
  49. $loader = new PhpConfig($path);
  50. $templates = $loader->read($file);
  51. $this->add($templates);
  52. }
  53. /**
  54. * Add one or more template strings.
  55. *
  56. * @param array $templates The templates to add.
  57. * @return void
  58. */
  59. public function add(array $templates) {
  60. $this->_templates = array_merge($this->_templates, $templates);
  61. }
  62. /**
  63. * Get one or all templates.
  64. *
  65. * @param string $name Leave null to get all templates, provide a name to get a single template.
  66. * @return string|array|null Either the template(s) or null
  67. */
  68. public function get($name = null) {
  69. if ($name === null) {
  70. return $this->_templates;
  71. }
  72. if (!isset($this->_templates[$name])) {
  73. return null;
  74. }
  75. return $this->_templates[$name];
  76. }
  77. /**
  78. * Remove the named template.
  79. *
  80. * @param string $name The template to remove.
  81. * @return void
  82. */
  83. public function remove($name) {
  84. unset($this->_templates[$name]);
  85. }
  86. /**
  87. * Format a template string with $data
  88. *
  89. * @param string $name The template name.
  90. * @param array $data The data to insert.
  91. * @return string
  92. */
  93. public function format($name, array $data) {
  94. $template = $this->get($name);
  95. if ($template === null) {
  96. return '';
  97. }
  98. $replace = [];
  99. $keys = array_keys($data);
  100. foreach ($keys as $key) {
  101. $replace['{{' . $key . '}}'] = $data[$key];
  102. }
  103. return strtr($template, $replace);
  104. }
  105. }