From 6a6aadca5020d3aaf4f62ce25bfd078a033a1a1d Mon Sep 17 00:00:00 2001 From: Tyler Akins Date: Wed, 28 Jan 2015 09:44:44 -0600 Subject: [PATCH] Adding more demo files --- README.md | 36 +++++++++++++++++++++++++++++++++--- demo/embedded-template | 26 ++++++++++++++++++++++++++ demo/fun-trip.mo | 3 +++ demo/using-arrays | 9 +++++++++ demo/using-strings | 4 ++++ 5 files changed, 75 insertions(+), 3 deletions(-) create mode 100755 demo/embedded-template create mode 100644 demo/fun-trip.mo create mode 100755 demo/using-arrays create mode 100755 demo/using-strings diff --git a/README.md b/README.md index 8199336..24f43c4 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/demo/embedded-template b/demo/embedded-template new file mode 100755 index 0000000..7a0aeb8 --- /dev/null +++ b/demo/embedded-template @@ -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}} diff --git a/demo/fun-trip.mo b/demo/fun-trip.mo new file mode 100644 index 0000000..c9d43b9 --- /dev/null +++ b/demo/fun-trip.mo @@ -0,0 +1,3 @@ +Hello, {{NAME}} + +I hope your {{TIME_PERIOD}} was fun. diff --git a/demo/using-arrays b/demo/using-arrays new file mode 100755 index 0000000..2e829e2 --- /dev/null +++ b/demo/using-arrays @@ -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 diff --git a/demo/using-strings b/demo/using-strings new file mode 100755 index 0000000..12db6d6 --- /dev/null +++ b/demo/using-strings @@ -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