mirror of
https://github.com/tests-always-included/mo.git
synced 2024-12-22 01:37:47 +00:00
Merge pull request #57 from Swivelgames/loop-accessors
Feature: Key and Index Accessors Inside Loops
This commit is contained in:
commit
d6794de1e2
71
README.md
71
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.
|
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
13
mo
@ -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[@]}"
|
||||||
|
@ -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>
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
{{#repo}}
|
{{#repo}}
|
||||||
<b>{{.}}</b>
|
<b>{{@index}} - {{.}}</b>
|
||||||
{{/repo}}
|
{{/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