Last active
March 26, 2020 12:03
-
-
Save RichardBronosky/e5a73e0d51039d165bb493c1e65e70c7 to your computer and use it in GitHub Desktop.
All other examples of rendering handlebars.js (double braces) templates in bash are way too complicated to put into a script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This function works with both GNU/Linux and macOS/BSD version of sed and grep | |
# Inspired by https://github.com/relaxdiego/renderest | |
render(){ | |
local template="$*" | |
for varname in $(<<<"$template" grep -oE '\{\{([A-Za-z0-9_]+)\}\}' | sed -En 's/.*\{\{([A-Za-z0-9_]+)\}\}.*/\1/p' | sort | uniq); do | |
template="$(<<<"$template" sed -E "s/\{\{$varname\}\}/$(sed 's_\\_\\\\\\\\_g;s_/_\\/_g' <<<"${!varname}")/g")" | |
done | |
printf "$template" | |
} | |
ipsum='//_M1_\\nnnn' | |
dolor='//_M2_\\tttt' | |
nascetur='//_M3_\\' | |
ridiculus='//_M4_\\' | |
quis='//_M5_\\' | |
consequat='//_M6_\\' | |
massa='//_M7_\\' | |
perd='//_M8_\\' | |
venenatis='//_M9_\\' | |
render 'Lorem {{ipsum}} {{dolor}} sit amet, consectetuer adipiscing elit. Aenean commodo ligula | |
eget {{dolor}}. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient | |
montes, {{nascetur}}{{ridiculus}} mus. Donec quam felis, ultricies nec, pellentesque | |
eu, pretium {{quis}}, sem. Nulla {{consequat}} {{massa}} {{quis}} enim. Donec pede justo, | |
fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, | |
im{{perd}}iet a, {{venenatis}} vitae, justo. Nullam {{perd}}{{perd}} eu pede mollis pretium.' | |
echo "THE END" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This function works with both GNU/Linux and macOS/BSD version of sed and grep | |
# Inspired by https://github.com/relaxdiego/renderest | |
render(){ | |
local template="$(cat)" | |
for varname in $(<<<"$template" grep -oE '\{\{([A-Za-z0-9_]+)\}\}' | sed -En 's/.*\{\{([A-Za-z0-9_]+)\}\}.*/\1/p' | sort | uniq); do | |
template="$(<<<"$template" sed -E "s/\{\{$varname\}\}/$(sed 's_\\_\\\\\\\\_g;s_/_\\/_g' <<<"${!varname}")/g")" | |
done | |
printf "$template" | |
} | |
ipsum='//_M1_\\nnnn' | |
dolor='//_M2_\\tttt' | |
nascetur='//_M3_\\' | |
ridiculus='//_M4_\\' | |
quis='//_M5_\\' | |
consequat='//_M6_\\' | |
massa='//_M7_\\' | |
perd='//_M8_\\' | |
venenatis='//_M9_\\' | |
render <<EOF | |
Lorem {{ipsum}} {{dolor}} sit amet, consectetuer adipiscing elit. Aenean commodo ligula | |
eget {{dolor}}. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient | |
montes, {{nascetur}}{{ridiculus}} mus. Donec quam felis, ultricies nec, pellentesque | |
eu, pretium {{quis}}, sem. Nulla {{consequat}} {{massa}} {{quis}} enim. Donec pede justo, | |
fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, | |
im{{perd}}iet a, {{venenatis}} vitae, justo. Nullam {{perd}}{{perd}} eu pede mollis pretium. | |
EOF | |
echo "THE END" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Lorem //_M1_\\nnnn //_M2_\\tttt sit amet, consectetuer adipiscing elit. Aenean commodo ligula | |
eget //_M2_\\tttt. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient | |
montes, //_M3_\\//_M4_\\ mus. Donec quam felis, ultricies nec, pellentesque | |
eu, pretium //_M5_\\, sem. Nulla //_M6_\\ //_M7_\\ //_M5_\\ enim. Donec pede justo, | |
fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, | |
im//_M8_\\iet a, //_M9_\\ vitae, justo. Nullam //_M8_\\//_M8_\\ eu pede mollis pretium.THE END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment