diff --git a/README.md b/README.md index b193a09..4f256c0 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,77 @@ There are more scripts available in the [demos directory](demo/) that could help There are additional features that the program supports. Try using `mo --help` to see what is available. +Enhancements +----------- + +In addition to many of the features built-in to Mustache, `mo` includes a number of unique features that make it a bit more powerful. + +### Loop @key + +`mo` implements Handlebar's `@key` references for outputting the key inside of a loop: + +Env: +```bash +myarr=( foo bar ) + +# Bash v4+ +declare -A myassoc +myassoc[hello]="mo" +myassoc[world]="is great" +``` + +Template: +```handlebars +{{#myarr}} + - {{@key}} {{.}} +{{/myarr}} + +{{#myassoc}} + * {{@key}} {{.}} +{{/myassoc}} +``` + +Output: +```markdown + - 0 foo + - 1 bar + + * hello mo + * world is great +``` + + +### Helpers / Function Arguments with + +Function Arguments are not a part of the official Mustache implementation, and are more often associated with Handlebar's Helper functionality. + +`mo` allows for passing strings to functions. + +```handlebars +{{myfunc foo bar}} +``` + +For security reasons, this arguments are not immediately available to function calls without a flag. + +#### with `--allow-function-arguments` + +```bash +myfunc() { + # Outputs "foo, bar" + echo "$1, $2"; +} +``` + +#### Using `$MO_FUNCTION_ARGS` + +```bash +myfunc() { + # Outputs "foo, bar" + echo "${MO_FUNCTION_ARGS[0]}, ${MO_FUNCTION_ARGS[1]}"; +} +``` + + Concessions ----------- diff --git a/mo b/mo index c928d7c..bb14fbf 100755 --- a/mo +++ b/mo @@ -666,7 +666,7 @@ moLoop() { moParse() { # Keep naming variables mo* here to not overwrite needed variables # used in the string replacements - local moArgs moBlock moContent moCurrent moIsBeginning moNextIsBeginning moTag + local moArgs moBlock moContent moCurrent moIsBeginning moNextIsBeginning moTag moKey moCurrent=$2 moIsBeginning=$3 @@ -782,6 +782,17 @@ moParse() { moShow "$moTag" "$moCurrent" ;; + '@key') + # Special vars + moStandaloneDenied moContent "${moContent[@]}" + # Current content (environment variable or function) + if [[ "$moCurrent" == *.* ]]; then + echo -n "${moCurrent#*.}" + else + echo -n "$moCurrent" + fi + ;; + *) # Normal environment variable or function call moStandaloneDenied moContent "${moContent[@]}" diff --git a/tests/array.expected b/tests/array.expected index a45557c..94d9b8e 100644 --- a/tests/array.expected +++ b/tests/array.expected @@ -1,3 +1,3 @@ - resque - hub - rip + 0 - resque + 1 - hub + 2 - rip diff --git a/tests/array.template b/tests/array.template index 4c61e9c..8671607 100644 --- a/tests/array.template +++ b/tests/array.template @@ -1,3 +1,3 @@ {{#repo}} - {{.}} + {{@index}} - {{.}} {{/repo}} diff --git a/tests/assoc-array.env b/tests/assoc-array.env new file mode 100644 index 0000000..934cade --- /dev/null +++ b/tests/assoc-array.env @@ -0,0 +1,4 @@ +declare -A repo +repo[resque]="Resque" +repo[hub]="Hub" +repo[rip]="Rip" diff --git a/tests/assoc-array.expected b/tests/assoc-array.expected new file mode 100644 index 0000000..6ef4ced --- /dev/null +++ b/tests/assoc-array.expected @@ -0,0 +1,3 @@ + hub - Hub + rip - Rip + resque - Resque diff --git a/tests/assoc-array.template b/tests/assoc-array.template new file mode 100644 index 0000000..771906f --- /dev/null +++ b/tests/assoc-array.template @@ -0,0 +1,3 @@ +{{#repo}} + {{@key}} - {{.}} +{{/repo}}