Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested layout: layout with its own layout #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.DS_Store
node_modules
*.swp
*~
#*#
.#*#
53 changes: 52 additions & 1 deletion lib/helpers/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ var dataTree = exports.dataTree = function (filename) {
*
*/

exports.getCurrent = function(sourcePath){
var getCurrent = exports.getCurrent = function(sourcePath){

// this could be a tad smarter
var namespace = sourcePath.split(".")[0].split("/")
Expand Down Expand Up @@ -503,3 +503,54 @@ exports.isJavaScript = function(filePath){



exports.findLayout = function(root, filePath, layout, explicitOnly) {
/**
* Layout Priority:
*
* 1. passed into partial() function.
* 2. in `_data.json` file.
* 3. default layout.
* 4. no layout
*/

// 1. check for layout passed in
if(layout === undefined){
var data = dataTree(root)

// 2. _data.json layout
// TODO: Change this lookup relative to path.
var current = getCurrent(filePath)
var templateLocals = walkData(current.path, data)

if(templateLocals && templateLocals.hasOwnProperty('layout')){
if(templateLocals['layout'] === false){
layout = null
} else if(templateLocals['layout'] !== true){
// relative path
var dirname = path.dirname(filePath)
var layoutPriorityList = buildPriorityList(path.join(dirname, templateLocals['layout'] || ""))

// absolute path (fallback)
layoutPriorityList.push(templateLocals['layout'])

// return first existing file
// TODO: Throw error if null
layout = findFirstFile(root, layoutPriorityList)
}
}

// if requested to only find explicit layout, don't look any further
if(explicitOnly && layout === undefined){
layout = null
}

// 3. default _layout file
if(layout === undefined){
layout = findNearestLayout(root, path.dirname(filePath))
}

// 4. no layout (do nothing)
}

return layout
}
8 changes: 7 additions & 1 deletion lib/template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,13 @@ var scope = module.exports = function(projectPath, partentLocals){
* render the layout (if there is one) with the output of the template as yield.
*/

if(layout) output = scope(projectPath, locals).partial(layout, { yield: output })
if(layout) {
var layout2 = helpers.findLayout(projectPath, layout, undefined, true)
var layoutLocals = { yield: output }
if(layout2 != layout)
layoutLocals.layout = layout2
output = scope(projectPath, locals).partial(layout, layoutLocals)
}

return output

Expand Down
43 changes: 1 addition & 42 deletions lib/terraform.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,48 +80,7 @@ exports.root = function(root, globals){
*/

locals.current = helpers.getCurrent(filePath)


/**
* Layout Priority:
*
* 1. passed into partial() function.
* 2. in `_data.json` file.
* 3. default layout.
* 4. no layout
*/

// 1. check for layout passed in
if(!locals.hasOwnProperty('layout')){

// 2. _data.json layout
// TODO: Change this lookup relative to path.
var templateLocals = helpers.walkData(locals.current.path, data)

if(templateLocals && templateLocals.hasOwnProperty('layout')){
if(templateLocals['layout'] !== true){

// relative path
var dirname = path.dirname(filePath)
var layoutPriorityList = helpers.buildPriorityList(path.join(dirname, templateLocals['layout'] || ""))

// absolute path (fallback)
layoutPriorityList.push(templateLocals['layout'])

// return first existing file
// TODO: Throw error if null
locals['layout'] = helpers.findFirstFile(root, layoutPriorityList)

}
}

// 3. default _layout file
if(!locals.hasOwnProperty('layout')){
locals['layout'] = helpers.findNearestLayout(root, path.dirname(filePath))
}

// 4. no layout (do nothing)
}
locals.layout = helpers.findLayout(root, filePath, locals.layout)

/**
* TODO: understand again why we are doing this.
Expand Down
8 changes: 7 additions & 1 deletion test/fixtures/layouts/deep/nested/deeply/_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
},
"relative2":{
"layout": "../../_layout.jade"
},
"relative3":{
"layout": "_layoutWithLayout.jade"
},
"_layoutWithLayout":{
"layout": "../../_layout.jade"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h2 Layout with Its Own Layout
!= yield
1 change: 1 addition & 0 deletions test/fixtures/layouts/deep/nested/deeply/relative3.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h3 Relative 3
2 changes: 2 additions & 0 deletions test/fixtures/layouts/none/false.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1 Should Not Appear
!= yield
5 changes: 5 additions & 0 deletions test/fixtures/layouts/none/false/_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"layout-false":{
"layout": false
}
}
1 change: 1 addition & 0 deletions test/fixtures/layouts/none/false/layout-false.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h2 Layout Explicitly Set to False
20 changes: 20 additions & 0 deletions test/layouts.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ describe("layout", function(){
done()
})
})

it("should render with no layout too", function(done){
poly.render("false/layout-false.jade", function(errors, body){
should.not.exist(errors)
should.exist(body)
body.should.eql("<h2>Layout Explicitly Set to False</h2>")
done()
})
})
})

describe("base", function(){
Expand Down Expand Up @@ -76,6 +85,17 @@ describe("layout", function(){
})
})

it("should render with explicit layout 3 and then layout 4", function(done){
poly.render("nested/deeply/relative3.jade", function(errors, body){
should.not.exist(errors)
should.exist(body)
body.should.include("<h1>Layout in Base</h1>")
body.should.include("<h2>Layout with Its Own Layout</h2>")
body.should.include("<h3>Relative 3</h3>")
done()
})
})

})

})