diff --git a/README.md b/README.md index 53aff25..01e3d65 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,22 @@ generate structured (configuration) files on the fly. Each (configuration) template file is broken into two parts: -* The leading YAML front matter (or *variables* or *meta data*) +* The optional, leading YAML front matter (or *meta-data* variables) * And the actual Twig template contents -Each section starts with a three-hyphen divider (`---`), so that a full file would +In its most simple form, a template without the optional YAML front matter would +look something like this: + +``` +timeout = {{ data.timeout }} +{% for interface in data.interfaces %} +auto {{ interface.name }} + address {{ interface.address }} +{% endfor %} +``` + +If you also want to include *meta-data* variables, then +each section starts with a three-hyphen divider (`---`), so that a full file would look something like this: ``` @@ -30,7 +42,7 @@ auto {{ interface.name }} ### Meta variables -The template files start with the meta-data in the form of a YAML front matter. +The template files can optionally start with the meta-data in the form of a YAML front matter. This syntax is quite simple and is pretty common for template processors and static site generators such as [Jekyll](http://jekyllrb.com/docs/frontmatter/). @@ -52,6 +64,7 @@ Can contain any *Twig template*. The input variables will be accessible under the `data` key. The meta variables will be accessible under the `meta` key. +If no *meta-data* variables are present, then this key defaults to an empty array. ## Install diff --git a/tests/ConfgenTest.php b/tests/ConfgenTest.php index 6b3b46d..0b7ca8f 100644 --- a/tests/ConfgenTest.php +++ b/tests/ConfgenTest.php @@ -72,6 +72,17 @@ public function test05Empty() $this->assertFileNotExists('empty'); } + public function test06Simple() + { + chdir(__DIR__ . '/fixtures/06-simple'); + + $this->confgen->processTemplate('example.conf.twig', $this->loadJson('data.json')); + + // output file successfully generated + $this->assertFileEquals('example.conf', 'example.conf.expected'); + unlink('example.conf'); + } + private function loadJson($path) { return json_decode(file_get_contents($path)); diff --git a/tests/fixtures/06-simple/data.json b/tests/fixtures/06-simple/data.json new file mode 100644 index 0000000..94bf520 --- /dev/null +++ b/tests/fixtures/06-simple/data.json @@ -0,0 +1,7 @@ +{ + "timeout": 20, + "values": [ + 10, + 12 + ] +} diff --git a/tests/fixtures/06-simple/example.conf.expected b/tests/fixtures/06-simple/example.conf.expected new file mode 100644 index 0000000..53feb02 --- /dev/null +++ b/tests/fixtures/06-simple/example.conf.expected @@ -0,0 +1,6 @@ +# just a sample +timeout = 20; + +[all] +add 10; +add 12; diff --git a/tests/fixtures/06-simple/example.conf.twig b/tests/fixtures/06-simple/example.conf.twig new file mode 100644 index 0000000..253d190 --- /dev/null +++ b/tests/fixtures/06-simple/example.conf.twig @@ -0,0 +1,7 @@ +# just a sample +timeout = {{ data.timeout }}; + +[all] +{% for value in data.values %} +add {{ value }}; +{% endfor %}