Skip to content

Commit

Permalink
Add implicit this to locals variables of partial
Browse files Browse the repository at this point in the history
When using `Cuba::Mote` and doing `partial`, we've changed
it so that you always have to use the explicit context `this`:

`{{ this.partial("something") }}`

In addition, the partials automatically get `this` as
a local variable.

Helpers and all other stuff are accessed through this too.
  • Loading branch information
Cyril David committed Jun 29, 2012
1 parent 590c003 commit 2b221bc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/cuba/contrib/mote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.setup(app)
end

def partial(template, locals = {})
mote(mote_path(template), locals)
mote(mote_path(template), locals.merge(this: self))
end

def view(template, locals = {}, layout = settings[:mote][:layout])
Expand All @@ -31,7 +31,7 @@ def mote_path(template)
end

def mote_vars(content)
{ context: self, content: content }
{ content: content }
end

class NoLayout < StandardError
Expand Down
13 changes: 13 additions & 0 deletions test/mote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
render("home", title: "Hola")
end

on "partial-fu" do
res.write partial("partial1")
end

on "abs_path" do
res.write view("./test/views/custom/abs_path.mote", title: "Absolute")
end
Expand All @@ -46,6 +50,15 @@
assert_response body, ["<title>Hola</title>\n<h1>Home</h1>\n\n"]
end

test "partial-fu" do
# We verify that nesting partials and contexts are passed along properly.
_, _, body = Cuba.call({ "PATH_INFO" => "/partial-fu/A", "SCRIPT_NAME" => "" })
assert_response body, ["partial1\npartial2\n/partial-fu/A\n\n"]

# And that we're able to use req.path in the inner partials without an issue.
_, _, body = Cuba.call({ "PATH_INFO" => "/partial-fu/B", "SCRIPT_NAME" => "" })
assert_response body, ["partial1\npartial2\n/partial-fu/B\n\n"]
end

test "partial" do
_, _, body = Cuba.call({ "PATH_INFO" => "/frag", "SCRIPT_NAME" => "" })
Expand Down
2 changes: 2 additions & 0 deletions test/views/partial1.mote
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
partial1
{{ this.partial "partial2" }}
2 changes: 2 additions & 0 deletions test/views/partial2.mote
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
partial2
{{ this.req.path }}

0 comments on commit 2b221bc

Please sign in to comment.