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

Adds examples using the @first and @last Handlebars variables #4113

Merged
merged 1 commit into from
Nov 28, 2017
Merged
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
40 changes: 31 additions & 9 deletions www/source/partials/docs/_built-in-helpers.html.md.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ blocks.
Here's an example that will only write out configuration for the
unixsocket tunable if a value was set by the user:

{{~#if cfg.unixsocket}}
{{#if cfg.unixsocket ~}}
unixsocket {{cfg.unixsocket}}
{{~/if}}
{{/if ~}}

> **Note** The `~` indicates that whitespace should be omitted when rendering

Expand All @@ -48,26 +48,26 @@ TOML allows you to create sections (called [TOML tables](https://github.com/toml

When writing your template, you can use the `with` helper to reduce duplication:

{{#with cfg.repl}}
{{#with cfg.repl ~}}
repl-backlog-size {{backlog-size}}
repl-backlog-ttl {{backlog-ttl}}
repl-disable-tcp-nodelay {{disable-tcp-nodelay}}
{{/with}}
{{/with ~}}

Helpers can also be nested and used together in block expressions. Here is another example from the redis.config file where the `if` and `with` helpers are used together to set up `core/redis` Habitat services in a leader-follower topology.

{{#if svc.me.follower}}
{{#if svc.me.follower ~}}
slaveof {{svc.leader.sys.ip}} {{svc.leader.cfg.port}}
{/if}}
{/if ~}}

Here's an example using `each` to render multiple server entries:

{{~#each cfg.servers as |server| }}
{{#each cfg.servers as |server| ~}}
server {
host {{server.host}}
port {{server.port}}
}
{{~/each}}
{{/each ~}}

You would specify the corresponding values in a TOML file using an
[array of tables](https://github.com/toml-lang/toml#array-of-tables)
Expand All @@ -79,4 +79,26 @@ like this:

[[servers]]
host = "host-2"
port = 3434
port = 3434

And for both `each` and `unless`, you can use `@first` and `@last` to specify which item in an array you want to perform business logic on. For example:

"mongo": {
{{#each bind.database.members as |member| ~}}
{{#if @first ~}}
"host" : "{{member.sys.ip}}",
"port" : "{{member.cfg.port}}"
{{/if ~}}
{{/each ~}}
}

> **Note** The `@first` and `@last` variables also work with the Habitat helper `eachAlive`, and in the example above, it would be preferrable to the built-in `each` helper because it checks whether the service is available before trying to retrieve any values.

For `unless`, using `@last` can also be helpful when you need to optionally include delimiters. In the example below, the IP addresses of the alive members returned by the `servers` binding is comma-separated. The logic check `{{#unless @last}}, {{/unless}}` at the end ensures that the comma is written after each element except the last element.

{{#eachAlive bind.servers.members as |member| ~}}
"{{member.sys.ip}}"
{{#unless @last ~}}, {{/unless ~}}
{{/eachAlive ~}}]