Skip to content

Instantly share code, notes, and snippets.

@whyvez
Created January 28, 2015 13:42
Show Gist options
  • Select an option

  • Save whyvez/07366bdebd594fccdf07 to your computer and use it in GitHub Desktop.

Select an option

Save whyvez/07366bdebd594fccdf07 to your computer and use it in GitHub Desktop.
Simple AngularJS template cache generator using AWK.
#!/usr/bin/awk -F"\n" -f
# usage
# the html templates need to be a one liner i.e. no line breaks
# $ ./scripts/ng-template-cache.awk -v module=<module> <templates.html > <templates.js>
# $ ./scripts/ng-template-cache.awk -v module=mvApp app/partials/*.html > app/js/templates.js
BEGIN { print "angular.module('"module"').run(['$templateCache', function($templateCache) {" }
{
# split file path using /
n=split(FILENAME, p, "/");
# escape " and ' with \" and \'
gsub(/"/, "\\\"");
gsub(/\047/, "\\\047");
# grab last item in p array which is the filename i.e. mytemplate.html
url="partials/"p[n]
# set content = first row (should only be one)
contents=$1
print "$templateCache.put('"url"','"contents"');"
}
END { print "}]);" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment