lego-haml 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = lego-haml
2
2
 
3
- A lego plugin for rendering haml
3
+ A lego[http://github.com/stjernstrom/lego-core] plugin for rendering haml
4
4
 
5
5
  == Usage
6
6
  From within an action use the haml method
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/example/config.ru ADDED
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'lego-core'
3
+ require File.join(Dir.pwd, '..', 'lib', 'lego', 'haml')
4
+
5
+ module ControllerPlugin
6
+
7
+ def self.register(lego)
8
+ lego.add_plugin :router, Matcher
9
+ lego.add_plugin :controller, Routes
10
+ end
11
+
12
+ module Routes
13
+ def get(path, &block)
14
+ add_route(:get, {:path => path, :action_block => block})
15
+ end
16
+ end
17
+
18
+ module Matcher
19
+ def self.match_route(route, env)
20
+ (route[:path] == env['PATH_INFO']) ? true : false
21
+ end
22
+ end
23
+ end
24
+
25
+ Lego.config do
26
+ set :views => File.join(Dir.pwd, 'views')
27
+ plugin ControllerPlugin
28
+ plugin Lego::Haml
29
+ end
30
+
31
+ class MyApp < Lego::Controller
32
+
33
+ get "/" do
34
+ haml :index
35
+ end
36
+ end
37
+
38
+ run MyApp
@@ -0,0 +1 @@
1
+ %p Partial loaded
@@ -0,0 +1,2 @@
1
+ %h1 Welcome to root
2
+ = haml :_partial
data/lego-haml.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{lego-haml}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Patrik Hedman"]
@@ -23,12 +23,19 @@ Gem::Specification.new do |s|
23
23
  "README.rdoc",
24
24
  "Rakefile",
25
25
  "VERSION",
26
+ "example/config.ru",
27
+ "example/views/_partial.haml",
28
+ "example/views/index.haml",
26
29
  "lego-haml.gemspec",
27
30
  "lib/lego/haml.rb",
28
31
  "spec/lego-haml_spec.rb",
29
32
  "spec/spec.opts",
30
33
  "spec/spec_helper.rb",
34
+ "spec/views/_foo.haml",
35
+ "spec/views/explicit_partial.haml",
31
36
  "spec/views/file_with_locals.haml",
37
+ "spec/views/instance_vars.haml",
38
+ "spec/views/partial.haml",
32
39
  "spec/views/template.haml"
33
40
  ]
34
41
  s.homepage = %q{http://github.com/polly/lego-haml}
data/lib/lego/haml.rb CHANGED
@@ -13,20 +13,19 @@ module Lego
13
13
  #
14
14
  # Method for rendering haml input as html.
15
15
  # <template> can be either a string or a symbol
16
- # - String: Renders the string and returns html
16
+ # - String: Path to the view file
17
17
  # - Symbol: Looks for a <template>.haml file in
18
18
  # the path specified by options(:views)
19
19
  # and renders the contents of that file
20
20
  #
21
21
  # Example:
22
- # haml "%h1 My Header" #=> "<h1>My Header</h1>\n"
23
- # haml :template #=> Renders <view_path>/template.haml
22
+ # haml "views/template.haml" #=> Renders views/template.haml
23
+ # haml :template #=> Renders <view_path>/template.haml
24
24
  #
25
- # <locals> is a hash of local variables to pass to
26
- # the template
25
+ # <locals> is a hash of local variables to pass to the template
27
26
  #
28
27
  # Example:
29
- # haml "%h1 foo", :foo => "My Header" #=> "<h1>My Header</h1>\n"
28
+ # haml :template, :foo => "My Header" #=> Passes the local variable foo to the view
30
29
  #
31
30
  # Instance variables are automatically made available to the view
32
31
  #
@@ -34,19 +33,32 @@ module Lego
34
33
  # @foo = "My Template"
35
34
  # haml "%h1 @foo" #=> "<h1>My Header</h1>\n"
36
35
  #
36
+ #
37
+ # The haml method can be used inside views to render partials
38
+ #
39
+ # Example:
40
+ # %h1 My Template
41
+ # = haml :_foo #=> Renders <view_path>/_foo.haml
42
+ # = haml 'views/_foo.haml' #=> Renders views/_foo.haml
43
+ # = haml :foo, :foo => "foo" #=> Passes foo to the partial
44
+ #
45
+ #
37
46
  def haml(template, locals={})
38
47
  ::Haml::Engine.new(extract(template)).render(self, locals)
39
48
  end
40
49
 
41
-
42
50
  private
43
51
 
44
52
  def extract(template)
45
- template.is_a?(Symbol) ? read(template) : template
53
+ template.is_a?(Symbol) ? read(template) : read_explicit(template)
46
54
  end
47
55
 
48
56
  def read(template)
49
- File.read("#{options(:views)}/#{template}.haml")
57
+ read_explicit "#{options(:views)}/#{template}.haml"
58
+ end
59
+
60
+ def read_explicit(file_path)
61
+ File.read(file_path)
50
62
  end
51
63
  end
52
64
  end
@@ -5,17 +5,13 @@ def app
5
5
  set :views => File.join(File.dirname(__FILE__), 'views')
6
6
 
7
7
  get "/string" do
8
- haml "%h1 My Header"
8
+ haml "spec/views/template.haml"
9
9
  end
10
10
 
11
11
  get "/instance_variables" do
12
12
  @foo = "foo"
13
13
 
14
- haml "%h1= @foo"
15
- end
16
-
17
- get "/string/with/locals" do
18
- haml "%h1= foo", :foo => "foo"
14
+ haml :instance_vars
19
15
  end
20
16
 
21
17
  get "/file" do
@@ -25,11 +21,19 @@ def app
25
21
  get "/file/with/locals" do
26
22
  haml :file_with_locals, :foo => "foo"
27
23
  end
24
+
25
+ get '/partial' do
26
+ haml :partial
27
+ end
28
+
29
+ get '/partial/with/path' do
30
+ haml :explicit_partial
31
+ end
28
32
  end
29
33
  end
30
34
 
31
35
  describe "Lego Haml" do
32
- it "should render a string of haml as html" do
36
+ it "should render the specified haml file when passed a string" do
33
37
  get '/string'
34
38
  last_response.body.should eql("<h1>My Header</h1>\n")
35
39
  end
@@ -46,15 +50,21 @@ describe "Lego Haml" do
46
50
  last_response.body.should eql("<h1>foo</h1>\n")
47
51
  end
48
52
 
49
- it "should render a string template with locals passed in" do
50
- get '/string/with/locals'
53
+ it "should render a file template with locals passed in" do
54
+ get '/file/with/locals'
51
55
 
52
56
  last_response.body.should eql("<h1>foo</h1>\n")
53
57
  end
54
58
 
55
- it "should render a file template with locals passed in" do
56
- get '/file/with/locals'
59
+ it "should render partials" do
60
+ get '/partial'
57
61
 
58
- last_response.body.should eql("<h1>foo</h1>\n")
62
+ last_response.body.should eql("<h1>Partial</h1>\n<p>content loaded from partial</p>\n")
63
+ end
64
+
65
+ it "should render partials with explicit path" do
66
+ get '/partial/with/path'
67
+
68
+ last_response.body.should eql("<h1>Partial</h1>\n<p>content loaded from partial</p>\n")
59
69
  end
60
70
  end
@@ -0,0 +1 @@
1
+ %p= foo
@@ -0,0 +1,2 @@
1
+ %h1 Partial
2
+ = haml 'spec/views/_foo.haml', :foo => "content loaded from partial"
@@ -0,0 +1 @@
1
+ %h1= @foo
@@ -0,0 +1,2 @@
1
+ %h1 Partial
2
+ = haml :_foo, :foo => "content loaded from partial"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lego-haml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrik Hedman
@@ -38,12 +38,19 @@ files:
38
38
  - README.rdoc
39
39
  - Rakefile
40
40
  - VERSION
41
+ - example/config.ru
42
+ - example/views/_partial.haml
43
+ - example/views/index.haml
41
44
  - lego-haml.gemspec
42
45
  - lib/lego/haml.rb
43
46
  - spec/lego-haml_spec.rb
44
47
  - spec/spec.opts
45
48
  - spec/spec_helper.rb
49
+ - spec/views/_foo.haml
50
+ - spec/views/explicit_partial.haml
46
51
  - spec/views/file_with_locals.haml
52
+ - spec/views/instance_vars.haml
53
+ - spec/views/partial.haml
47
54
  - spec/views/template.haml
48
55
  has_rdoc: true
49
56
  homepage: http://github.com/polly/lego-haml