grunt-bake
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -117,2 +117,13 @@ /* | ||
} | ||
}, | ||
foreach_bake: { | ||
options: { | ||
content: "test/fixtures/content.json", | ||
section: "en" | ||
}, | ||
files: { | ||
"tmp/foreach_bake.html": "test/fixtures/foreach_bake.html" | ||
} | ||
} | ||
@@ -119,0 +130,0 @@ }, |
{ | ||
"name": "grunt-bake", | ||
"description": "Bake external includes into files to create static pages with no server-side compilation time", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/MathiasPaumgarten/grunt-bake", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -300,4 +300,56 @@ # grunt-bake | ||
#### Foreach Loop | ||
Another special inline attribute is the `_foreach` attribute. This keyword expects a specific syntax and can be used both inline as well as pulling | ||
content from the json. This allows to loop over a set of values and using that value in the partial. | ||
It accepts an inline syntax: `_foreach="name:[mike, drew, steve]"` as well as a reference to an array in the json: `_foreach="name:authors.names"`. The values from the array can then be used with the key `name`. This key can be chosen arbitrarily. | ||
_app/base.html_: | ||
```html | ||
<html> | ||
<body> | ||
<ul class="first"> | ||
<!--(bake includes/li.html _foreach="name:[mike, drew, steve]")--> | ||
</ul> | ||
<ul class="second"> | ||
<!--(bake includes/li.html _foreach="name:authors.names")--> | ||
</ul> | ||
</body> | ||
</html> | ||
``` | ||
_app/includes/li.html_: | ||
```html | ||
<li>{{name}}</li> | ||
``` | ||
_app/content.json_: | ||
```json | ||
{ | ||
"authors: { | ||
"names": [ "jenna", "carla", "susy" ] | ||
} | ||
} | ||
``` | ||
This bake task will create _app/index.html_: | ||
```html | ||
<html> | ||
<body> | ||
<ul class="first"> | ||
<li>mike</li> | ||
<li>drew</li> | ||
<li>steve</li> | ||
</ul> | ||
<ul class="second"> | ||
<li>jenna</li> | ||
<li>carla</li> | ||
<li>susy</li> | ||
</ul> | ||
</body> | ||
</html> | ||
``` | ||
#### Costum process | ||
@@ -304,0 +356,0 @@ This example shows the use of a costum process funtion. |
@@ -56,2 +56,7 @@ /* | ||
// Regex to detect array syntax. | ||
var arrayRegex = /\[([\w\.\,\-]*)\]/; | ||
// Method to check wether file exists and warn if not. | ||
@@ -154,2 +159,17 @@ | ||
// Helper to either find values from JSON or inline values | ||
function getArrayValues( string, values ) { | ||
string = string.split( " " ).join( "" ); | ||
if ( arrayRegex.test( string ) ) { | ||
return string.match( arrayRegex )[ 1 ].split( "," ); | ||
} else { | ||
return resolveName( string, values ); | ||
} | ||
} | ||
// ===================== | ||
@@ -170,2 +190,4 @@ // -- RECURSIVE PARSE -- | ||
var inlineOptions = parseInlineOptions( attributes ); | ||
var array = []; | ||
var name = ""; | ||
@@ -180,4 +202,14 @@ if ( "_if" in inlineOptions ) { | ||
delete inlineOptions[ "_if" ]; | ||
} | ||
if ( "_foreach" in inlineOptions ) { | ||
var pair = inlineOptions[ "_foreach" ].split( ":" ); | ||
name = pair[ 0 ]; | ||
array = getArrayValues( pair[ 1 ], values ); | ||
delete inlineOptions[ "_foreach" ]; | ||
} | ||
grunt.util._.merge( values, inlineOptions ); | ||
@@ -194,3 +226,29 @@ | ||
return parse( includeContent, includePath, values ); | ||
if ( array.length > 0 ) { | ||
var fragment = ""; | ||
var newline = ""; | ||
var oldValue = values[ name ]; | ||
array.forEach( function( value, index ) { | ||
values[ name ] = value; | ||
newline = index > 0 ? "\n" : ""; | ||
fragment += newline + parse( includeContent, includePath, values ); | ||
} ); | ||
if ( oldValue ) { | ||
values[ name ] = oldValue; | ||
} else { | ||
delete values[ name ]; | ||
} | ||
return fragment; | ||
} else { | ||
return parse( includeContent, includePath, values ); | ||
} | ||
} ); | ||
@@ -197,0 +255,0 @@ } |
@@ -95,3 +95,13 @@ "use strict"; | ||
test.done(); | ||
}, | ||
foreachBake: function( test ) { | ||
test.expect( 1 ); | ||
var actual = grunt.file.read( "tmp/foreach_bake.html" ); | ||
var expected = grunt.file.read( "test/expected/foreach_bake.html" ); | ||
test.equal( actual, expected, "foreach bake" ); | ||
test.done(); | ||
} | ||
}; |
@@ -7,3 +7,9 @@ { | ||
}, | ||
"bar": false | ||
"bar": false, | ||
"items": [ | ||
"foo", | ||
"bar", | ||
"baz", | ||
"superman" | ||
] | ||
}, | ||
@@ -10,0 +16,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26715
35
420
400