Browse Source

Allow StringTemplate::__construct() to take templates.

This saves a line of code when using templates that have already been
loaded.
mark_story 12 years ago
parent
commit
3489cd23ab
2 changed files with 24 additions and 0 deletions
  1. 11 0
      src/View/StringTemplate.php
  2. 13 0
      tests/TestCase/View/StringTemplateTest.php

+ 11 - 0
src/View/StringTemplate.php

@@ -49,6 +49,17 @@ class StringTemplate {
 	];
 
 /**
+ * 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.
  *
  * Template files should define a `$config` variable containing

+ 13 - 0
tests/TestCase/View/StringTemplateTest.php

@@ -31,6 +31,19 @@ class StringTemplateTest extends TestCase {
 	}
 
 /**
+ * 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.
  *
  * @return void