Skip to content

Commit

Permalink
Added an option to remove extra whitespace around hbs tags
Browse files Browse the repository at this point in the history
It annoys me and apparently many others that handlebars does not strip extra whitespace around its elements when compiling. 

handlebars-lang/handlebars.js#336

This is a quick hack to make it strip that extra whitespace as if the rows with only a single hbs tag were not separate rows. This pull possibly has some bugs in its regexp, but it's a start.

I thought that maybe this should be toggleable by the user, so only by adding an option: removeHbsWhitespace:true to grunt config of assemble will enable this feature. (without it, no ill effects should happen)

With removeHbsWhitespace enabled code like this:
```
{{#each collection}}
<div>{{this}}</div>
{{/each}}
```
normally resulting this:
```
<div>ele1</div>

<div>ele2</div>

<div>ele3</div>
```

would be preprocessed to be like this:
´´´
{{#each collection}}<div>{{this}}</div>{{/each}}
´´´
resulting this:
```
<div>ele1</div>
<div>ele2</div>
<div>ele3</div>
```

This feels more natural and helps creating cleaner and more readable layouts, partials and page code.
  • Loading branch information
Arkkimaagi committed May 4, 2013
1 parent 1cd7109 commit 6e9b039
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tasks/assemble.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ module.exports = function(grunt) {
var utils = assemble.Utils;
var extensions = utils.ExtensionMap;

//Removes extra whitespace around {{}}-elements somewhat like .mustache does.
var removeHbsWhitespace = function(assemble,filecontent){
if(assemble.options.removeHbsWhitespace){
filecontent = filecontent.replace(/\n\s*(\{\{\{[^}]+?\}\}\}\n)/gi,"$1");
filecontent = filecontent.replace(/\n\s*(\{\{[^}]+?\}\}\n)/gi,"$1");
}
return filecontent;
};

grunt.registerMultiTask('assemble', 'Compile template files with specified engines', function(){

Expand Down Expand Up @@ -133,6 +141,9 @@ module.exports = function(grunt) {

var partial = fs.readFileSync(filepath, 'utf8');

//If remove hbs whitespace...
partial = removeHbsWhitespace(assemble,partial);

partial = assemble.engine.compile(partial, {
preprocessers: [
assemble.yamlPreprocessor(filename, function(output) {
Expand Down Expand Up @@ -275,6 +286,9 @@ module.exports = function(grunt) {
grunt.verbose.writeln('compiling page ' + filename.magenta);
var pageContext = {};

//If remove hbs whitespace...
page = removeHbsWhitespace(assemble,page);

page = assemble.engine.compile(page, {
preprocessers: [
assemble.yamlPreprocessor(filename, function(output) {
Expand Down Expand Up @@ -548,6 +562,9 @@ module.exports = function(grunt) {
layout = fs.readFileSync(layout, 'utf8');
}

//If remove hbs whitespace...
layout = removeHbsWhitespace(assemble,layout);

var layoutData = {};
layout = assemble.engine.compile(layout, {
preprocessers: [
Expand Down

0 comments on commit 6e9b039

Please sign in to comment.