Skip to content

Commit

Permalink
primitive layouts working
Browse files Browse the repository at this point in the history
  • Loading branch information
Kellen Presley committed Dec 5, 2009
1 parent 255e882 commit 8267974
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 37 deletions.
26 changes: 8 additions & 18 deletions picard/lib/picard/haml.js
Expand Up @@ -387,21 +387,11 @@ Haml.parse = function (text) {
return haml;
};

// Exports for node
if (exports) {
exports.parse = Haml.parse;
exports.to_html = Haml.to_html;
exports.render = function(scope, callback){
var json = Haml.parse.call(scope, scope.body)

scope.body = Haml.to_html(json).replace('\n\n', '\n')
callback(scope.body)
}
// exports.render = function (scope, filename, callback) {
// process.fs.cat(filename).addCallback(function (text) {
// var json = Haml.parse.call(scope, text);
// callback(Haml.to_html(json).replace("\n\n", "\n"));
// });
// }
}

exports.parse = Haml.parse;
exports.to_html = Haml.to_html;
exports.render = function(scope, callback){
var json = Haml.parse.call(scope, scope.body)
scope.body = Haml.to_html(json).replace('\n\n', '\n')
require('sys').puts(scope.body)
callback(scope.body)
}
48 changes: 34 additions & 14 deletions picard/lib/picard/request_extensions.js
Expand Up @@ -80,15 +80,10 @@ var request_extensions = {
scope.headers.push([ 'Content-Type', scope.type || 'text/html' ])
scope.headers = req.set_cookies(scope.headers)

if(scope.template){
if(scope.template)
req.build_document(scope, true)
// var template_path = picard.env.root + picard.env.views + '/' + scope.template
// haml.render(scope, template_path, function(body){
// req.send_data(status, headers, body, encoding)
// })
} else {
else
req.send_data(scope)
}

sys.puts((this._method || this.method).toUpperCase() + ' ' + this.uri.path + ' ' + scope.status)

Expand Down Expand Up @@ -162,7 +157,7 @@ var request_extensions = {

build_document: function(scope, first_read){
var basepath = picard.env.root + picard.env.views + '/'
var filename = basepath + scope.template
var filename = basepath + scope.template + '.haml'
var req = this

if(first_read){
Expand All @@ -171,10 +166,10 @@ var request_extensions = {
req.build_document(scope)
})
} else {
var partial = scope.body.match(/\=.?partial.?\(.?['|"](.*)['|"].?\)/)
var partial = scope.body.match(/\=.?partial.?\(.?['|"](.*)['|"].?\)$/)

if ( partial && partial[1] ){
var path = basepath + '_' + partial[1]
var path = basepath + '_' + partial[1] + '.haml'

posix.cat(path).addCallback(function(body){

Expand All @@ -186,13 +181,38 @@ var request_extensions = {
})

} else {
haml.render(scope, function(body){
req.send_data(scope)
})
req.layout_yield(scope)
}
}
}
},

layout_yield: function(scope){
var req = this

if(scope.layout){
var basepath = picard.env.root + picard.env.views + '/'
var filename = basepath + scope.layout + '.haml'

posix.cat(filename).addCallback(function(layout){
var yield = layout.match(/(\=.*yield)$/)
if( yield && yield[1] ){
scope.body = layout.replace(yield[1], scope.body)
haml.render(scope, function(body){
req.send_data(scope)
})
} else {
scope.body = layout
haml.render(scope, function(body){
req.send_data(scope)
})
}
})
} else {
haml.render(scope, function(body){
req.send_data(scope)
})
}
}

}

Expand Down
10 changes: 8 additions & 2 deletions sample_app/app.js
@@ -1,7 +1,13 @@
require('./config/env')

var layout = "application"

get('/layout', function(){
return { template: 'partial_test', layout: layout }
})

get('/partial', function(){
return { template: 'partial_test.haml' }
return { template: 'partial_test' }
})

get('/', function(){
Expand All @@ -14,7 +20,7 @@ get('/foo/:bar', function(params){

get('/haml', function(){
var scope = {
template: 'index.haml',
template: 'index',
print_date: function () {
return (new Date()).toDateString();
},
Expand Down
5 changes: 4 additions & 1 deletion sample_app/views/_snippet.haml
@@ -1 +1,4 @@
this is the partial content
%p This is "_snippet.haml" content

#picard_rules
=partial("sub_partial")
1 change: 1 addition & 0 deletions sample_app/views/_sub_partial.haml
@@ -0,0 +1 @@
%p This is "_sub_partial.haml" content
8 changes: 8 additions & 0 deletions sample_app/views/application.haml
@@ -0,0 +1,8 @@
!!! XML
!!! strict
%html{ xmlns: "http://www.w3.org/1999/xhtml" }
%head
%title
Picard v0.1 "Prime Directive"
%body
= yield
6 changes: 4 additions & 2 deletions sample_app/views/partial_test.haml
@@ -1,2 +1,4 @@
.this this is ham
#that.this = partial('snippet.haml')
%p This is "partial_test.haml" content

#this.that
= partial('snippet')

0 comments on commit 8267974

Please sign in to comment.