Adding more demo files

This commit is contained in:
Tyler Akins 2015-01-28 09:44:44 -06:00
parent bc838de550
commit 6a6aadca50
5 changed files with 75 additions and 3 deletions

View File

@ -7,9 +7,9 @@ Mo - Mustache Templates in Bash
I hope your {{TIME_PERIOD}} was fun.
Let's try using this with some data in bash. Save those lines to `trip.txt` and run a command like this:
The above file is [`demo/fun-trip.mo`](demo/fun-trip.mo). Let's try using this template some data from bash's environment. Go to your checked out copy of the project and run a command like this:
NAME=Tyler TIME_PERIOD=weekend ./mo weekend-trip.txt
NAME=Tyler TIME_PERIOD=weekend ./mo demo/fun-trip.mo
Your result?
@ -30,6 +30,35 @@ Requirements
If you intend to develop this and run the official specs, you also need node.js.
How to Use
----------
If you only plan using strings and numbers, nothing could be simpler. In your shell script you can choose to export the variables. The below script is [`demo/using-strings`](demo/using-strings).
#!/bin/bash
cd "$(dirname "$0")" # Go to the script's directory
export TEST="This is a test"
echo "Your message: {{TEST}}" | ../mo
The result? "Your message: This is a test".
Using arrays adds a slight level of complexity. *You must source `mo`.* Look at [`demo/using-arrays`](demo/using-arrays).
#!/bin/bash
cd "$(dirname "$0")" # Go to the script's directory
export ARRAY=( one two "three three three" four five )
cat << EOF | . ../mo
Here are the items in the array:
{{#ARRAY}}
* {{.}}
{{/ARRAY}}
EOF
The result? You get a list of the five elements in the array. Take a look at the line that executes `mo`. You'll see that we went from `../mo` to using `. ../mo`. That is very important when you want arrays to work, since you can not execute a command and have arrays passed to that command's environment. Instead, we source the file. `. ../mo` is identical to `source ../mo` and you can find more about that in bash's man page.
There are more scripts available in the [demos directory](demo/) that could help illustrate how you would use this program.
Concessions
-----------
@ -37,6 +66,7 @@ I admit that implementing everything in bash just doesn't make a lot of sense.
Pull requests to solve the following issues would be helpful.
### Mustache Syntax
* Dotted names are not supported and this means associative arrays are not addressable via their index. Partly this is because our target (Bash 3) does not support associative arrays.
@ -67,7 +97,7 @@ There is a diagnostic script that is aimed to help at making sure the internal f
### Failed Specs
It is acceptable for some of the official spec tests to fail. Anything dealing with multiple levels of objects (eg. `{{a.b.c}}`) and changing the delimiters (`{{= | | =}}` will fail. Other than that, this bash implementation of the mustache spec should pass tests.
It is acceptable for some of the official spec tests to fail. Anything dealing with multiple levels of objects (eg. `{{a.b.c}}`) and changing the delimiters (`{{= | | =}}`) will fail. Other than that, this bash implementation of the mustache spec should pass tests.
Specific issues:
* Interpolation - Multiple Calls: This fails because lambdas execute in a subshell so their output can be captured. This is flagged as a TODO in the code.

26
demo/embedded-template Executable file
View File

@ -0,0 +1,26 @@
#!/bin/bash
#
# This embeds a template in the script without using strange `cat` syntax.
cd "$(dirname "$0")" # Go to the script's directory
export NAME="Tyler"
export VEHICLE="Ford Explorer"
export OVERDUE_LENGTH="2 months"
export OPTIONS=(
"Call a service representative at 1-800-000-0000 to discuss payment options"
"Return the vehicle immediately and pay a fine of 1 million dollars"
)
sed '0,/^# END/ d' "$(basename "$0")" | . ../mo
exit
# END
Attention {{NAME}},
You need to pay for the {{VEHICLE}} you are leasing from us.
It has been {{OVERDUE_LENGTH}} since your last payment.
At this point you must do one of the following:
{{#OPTIONS}}
* {{.}}
{{/OPTIONS}}

3
demo/fun-trip.mo Normal file
View File

@ -0,0 +1,3 @@
Hello, {{NAME}}
I hope your {{TIME_PERIOD}} was fun.

9
demo/using-arrays Executable file
View File

@ -0,0 +1,9 @@
#!/bin/bash
cd "$(dirname "$0")" # Go to the script's directory
export ARRAY=( one two "three three three" four five )
cat << EOF | . ../mo
Here are the items in the array:
{{#ARRAY}}
* {{.}}
{{/ARRAY}}
EOF

4
demo/using-strings Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
cd "$(dirname "$0")" # Go to the script's directory
export TEST="This is a test"
echo "Your message: {{TEST}}" | ../mo