Skip to content

Commit

Permalink
Initial test of html2js for partials
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Sep 18, 2012
1 parent f6792e8 commit aa56bbf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
dist
dist
*.tpl.html.js
26 changes: 24 additions & 2 deletions grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = function (grunt) {
src: {
js: ['src/**/*.js'],
html: ['src/index.html'],
tpl: ['src/**/*.tpl.html'],
less: ['src/modules/*/less/*.less'] // recess:build doesn't accept ** in its file patterns
},
test: {
Expand All @@ -24,6 +25,7 @@ module.exports = function (grunt) {
lint:{
files:['grunt.js', '<config:src.js>', '<config:test.js>']
},
html2js: { tpl: '<config:src.tpl>' },
concat:{
dist:{
src:['<banner:meta.banner>', '<config:src.js>'],
Expand Down Expand Up @@ -74,8 +76,8 @@ module.exports = function (grunt) {
});

// Default task.
grunt.registerTask('default', 'lint test build');
grunt.registerTask('build', 'concat recess:build concatPartials index');
grunt.registerTask('default', 'build lint test');
grunt.registerTask('build', 'html2js concat recess:build concatPartials index');
grunt.registerTask('release', 'lint test min recess:min concatPartials index');

// Testacular stuff
Expand Down Expand Up @@ -145,4 +147,24 @@ module.exports = function (grunt) {
//tests
grunt.file.write(testPath + '/unit/' + moduleName + 'Spec.js', grunt.template.process(grunt.file.read('build/scaffolding/test.js'), tplvars));
});

// HTML-2-JS Templates
var TPL = 'angular.module("<%= file %>", []).run(function($templateCache) {\n' +
' $templateCache.put("<%= file %>",\n "<%= content %>");\n' +
'});\n';

var escapeContent = function(content) {
return content.replace(/"/g, '\\"').replace(/\r?\n/g, '" +\n "');
};

grunt.registerMultiTask('html2js', 'Generate js version of html template.', function() {
var files = grunt._watch_changed_files || grunt.file.expand(this.data);

files.forEach(function(file) {
grunt.file.write(file + '.js', grunt.template.process(TPL, {
file: file,
content: escapeContent(grunt.file.read(file))
}));
});
});
};
9 changes: 1 addition & 8 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<!DOCTYPE html>
<html lang="en" ng-app="app">
<head>

<title>AScrum</title>
<link rel="stylesheet" type="text/css" href="<%= grunt.config.get('pkg.name') %>.css"/>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.js"></script>
<script type="text/javascript" src="<%= grunt.config.get('pkg.name') %>.js"></script>

<!-- partials:start -->
<%= grunt.file.read('dist/partials.tpl.html') %>
<!-- partials:end -->
<link rel="stylesheet" type="text/css" href="<%= grunt.config.get('pkg.name') %>.css"/>
</head>

<body ng-controller="AppCtrl">
Expand Down

0 comments on commit aa56bbf

Please sign in to comment.