Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |