grunt-hogan-client
Advanced tools
Comparing version 0.1.1 to 0.1.2
13
grunt.js
@@ -9,2 +9,3 @@ module.exports = function(grunt) { | ||
hoganclient: { | ||
// defining a tmpl | ||
namespaced: { | ||
@@ -17,5 +18,17 @@ options: { | ||
}, | ||
// not defining any variable name | ||
global: { | ||
src: ['test/templates/**/*.hogan'], | ||
dest: 'test/tmp/bar.js' | ||
}, | ||
// wrapping the temnplates in custom code. | ||
wrapping: { | ||
options: { | ||
wrap: { | ||
start: 'head.ready(function(){', | ||
end: '});' | ||
} | ||
}, | ||
src: ['test/templates/wrap.mustache'], | ||
dest: 'test/tmp/wrapping.js' | ||
} | ||
@@ -22,0 +35,0 @@ }, |
{ | ||
"name": "grunt-hogan-client", | ||
"description": "compiles hogan templates into ready to use includes", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"homepage": "https://github.com/ullmark/grunt-hogan-client", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -49,7 +49,5 @@ # grunt-hogan-client | ||
```javascript | ||
(function compileHoganTemplates() { | ||
window.tmpl=window.tmpl||{}; | ||
window.tmpl.item=Hogan.compile('<li><h2>{{title}}</h2><p>{{text}}</p></li>'); | ||
window.tmpl.list=Hogan.compile('<ul id="a-list">{{#items}}{{>item}}{{/items}}</ul>'); | ||
}()); | ||
window.tmpl=window.tmpl||{}; | ||
window.tmpl.item=Hogan.compile('<li><h2>{{title}}</h2><p>{{text}}</p></li>'); | ||
window.tmpl.list=Hogan.compile('<ul id="a-list">{{#items}}{{>item}}{{/items}}</ul>'); | ||
``` | ||
@@ -62,2 +60,20 @@ ready to use/include/concat etc in your app like this. | ||
### Wrapping the templates. | ||
I made this plugin for a very specific case where I also needed to wrap | ||
the templates in some code due to async loading of Hogan using | ||
[head.js](http://headjs.com/). | ||
Since this task is a code generator I decided to add the **wrap** | ||
property to the options. | ||
#### config | ||
```javascript | ||
options: { | ||
wrap: { | ||
start: 'head.ready(function() {', | ||
end: '});' | ||
} | ||
} | ||
``` | ||
## Todo | ||
@@ -64,0 +80,0 @@ I guess there will be need to tweek the regex that cleans the template. |
@@ -42,3 +42,4 @@ /* | ||
grunt.registerHelper('hoganclient', function(files, options) { | ||
var src = ''; | ||
var src = '', | ||
indent = ''; | ||
@@ -49,12 +50,23 @@ options = grunt.utils._.defaults(options || {}, { | ||
src += '(function compileHoganTemplates() {' + grunt.utils.linefeed; | ||
src += ' ' + options.variable + '=' + options.variable + '||{};' + grunt.utils.linefeed; | ||
if (options.wrap) { | ||
indent = ' '; | ||
} | ||
if (options.wrap && options.wrap.start) { | ||
src += options.wrap.start; | ||
src += grunt.utils.linefeed; | ||
} | ||
src += indent + options.variable + '=' + options.variable + '||{};' + grunt.utils.linefeed; | ||
files.map(function(filepath) { | ||
var name = path.basename(filepath, path.extname(filepath)); | ||
var file = grunt.file.read(filepath).replace(cleaner, '').replace(/'/, '\''); | ||
src += ' ' + options.variable + '.' + name + '=Hogan.compile(\'' + file + '\');' + grunt.utils.linefeed; | ||
src += indent + options.variable + '.' + name + '=Hogan.compile(\'' + file + '\');' + grunt.utils.linefeed; | ||
}); | ||
src += '}());' + grunt.utils.linefeed; | ||
if (options.wrap && options.wrap.end) { | ||
src += options.wrap.end; | ||
src += grunt.utils.linefeed; | ||
} | ||
@@ -61,0 +73,0 @@ return src; |
@@ -1,5 +0,3 @@ | ||
(function compileHoganTemplates() { | ||
window.tmpl=window.tmpl||{}; | ||
window.tmpl.bar=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); | ||
window.tmpl.foo=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); | ||
}()); | ||
window.tmpl=window.tmpl||{}; | ||
window.tmpl.bar=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); | ||
window.tmpl.foo=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); |
@@ -1,5 +0,3 @@ | ||
(function compileHoganTemplates() { | ||
foo.tmpl=foo.tmpl||{}; | ||
foo.tmpl.bar=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); | ||
foo.tmpl.foo=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); | ||
}()); | ||
foo.tmpl=foo.tmpl||{}; | ||
foo.tmpl.bar=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); | ||
foo.tmpl.foo=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); |
@@ -6,3 +6,5 @@ | ||
var grunt = require('grunt'); | ||
var grunt = require('grunt'), | ||
fs = require('fs'), | ||
path = require('path'); | ||
@@ -29,13 +31,15 @@ /* | ||
exports.namespaced = { | ||
exports.acceptanceTests = { | ||
compile: function(test) { | ||
test.expect(2); | ||
var expectedFiles = grunt.file.expandFiles('test/expected/**/*.js'); | ||
test.expect(expectedFiles.length); | ||
var actual = grunt.file.read('test/tmp/bar.js'); | ||
var expected = grunt.file.read('test/expected/bar.js'); | ||
test.equal(expected, actual, 'Should compile to correct javascript format using new global variable'); | ||
var expected, actual, basename; | ||
expectedFiles.forEach(function(expectedSrc) { | ||
basename = path.basename(expectedSrc); | ||
expected = grunt.file.read(expectedSrc); | ||
actual = grunt.file.read(path.join('test/tmp', basename)); | ||
actual = grunt.file.read('test/tmp/foo.js'); | ||
expected = grunt.file.read('test/expected/foo.js'); | ||
test.equal(expected, actual, 'Should compile to correct javascript format using existing variable'); | ||
test.equal(expected, actual, 'expected "' + basename + '" to match'); | ||
}); | ||
@@ -45,2 +49,2 @@ test.done(); | ||
}; | ||
}()); | ||
}()); |
@@ -1,5 +0,3 @@ | ||
(function compileHoganTemplates() { | ||
window.tmpl=window.tmpl||{}; | ||
window.tmpl.bar=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); | ||
window.tmpl.foo=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); | ||
}()); | ||
window.tmpl=window.tmpl||{}; | ||
window.tmpl.bar=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); | ||
window.tmpl.foo=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); |
@@ -1,5 +0,3 @@ | ||
(function compileHoganTemplates() { | ||
foo.tmpl=foo.tmpl||{}; | ||
foo.tmpl.bar=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); | ||
foo.tmpl.foo=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); | ||
}()); | ||
foo.tmpl=foo.tmpl||{}; | ||
foo.tmpl.bar=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); | ||
foo.tmpl.foo=Hogan.compile('<div class="something"><ul>{{#foo}}<li><h1>{{bar}}</h1></li>{{/foo}}</ul></div>'); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
10765
17
184
89
2