Skip to content

Commit

Permalink
Allow StringTemplate::__construct() to take templates.
Browse files Browse the repository at this point in the history
This saves a line of code when using templates that have already been
loaded.
  • Loading branch information
markstory committed Jan 15, 2014
1 parent a62438f commit 3489cd2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/View/StringTemplate.php
Expand Up @@ -48,6 +48,17 @@ class StringTemplate {
'compactAttribute' => '{{name}}="{{value}}"', 'compactAttribute' => '{{name}}="{{value}}"',
]; ];


/**
* Constructor.
*
* @param array $templates A set of templates to add.
*/
public function __construct(array $templates = null) {
if ($templates) {
$this->add($templates);
}
}

/** /**
* Load a config file containing templates. * Load a config file containing templates.
* *
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/View/StringTemplateTest.php
Expand Up @@ -30,6 +30,19 @@ public function setUp() {
$this->template = new StringTemplate(); $this->template = new StringTemplate();
} }


/**
* Test adding templates through the constructor.
*
* @return void
*/
public function testConstructorAdd() {
$templates = [
'link' => '<a href="{{url}}">{{text}}</a>'
];
$template = new StringTemplate($templates);
$this->assertEquals($templates['link'], $template->get('link'));
}

/** /**
* test adding templates. * test adding templates.
* *
Expand Down

0 comments on commit 3489cd2

Please sign in to comment.