Permalink
Browse files

support for regex in if and unless layout

  • Loading branch information...
1 parent e583095 commit fceefef1e7a4d3349c32549174b7d9fb4d71c7f6 Dirk Kelly committed Dec 13, 2010
Showing with 69 additions and 28 deletions.
  1. +32 −23 lib/nested_layouts/tags/core.rb
  2. +37 −5 spec/lib/nested_layouts/tags/core_spec.rb
@@ -90,32 +90,30 @@ class TagError < StandardError; end
current_layout_name(tag)
end
- desc %{ output the contents of tag if layout equals name }
- tag 'if_layout' do |tag|
- if name = tag.attr['name']
- if layout = tag.globals.page_original_layout
- tag.expand if layout.name == name
- elsif layout = tag.globals.page.layout
- tag.expand if layout.name == name
- end
- else
- raise TagError.new(%{Error (nested_layouts): "if_layout" tag must contain a "name" attribute})
- end
+ desc %{
+ output the contents of tag if layout equals name (support regex)
+
+ *Usage:*
+
+ <pre><code><r:if_layout name="(parent|parent_of_child)">
+ one of those layouts
+ </r:if_layout></code></pre>
+ }
+ tag 'if_layout' do |tag|
+ tag.expand if is_current_layout(tag)
end
- desc %{ output the contents of tag unless layout equals name }
+ desc %{
+ Output the contents of tag unless layout equals name (support regex)
+
+ *Usage:*
+
+ <pre><code><r:unless_layout name="parent">
+ not the parent layotu
+ </r:unless_layout></code></pre>
+ }
tag 'unless_layout' do |tag|
- if name = tag.attr['name']
- if layout = tag.globals.page_original_layout
- tag.expand if layout.name != name
- elsif layout = tag.globals.page.layout
- tag.expand if layout.name != name
- else
- tag.expand
- end
- else
- raise TagError.new(%{Error (nested_layouts): "if_layout" tag must contain a "name" attribute})
- end
+ tag.expand unless is_current_layout(tag)
end
tag 'body' do |tag|
@@ -136,6 +134,17 @@ def current_layout_name(tag)
result
end
+ def is_current_layout(tag)
+ if tag.attr['name'].nil?
+ raise TagError.new(%{Error (nested_layouts): "if_layout" tag must contain a "name" attribute})
+ end
+
+ layout = tag.globals.page_original_layout || tag.globals.page.layout
+ search = %r{#{tag.attr['name']}}
+
+ (layout.name =~ search).present?
+ end
+
end
end
end
@@ -58,6 +58,22 @@
pages(:parent).should render(tag).as(expected)
end
+ it 'it should support regex' do
+ tag = %{<r:inside_layout name='parent'><r:if_layout name='(parent|child)'><h1>Hi</h1></r:if_layout></r:inside_layout>}
+ expected = <<-CONTENT
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Title</title>
+ </head>
+ <body id="" class="parent site">
+ <h1>Hi</h1>
+ </body>
+</html>
+CONTENT
+ pages(:parent).should render(tag).as(expected)
+ end
+
it 'it should not render the contents if false' do
tag = %{<r:inside_layout name='parent'><r:if_layout name='not parent'><h1>Hi</h1></r:if_layout></r:inside_layout>}
expected = <<-CONTENT
@@ -78,32 +94,48 @@
describe '<r:unless_layout>' do
- it 'it should not render the contents if true' do
- tag = %{<r:inside_layout name='parent'><r:unless_layout name='parent'><h1>Hi</h1></r:unless_layout></r:inside_layout>}
+ it 'it should not render the contents if false' do
+ tag = %{<r:inside_layout name='parent'><r:unless_layout name='not parent'><h1>Hi</h1></r:unless_layout></r:inside_layout>}
expected = <<-CONTENT
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body id="" class="parent site">
+ <h1>Hi</h1>
+ </body>
+</html>
+CONTENT
+ pages(:parent).should render(tag).as(expected)
+ end
+ it 'it should support regex' do
+ tag = %{<r:inside_layout name='parent'><r:unless_layout name='(not_parent|not_child)'><h1>Hi</h1></r:unless_layout></r:inside_layout>}
+ expected = <<-CONTENT
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Title</title>
+ </head>
+ <body id="" class="parent site">
+ <h1>Hi</h1>
</body>
</html>
CONTENT
pages(:parent).should render(tag).as(expected)
end
- it 'it should not render the contents if false' do
- tag = %{<r:inside_layout name='parent'><r:unless_layout name='not parent'><h1>Hi</h1></r:unless_layout></r:inside_layout>}
+ it 'it should not render the contents if true' do
+ tag = %{<r:inside_layout name='parent'><r:unless_layout name='parent'><h1>Hi</h1></r:unless_layout></r:inside_layout>}
expected = <<-CONTENT
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body id="" class="parent site">
- <h1>Hi</h1>
+
</body>
</html>
CONTENT

0 comments on commit fceefef

Please sign in to comment.