Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

YAML front matter is optional #9

Merged
merged 1 commit into from
Jul 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

```
Expand All @@ -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/).

Expand All @@ -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

Expand Down
11 changes: 11 additions & 0 deletions tests/ConfgenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/06-simple/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"timeout": 20,
"values": [
10,
12
]
}
6 changes: 6 additions & 0 deletions tests/fixtures/06-simple/example.conf.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# just a sample
timeout = 20;

[all]
add 10;
add 12;
7 changes: 7 additions & 0 deletions tests/fixtures/06-simple/example.conf.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# just a sample
timeout = {{ data.timeout }};

[all]
{% for value in data.values %}
add {{ value }};
{% endfor %}