Skip to content

Commit 4c3ed73

Browse files
committed
add content_for tag
1 parent c43d59e commit 4c3ed73

File tree

6 files changed

+105
-7
lines changed

6 files changed

+105
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ Currently, **Liquid-Rails** adds only the followings:
7676
1. [csrf_meta_tags](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/csrf_meta_tags.rb)
7777
2. [google_analytics_tag](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/google_analytics_tag.rb)
7878
3. [javascript_tag](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/javascript_tag.rb)
79-
4. [paginate_tag](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/paginate_tag.rb)
79+
4. [paginate](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/paginate_tag.rb)
80+
4. [content_for](https://github.com/yoolk/liquid-rails/blob/master/lib/liquid-rails/tags/content_for.rb)
8081

8182
### Drop Class
8283

lib/liquid-rails/rspec/drop_example_group.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ module DropExampleGroup
1717
end
1818
}
1919

20-
before {
21-
setup_view_and_controller
22-
subject.context = context
23-
}
20+
before(:all) { setup_view_and_controller }
21+
before(:each) { subject.context = context }
2422
end
2523
end
2624
end

lib/liquid-rails/rspec/tag_example_group.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ module TagExampleGroup
1212
end
1313

1414
def expect_template_result(template, expected, assigns={})
15-
actual = Liquid::Template.parse(template).render!(assigns, { registers: { helper: @view, view: @view, controller: @controller } })
15+
# make assigns available inside context
16+
assigns.each do |key, value|
17+
context[key] = value
18+
end
19+
20+
actual = Liquid::Template.parse(template).render!(context)
1621
expect(actual).to eq(expected)
1722
end
1823
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# It works similar to Rails #content_for.
2+
# Calling #content_for stores a block of markup in an identifier for later use.
3+
# In order to access this stored content in other templates or the layout, you would pass the identifier as an argument to content_for.
4+
#
5+
# Usage:
6+
#
7+
# {% content_for :not_authorized %}
8+
# alert('You are not authorized to do that!');
9+
# {% endcontent_for %}
10+
#
11+
# You can then use content_for :not_authorized anywhere in your templates.
12+
# {% if current_user.nil? %}
13+
# {% yield :not_authorized %}
14+
# {% endif %}
15+
16+
module Liquid
17+
module Rails
18+
class ContentForTag < ::Liquid::Block
19+
Syntax = /(#{::Liquid::QuotedFragment})\s*(flush\s*(true|false))?/
20+
21+
def initialize(tag_name, markup, context)
22+
super
23+
24+
if markup =~ Syntax
25+
@identifier = $1.to_s
26+
@flush = $3
27+
else
28+
raise SyntaxError.new("Syntax Error - Valid syntax: {% content_for [name] %}")
29+
end
30+
end
31+
32+
def render(context)
33+
@context = context
34+
35+
options = @flush == 'true' ? { flush: true } : {}
36+
@context.registers[:view].content_for(@identifier, super.html_safe, options)
37+
''
38+
end
39+
end
40+
end
41+
end
42+
43+
module Liquid
44+
module Rails
45+
class YieldTag < ::Liquid::Tag
46+
Syntax = /(#{::Liquid::QuotedFragment})/
47+
48+
def initialize(tag_name, markup, context)
49+
super
50+
51+
if markup =~ Syntax
52+
@identifier = $1
53+
else
54+
raise SyntaxError.new("Syntax Error - Valid syntax: {% yield [name] %}")
55+
end
56+
end
57+
58+
def render(context)
59+
@context = context
60+
61+
@context.registers[:view].content_for(@identifier).try(:html_safe)
62+
end
63+
end
64+
end
65+
end
66+
67+
Liquid::Template.register_tag('content_for', Liquid::Rails::ContentForTag)
68+
Liquid::Template.register_tag('yield', Liquid::Rails::YieldTag)

lib/liquid-rails/tags/google_analytics_tag.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
module Liquid
88
module Rails
99
class GoogleAnalyticsTag < ::Liquid::Tag
10-
1110
Syntax = /(#{::Liquid::QuotedFragment}+)?/
1211

1312
def initialize(tag_name, markup, tokens)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'spec_helper'
2+
3+
module Liquid
4+
module Rails
5+
describe ContentForTag, type: :tag do
6+
it 'content_for and yield' do
7+
Liquid::Template.parse(%|{% content_for not_authorized1 %}alert('You are not authorized to do that!');{% endcontent_for %}|).render(context)
8+
9+
expect_template_result(%|{% yield not_authorized1 %}|, "alert('You are not authorized to do that!');")
10+
end
11+
12+
it 'invokes content_for with the same identifier multiple times' do
13+
Liquid::Template.parse(%|{% content_for not_authorized2 %}alert('You are not authorized to do that 1!');{% endcontent_for %}|).render(context)
14+
Liquid::Template.parse(%|{% content_for not_authorized2 %}alert('You are not authorized to do that 2!');{% endcontent_for %}|).render(context)
15+
16+
expect_template_result(%|{% yield not_authorized2 %}|, "alert('You are not authorized to do that 1!');alert('You are not authorized to do that 2!');")
17+
end
18+
19+
it 'invokes content_for with the same identifier multiple times and flush' do
20+
Liquid::Template.parse(%|{% content_for not_authorized3 %}alert('You are not authorized to do that 1!');{% endcontent_for %}|).render(context)
21+
Liquid::Template.parse(%|{% content_for not_authorized3 flush true %}alert('You are not authorized to do that 2!');{% endcontent_for %}|).render(context)
22+
23+
expect_template_result(%|{% yield not_authorized3 %}|, "alert('You are not authorized to do that 2!');")
24+
end
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)