Skip to content

Commit

Permalink
Initial implementation of string templates.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Sep 22, 2013
1 parent 36b96be commit 168ba36
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
70 changes: 70 additions & 0 deletions 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 changes: 76 additions & 0 deletions 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);
}

}

0 comments on commit 168ba36

Please sign in to comment.