Merge pull request #57 from Swivelgames/loop-accessors

Feature: Key and Index Accessors Inside Loops
This commit is contained in:
Tyler Akins 2023-04-05 20:51:13 -05:00 committed by GitHub
commit d6794de1e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 5 deletions

View File

@ -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. 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 Concessions
----------- -----------

13
mo
View File

@ -666,7 +666,7 @@ moLoop() {
moParse() { moParse() {
# Keep naming variables mo* here to not overwrite needed variables # Keep naming variables mo* here to not overwrite needed variables
# used in the string replacements # used in the string replacements
local moArgs moBlock moContent moCurrent moIsBeginning moNextIsBeginning moTag local moArgs moBlock moContent moCurrent moIsBeginning moNextIsBeginning moTag moKey
moCurrent=$2 moCurrent=$2
moIsBeginning=$3 moIsBeginning=$3
@ -782,6 +782,17 @@ moParse() {
moShow "$moTag" "$moCurrent" 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 # Normal environment variable or function call
moStandaloneDenied moContent "${moContent[@]}" moStandaloneDenied moContent "${moContent[@]}"

View File

@ -1,3 +1,3 @@
<b>resque</b> <b>0 - resque</b>
<b>hub</b> <b>1 - hub</b>
<b>rip</b> <b>2 - rip</b>

View File

@ -1,3 +1,3 @@
{{#repo}} {{#repo}}
<b>{{.}}</b> <b>{{@index}} - {{.}}</b>
{{/repo}} {{/repo}}

4
tests/assoc-array.env Normal file
View File

@ -0,0 +1,4 @@
declare -A repo
repo[resque]="Resque"
repo[hub]="Hub"
repo[rip]="Rip"

View File

@ -0,0 +1,3 @@
<b>hub - Hub</b>
<b>rip - Rip</b>
<b>resque - Resque</b>

View File

@ -0,0 +1,3 @@
{{#repo}}
<b>{{@key}} - {{.}}</b>
{{/repo}}