Skip to content

Instantly share code, notes, and snippets.

@sebastianha
Created October 28, 2014 10:39
Show Gist options
  • Save sebastianha/613e37aedfd99a1e9fa2 to your computer and use it in GitHub Desktop.
Save sebastianha/613e37aedfd99a1e9fa2 to your computer and use it in GitHub Desktop.
Create inline template from html template by replacing templateUrl in AngularJS directives
# This commandline removes all tabs from a directive template file, then adds '"' at the beginning of every line, '" +' at the end of every line, then replaces 'templateUrl' in directive file with the new contents of the template.
# This is very useful when an originally externalized template file for a AngularJS directive needs to be inline for e.g. minimizing. The original template file is no longer needed.
sed -e 's/^[ \t]*//' -e 's/\"/\\\"/g' -e 's/^/"/g' -e 's/$/" +/g' -e '1 i\template: \"\" +' -e "\$a\"\"," TEMPLATE.html > TEMPLATE.html.tmp && sed -e '/templateUrl/{r TEMPLATE.html.tmp' -e 'd}' DIRECTIVE.js > DIRECTIVE-tpl.js && rm TEMPLATE.html.tmp
# EXAMPLE:
############################################################
# DIRECTIVE.js:
############################################################
# angular.module("test", []).directive("test", function() {
# return {
# scope : {},
# restrict : "E",
# replace : "true",
# templateUrl: "TEMPLATE.html",
# link : function(scope) {
# // do something
# }
# };
# });
############################################################
# TEMPLATE.html:
############################################################
# <div>
# bla blubb
# </div>
############################################################
# DIRECTIVE-tpl.js:
############################################################
# angular.module("test", []).directive("test", function() {
# return {
# scope : {},
# restrict : "E",
# replace : "true",
# template: "" +
# "<div>" +
# "bla blubb" +
# "</div>" +
# "",
# link : function(scope) {
# // do something
# }
# };
# });
############################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment