mirror of
https://github.com/tests-always-included/mo.git
synced 2024-12-18 08:26:21 +00:00
Added @key loop accessor
This commit is contained in:
parent
6dc284f05a
commit
c28bffe708
40
README.md
40
README.md
@ -118,6 +118,46 @@ 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
|
||||
```
|
||||
|
||||
|
||||
Concessions
|
||||
-----------
|
||||
|
||||
|
13
mo
13
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[@]}"
|
||||
|
@ -1,3 +1,3 @@
|
||||
<b>resque</b>
|
||||
<b>hub</b>
|
||||
<b>rip</b>
|
||||
<b>0 - resque</b>
|
||||
<b>1 - hub</b>
|
||||
<b>2 - rip</b>
|
||||
|
@ -1,3 +1,3 @@
|
||||
{{#repo}}
|
||||
<b>{{.}}</b>
|
||||
<b>{{@index}} - {{.}}</b>
|
||||
{{/repo}}
|
||||
|
4
tests/assoc-array.env
Normal file
4
tests/assoc-array.env
Normal file
@ -0,0 +1,4 @@
|
||||
declare -A repo
|
||||
repo[resque]="Resque"
|
||||
repo[hub]="Hub"
|
||||
repo[rip]="Rip"
|
3
tests/assoc-array.expected
Normal file
3
tests/assoc-array.expected
Normal file
@ -0,0 +1,3 @@
|
||||
<b>hub - Hub</b>
|
||||
<b>rip - Rip</b>
|
||||
<b>resque - Resque</b>
|
3
tests/assoc-array.template
Normal file
3
tests/assoc-array.template
Normal file
@ -0,0 +1,3 @@
|
||||
{{#repo}}
|
||||
<b>{{@key}} - {{.}}</b>
|
||||
{{/repo}}
|
Loading…
Reference in New Issue
Block a user