A BASH-powered extendable template engine.
Why? Because I felt like it! Plus, I needed something for my blog which is also powered by BASH.
It is nothing fancy, but it does the job.
#!/bin/bash
. ./be
be_bold() {
local input
input=$(cat -)
echo "<strong>${input}</strong>"
}
be_replace() {
local input
input=$(cat -)
echo "${input//$1/$2}"
}
be_capitalize() {
local input
input=$(cat -)
echo "${input^}"
}
be_equals() {
local block
block=$(cat -)
if [[ "$1" == "$2" ]]; then
be <<< "$block"
else
# Failed, return nothing.
echo ""
fi
}
NAME="Joe"
LIKES="hockey,soccer"
TPL=$(cat <<EOF
Well, well, hello {{NAME|APPEND e|APPEND e}}!
{{@ASSIGN GOODBYE}}Goodbye, {{NAME|CAPITALIZE}}!{{/ASSIGN GOODBYE}}
{{#LIKES}}
So {{NAME|REPLACE "e" "ey"|BOLD}}, I heard you like:
{{@FOREACH LIKES ,}}
{{@ASSIGN IS_HOCKEY}}{{@EQUALS VALUE "hockey"}}true{{/EQUALS VALUE "hockey"}}{{/ASSIGN IS_HOCKEY}}
{{KEY1}}) {{VALUE|CAPITALIZE}}{{#IS_HOCKEY}} (love!){{/IS_HOCKEY}}{{^LAST}}; and {{/LAST}}
{{/FOREACH LIKES ,}}
{{@RAW}}Won't be processed: {{NAME}}{{/RAW}}
{{/LIKES}}
{{GOODBYE}}
EOF
)
echo "$TPL" | be
Output:
Well, well, hello Joeee!
So <strong>Joey</strong>, I heard you like:
1) Hockey (love!); and
2) Soccer
Won't be processed: {{NAME}}
{{VAR}}
Will replace with the value of VAR
.
Piping: {{VAR|UPPERCASE|REPLACE X Y}}
Will replace VAR
with the value of VAR
, then run it through a custom function UPPERCASE
, then run it througha custom function REPLACE
where it has the arguments of X
and Y
(replace X
with Y
).
Supports for quoting as well:
{{VAR|UPPERCASE|REPLACE "X" "Y"}}
; or{{VAR|UPPERCASE|REPLACE "\"" "'"}}
{{#VAR}}Works!{{/VAR}}
Will show Works!
if VAR
is defined.
{{^VAR}}Works!{{/VAR}}
Will only show Works!
if VAR
is not defined.
{{@FOREACH VAR DEL}}{{KEY}}:{{VALUE}}{{^LAST}}, {{/LAST}}{{/FOREACH VAR DEL}}
Will accept a variable to loop VAR
with optional delimiter DEL
. If DEL
is not provided, it will simply loop by space.
The following special variables are usable inside a loop:
KEY
- Current key indexKEY0
- Current key index, zero-basedKEY1
- Current key index + 1VALUE
- Value for current keyFIRST
- If this is the first iterationLAST
- If this is the last iteration
{{>templates/_partial}}
Will include and process the file templates/_partial
.
{{@RAW}}Will be skipped {{NAME}}{{/RAW}}
Will skip over anything defined inside.
{{@ASSIGN HELLO}}Hello! {{NAME|CAPITALIZE}}{{/ASSIGN HELLO}}
Then, use {{HELLO}}
to display.
Simply define your own function with a prefix of be_
. The subject and arguments will be passed in to work with.
The subject is subjective to the type. If it is a block, then the subject is the block content. If it is a pipe in a variable, then the subject is the parsed variable (thus far).
# Pipe example.
be_bold() {
local input
input=$(cat -)
echo "<strong>${input}</strong>"
}
NAME="Joe"
echo "{{NAME|BOLD}}" | be
# Output: "<strong>Joe</strong>"
# Pipe example #2.
be_bold() {
local input
input=$(cat -)
echo "<$1>${input}</$1>"
}
NAME="Joe"
echo "{{NAME|BOLD em}}" | be
# Output: "<em>Joe</em>"
# Function example.
be_bold() {
local input
input=$(cat -)
echo "<strong>${input}</strong>"
}
NAME="Joe"
echo "{{@BOLD}}{{NAME}}{{/BOLD}}" | be
# Output: "<strong>Joe</strong>"
This project is released under the MIT license.