Skip to content

Commit

Permalink
fixed haml-js's if and foreach plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Kellen Presley committed Dec 8, 2009
1 parent 1e450f7 commit f371737
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
25 changes: 11 additions & 14 deletions picard/lib/picard/haml.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ Haml.parse = function (text) {

function process_plugins() {
var contents, i;
switch (element[0]) {
case ':if':
var condition = element[1].condition;
contents = element[2];
switch (element[0].plugin) {
case 'if':
var condition = element[0].condition
contents = element[1]
for (i in element) {
if (element.hasOwnProperty(i)) {
delete element[i];
Expand All @@ -291,17 +291,14 @@ Haml.parse = function (text) {
element.length = new_element.length;
}
break;
case ':foreach':
case 'foreach':
var array, key, value, key_name, value_name;
array = element[1].array;
key_name = element[1].key;
value_name = element[1].value;
contents = element[2];
for (i in element) {
if (element.hasOwnProperty(i)) {
delete element[i];
}
}

array = element[0].array
for (var i in array[0]) { key_name = i }
value_name = element[0].value
contents = element[1]

element.length = 0;
for (key in array) {
if (array.hasOwnProperty(key)) {
Expand Down
29 changes: 23 additions & 6 deletions sample_app/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
require('./config/env')

get('/power_haml', function(p){

return {
template: 'kellen.haml',
todolist: [ { description: 'foo' }, { description: 'bar' } ]
}
})

get('/', function(){
return { text: 'Hello Universe' }
})

get('/haml', function(){
var scope = {
return {
template: 'index.haml',
print_date: function () {
return (new Date()).toDateString();
Expand All @@ -14,16 +22,25 @@ get('/haml', function(){
name: "Jean-Luc Picard",
bio: "Captain of the USS Enterprise"
}
};
return scope
}
})

var commands = [
{ command: 'Make it so' },
{ command: 'You have the bridge, Number One' }
]

get('/json', function(){
return {
type: 'application/json',
body: JSON.stringify(
[ { command_1: 'Make it so' },
{ command_2: 'You have the bridge, Number One' } ] )
body: JSON.stringify(commands)
}
})

get('/advanced_haml', function(){
return {
template: 'advanced.haml',
commands: commands // defined above
}
})

Expand Down
2 changes: 1 addition & 1 deletion sample_app/spec/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

it 'should render json' do
res = Curl::Easy.perform(base_url + '/json')
res.body_str.should include("[{\"command_1\":\"Make it so\"},{\"command_2\":\"You have the bridge, Number One\"}]")
res.body_str.should include("[{\"command\":\"Make it so\"},{\"command\":\"You have the bridge, Number One\"}]")
res.header_str.should include('application/json')
res.header_str.should include('200')
end
Expand Down
16 changes: 16 additions & 0 deletions sample_app/spec/picard_specific_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,20 @@
end
end

describe 'advanced haml' do
before do
@res = Curl::Easy.perform(base_url + '/advanced_haml')
end

it 'should allow for the "if" plugin' do
@res.body_str.should include("This will show up!")
@res.body_str.should_not include("This will not show up!")
end

it 'should allow for the "foreach" plugin' do
@res.body_str.should include("<li>Make it so</li>")
@res.body_str.should include("<li>You have the bridge, Number One</li>")
end
end

end
11 changes: 11 additions & 0 deletions sample_app/views/advanced.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#page Advanced Haml Usage

:if{ condition: true }
%p This will show up!

:if{ condition: 0 > 1 }
%p This will not show up!

%ul.todolist
:foreach{ array: commands, value: "item" }
%li= item.command

0 comments on commit f371737

Please sign in to comment.