Last active
August 29, 2015 14:08
-
-
Save atika/e7facba4713a49f01d42 to your computer and use it in GitHub Desktop.
Simple content generator from a template file and variables. Replace variables on a file with bash. I use it for an html email template.
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 | |
template_path="$1" | |
function usage (){ | |
echo -e "Usage: template <path to template> 'var1|content' 'var2|content'..."; | |
[[ -f "$template_path" ]] && { echo "Current variables:" $(cat $template_path |perl -ne '/#([A-Za-z1-9]+)#[^#].*/ && printf "%s, ", $1'); } | |
exit 1; | |
} | |
if [[ -f "$template_path" ]]; then | |
template=$(cat "$1") | |
shift | |
[ $# -eq 0 ] && usage | |
for param in "$@"; do | |
varName=$(echo $param | cut -d"|" -f1) | |
varContent=$(echo $param | cut -d"|" -f2) | |
template=$(echo $template | perl -pi -e "s!#$varName#!$varContent!g" ); | |
done | |
else | |
clear | |
echo "The template file doesn't exists!" | |
usage | |
fi | |
echo $template |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Display template file variables
template ~/path/to/your/template/file.html
Replace variables and output text (in your template: #MYVAR1#)
template ~/path/to/yout/template/file.html 'MYVAR1|variable one content' 'MYVAR2|variable two content'