ソースを参照

Initial implementation of string templates.

This will eventually be used by PaginatorHelper and later other helpers.
Having a separate object will allow easy re-use of logic in core and
application helpers.
mark_story 12 年 前
コミット
168ba36f86
2 ファイル変更146 行追加0 行削除
  1. 70 0
      Cake/Test/TestCase/View/StringTemplateTest.php
  2. 76 0
      Cake/View/StringTemplate.php

+ 70 - 0
Cake/Test/TestCase/View/StringTemplateTest.php

@@ -0,0 +1,70 @@
+<?php
+
+namespace Cake\Test\View;
+
+use Cake\TestSuite\TestCase;
+use Cake\View\StringTemplate;
+
+class StringTemplateTest extends TestCase {
+
+/**
+ * setUp
+ *
+ * @return void
+ */
+	public function setUp() {
+		parent::setUp();
+		$this->template = new StringTemplate();
+	}
+
+/**
+ * test adding templates.
+ *
+ * @return void
+ */
+	public function testAdd() {
+		$templates = [
+			'link' => '<a href="{{url}}">{{text}}</a>'
+		];
+		$result = $this->template->add($templates);
+		$this->assertNull($result, 'No return');
+
+		$this->assertEquals($templates['link'], $this->template->get('link'));
+	}
+
+/**
+ * Test remove.
+ *
+ * @return void
+ */
+	public function testRemove() {
+		$templates = [
+			'link' => '<a href="{{url}}">{{text}}</a>'
+		];
+		$this->template->add($templates);
+		$this->assertNull($this->template->remove('link'), 'No return');
+		$this->assertNull($this->template->get('link'), 'Template should be gone.');
+	}
+
+/**
+ * Test formatting strings.
+ *
+ * @return void
+ */
+	public function testFormat() {
+		$templates = [
+			'link' => '<a href="{{url}}">{{text}}</a>'
+		];
+		$this->template->add($templates);
+
+		$result = $this->template->format('not there', []);
+		$this->assertSame('', $result);
+
+		$result = $this->template->format('link', [
+			'url' => '/',
+			'text' => 'example'
+		]);
+		$this->assertEquals('<a href="/">example</a>', $result);
+	}
+
+}

+ 76 - 0
Cake/View/StringTemplate.php

@@ -0,0 +1,76 @@
+<?php
+namespace Cake\View;
+
+/**
+ * Provides a interface for registering and inserting
+ * content into simple logic-less string templates.
+ *
+ * Used by several helpers to provide simple flexible templates
+ * for generating HTML and other content.
+ */
+class StringTemplate {
+
+/**
+ * The templates this instance holds.
+ *
+ * @var array
+ */
+	protected $_templates = [];
+
+/**
+ * Add one or more template strings.
+ *
+ * @param array $templates The templates to add.
+ * @return void
+ */
+	public function add(array $templates) {
+		$this->_templates = array_merge($this->_templates, $templates);
+	}
+
+/**
+ * Get one or all templates.
+ *
+ * @param string $name Leave null to get all templates, provide a name to get a single template.
+ * @return string|array|null Either the template(s) or null
+ */
+	public function get($name = null) {
+		if ($name === null) {
+			return $this->_templates;
+		}
+		if (!isset($this->_templates[$name])) {
+			return null;
+		}
+		return $this->_templates[$name];
+	}
+
+/**
+ * Remove the named template.
+ *
+ * @param string $name The template to remove.
+ * @return void
+ */
+	public function remove($name) {
+		unset($this->_templates[$name]);
+	}
+
+/**
+ * Format a template string with $data
+ *
+ * @param string $name The template name.
+ * @param array $data The data to insert.
+ * @return string
+ */
+	public function format($name, array $data) {
+		$template = $this->get($name);
+		if ($template === null) {
+			return '';
+		}
+		$replace = [];
+		$keys = array_keys($data);
+		foreach ($keys as $key) {
+			$replace['{{' . $key . '}}'] = $data[$key];
+		}
+		return strtr($template, $replace);
+	}
+
+}