Skip to content

Commit 550411b

Browse files
author
Carlos Rodríguez Hernández
authored
Update README.md
1 parent 7183055 commit 550411b

File tree

1 file changed

+59
-54
lines changed

1 file changed

+59
-54
lines changed

README.md

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ This tools allows rendering Handlebars 3.0 templates, using as context data the
66

77
# Basic usage
88

9-
```bash
10-
$> render-template --help
9+
```console
10+
$ render-template --help
1111
Usage:
1212
render-template [OPTIONS] [template-file]
1313

@@ -25,26 +25,25 @@ The tool supports rendering templates from a file or from stdin (for convenience
2525

2626
The source data is taken from the environment variables or from a data file, with properties-file format (key=value, a line for each pair). When a variable is defined both as an environment variable and in the data file, the latter will take precedence.
2727

28-
2928
# Examples
3029

3130
## Render data from template file with environment variables
3231

33-
```bash
32+
```console
3433
# Create the template
35-
$> echo 'hello {{who}}' > template.tpl
34+
$ echo 'hello {{who}}' > template.tpl
3635
# Render it without 'who' variable
37-
$> render-template template.tpl
36+
$ render-template template.tpl
3837
hello
3938
# Render it with 'who' variable defined
40-
$> who=bitnami render-template template.tpl
39+
$ who=bitnami render-template template.tpl
4140
hello bitnami
4241
```
4342

4443
## Render data from stdin with environment variables
4544

46-
```bash
47-
$> log_file=/tmp/stout.log port=8080 pid_file=/tmp/my.pid render-template <<"EOF"
45+
```console
46+
$ log_file=/tmp/stout.log port=8080 pid_file=/tmp/my.pid render-template <<"EOF"
4847
# My servide log file
4948
log_file "{{log_file}}"
5049

@@ -67,20 +66,19 @@ port 8080
6766
6867
# My service pid file
6968
pid_file "/tmp/my.pid"
70-
7169
```
7270

7371
## Render data from stdin with data file
7472

75-
```bash
73+
```console
7674
# write data file
77-
$> cat > data.properties <<"EOF"
75+
$ cat > data.properties <<"EOF"
7876
log_file=/tmp/stout.log
7977
port=8080
8078
pid_file=/tmp/my.pid
8179
EOF
8280

83-
$> render-template --data-file ./data.properties <<"EOF"
81+
$ render-template --data-file ./data.properties <<"EOF"
8482
# My servide log file
8583
log_file "{{log_file}}"
8684

@@ -107,39 +105,39 @@ pid_file "/tmp/my.pid"
107105

108106
## Overriding environment variables in data file
109107

110-
```bash
108+
```console
111109
# Lets define some environment variables
112-
$> export name=foo
113-
$> export company=bar
114-
$> export year=3000
110+
$ export name=foo
111+
$ export company=bar
112+
$ export year=3000
115113

116114
# And write a template
117-
$> cat > data.tpl <<"EOF"
115+
$ cat > data.tpl <<"EOF"
118116
{{name}} works at {{company}}
119117
since {{year}}
120118
EOF
121119

122120
# Rendering from the environment would yield
123-
$> render-template data.tpl
121+
$ render-template data.tpl
124122
foo works at bar
125123
since 3000
126124

127125
# But we can override it from a data file, either partially, to get a mix:
128126

129-
$> echo "name=mike" > data.properties
130-
$> render-template --data-file data.properties data.tpl
127+
$ echo "name=mike" > data.properties
128+
$ render-template --data-file data.properties data.tpl
131129
mike works at bar
132130
since 3000
133131

134132
# Or completely:
135133

136-
$> cat > data.properties <<"EOF"
134+
$ cat > data.properties <<"EOF"
137135
name=mike
138136
company=Bitnami
139137
year=2010
140138
EOF
141139

142-
$> render-template --data-file data.properties data.tpl
140+
$ render-template --data-file data.properties data.tpl
143141
mike works at Bitnami
144142
since 2010
145143
```
@@ -148,8 +146,8 @@ since 2010
148146

149147
The tool supports all the standard handlebars helpers: https://handlebarsjs.com/builtin_helpers.html
150148

151-
~~~bash
152-
$> render-template <<"EOF"
149+
```console
150+
$ render-template <<"EOF"
153151
{{#if author}}
154152
{{firstName}} {{lastName}}
155153
{{else}}
@@ -160,7 +158,7 @@ EOF
160158
# Which outputs
161159
Unknown Author
162160

163-
$> author=me firsName=foo lastName=bar render-template <<"EOF"
161+
$ author=me firsName=foo lastName=bar render-template <<"EOF"
164162
{{#if author}}
165163
{{firstName}} {{lastName}}
166164
{{else}}
@@ -170,91 +168,98 @@ EOF
170168

171169
# Outputs:
172170
foo bar
173-
~~~
171+
```
174172

175173
In addition, it includes a few custom helpers:
176174

177175
### json_escape
178176

179177
The json_escape helper converts the provided value into a valid JSON string
180-
~~~bash
181-
$> export VALUE='this is "a string", with quoting
178+
```console
179+
$ export VALUE='this is "a string", with quoting
182180

183181
and some line breaks'
184-
~~~
182+
```
185183
Without the helper:
186184

187-
~~~bash
188-
$> render-template <<<'VALUE={{VALUE}}'
185+
```console
186+
$ render-template <<<'VALUE={{VALUE}}'
189187
VALUE=this is "a string", with quoting
190188

191189
and some line breaks
192-
~~~
190+
```
193191

194192
Using the helper:
195-
~~~bash
196-
$> render-template <<<'VALUE={{json_escape VALUE}}'
193+
```console
194+
$ render-template <<<'VALUE={{json_escape VALUE}}'
197195
VALUE="this is \"a string\", with quoting\n\nand some line breaks"
198-
~~~
199-
196+
```
200197

201198
### quote
202199

203200
The quote helper Quotes a string
204201

205202
Without the helper:
206-
~~~bash
207-
$> ARG1="some arg" ARG2="some other \"arg\"" render-template <<"EOF"
203+
```console
204+
$ ARG1="some arg" ARG2="some other \"arg\"" render-template <<"EOF"
208205
ARG1={{ARG1}} ARG2={{ARG2}}
209206
EOF
210207
ARG1=some arg ARG2=some other "arg"
211-
~~~
208+
```
212209

213210
With the helper
214211

215-
~~~bash
212+
```console
216213
ARG1="some arg" ARG2="some other \"arg\"" render-template <<"EOF"
217214
ARG1={{quote ARG1}} ARG2={{quote ARG2}}
218215
EOF
219216
ARG1="some arg" ARG2="some other \"arg\""
220-
~~~
217+
```
221218

222219
### or
223220

224221
This helper allows using the "or" logical operation over two values (a value will be true if not empty)
225222

226223
To render a block when either "firstName" or "lastName" values ar not empty:
227224

228-
~~~bash
229-
$> cat > data.tpl <<"EOF"
225+
```console
226+
$ cat > data.tpl <<"EOF"
230227
{{#if (or firstName lastName)}}
231228
{{firstName}} {{lastName}}
232229
{{else}}
233230
Unknown Author
234231
{{/if}}
235232
EOF
236233

237-
$>render-template data.tpl
234+
$ render-template data.tpl
238235
Unknown Author
239236

240-
$> firstName=foo render-template data.tpl
237+
$ firstName=foo render-template data.tpl
241238
foo
242239

243-
$> lastName=bar render-template data.tpl
244-
bar
240+
$ lastName=bar render-template data.tpl
241+
bar
245242
246-
$> firstName=foo lastName=bar render-template data.tpl
243+
$ firstName=foo lastName=bar render-template data.tpl
247244
foo bar
248-
~~~
245+
```
249246

250247
This helper can also be used to provide defaults for your template variables:
251248

252-
~~~bash
249+
```console
253250
# No value provided, so we fallback to the second "or" argument
254-
$> render-template <<<'VALUE={{or ENV_VALUE "default value"}}'
251+
$ render-template <<<'VALUE={{or ENV_VALUE "default value"}}'
255252
VALUE=default value
256253

257254
# ENV_VALUE is defined, so we take it
258-
$> ENV_VALUE="customized value" render-template <<<'VALUE={{or ENV_VALUE "default value"}}'
255+
$ ENV_VALUE="customized value" render-template <<<'VALUE={{or ENV_VALUE "default value"}}'
259256
VALUE=customized value
260-
~~~
257+
```
258+
259+
## License
260+
261+
Copyright &copy; 2023 Bitnami
262+
263+
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.
264+
265+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

0 commit comments

Comments
 (0)