Skip to content

Instantly share code, notes, and snippets.

@atika
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atika/e7facba4713a49f01d42 to your computer and use it in GitHub Desktop.
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.
#!/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
@atika
Copy link
Author

atika commented Nov 8, 2014

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment