Skip to content

Commit 1a5028f

Browse files
committed
Merge pull request #7 from MountainRoseHerbs/feature/render-html-safe
Render liquid templates as html safe
2 parents ed37dc0 + 26e790c commit 1a5028f

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

lib/liquid-rails/template_handler.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def render(template, local_assigns={})
2525

2626
liquid = Liquid::Template.parse(template)
2727
render_method = (::Rails.env.development? || ::Rails.env.test?) ? :render! : :render
28-
liquid.send(render_method, assigns, filters: filters, registers: { view: @view, controller: @controller, helper: @helper })
28+
liquid.send(render_method, assigns, filters: filters, registers: { view: @view, controller: @controller, helper: @helper }).html_safe
2929
end
3030

3131
def filters
@@ -41,4 +41,4 @@ def compilable?
4141
end
4242
end
4343
end
44-
end
44+
end

spec/dummy/app/controllers/home_controller.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ def index_partial_with_full_path
1717
def index_with_filter
1818
end
1919

20+
def erb_with_html_liquid_partial
21+
end
22+
2023
private
2124

2225
def set_book
2326
@book = { 'name' => 'Liquid on Rails' }
2427
end
25-
end
28+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>Partial Content</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= render 'html_liquid_partial' %>

spec/dummy/config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
get '/index_partial', to: 'home#index_partial'
99
get '/index_partial_with_full_path', to: 'home#index_partial_with_full_path'
1010

11+
get '/erb_with_html_liquid_partial', to: 'home#erb_with_html_liquid_partial'
12+
1113
get '/foospace/bar/index_partial', to: 'foospace/bar#index_partial'
1214
end

spec/lib/liquid-rails/template_handler_spec.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
it 'renders with liquid template' do
66
visit '/'
77

8-
expect(page.body).to eq 'Liquid on Rails'
8+
expect(page.body).to eq('Liquid on Rails')
99
end
1010

1111
it 'sets content_type as html by default' do
@@ -19,28 +19,28 @@
1919
it 'renders with layout' do
2020
visit '/index_with_layout'
2121

22-
expect(page.body).to eq "Application Layout\nLiquid on Rails"
22+
expect(page.body).to eq("Application Layout\nLiquid on Rails")
2323
end
2424
end
2525

2626
context 'render with partial' do
2727
it 'no full path for the current controller' do
2828
visit '/index_partial'
2929

30-
expect(page.body).to eq "Application Layout\nLiquid on Rails\n\nHome Partial\nShared Partial"
30+
expect(page.body).to eq("Application Layout\nLiquid on Rails\n\nHome Partial\nShared Partial")
3131
end
3232

3333
it 'full path' do
3434
visit '/index_partial_with_full_path'
3535

36-
expect(page.body).to eq "Application Layout\nLiquid on Rails\n\nHome Partial\nShared Partial"
36+
expect(page.body).to eq("Application Layout\nLiquid on Rails\n\nHome Partial\nShared Partial")
3737
end
3838

3939
it 'respects namespace of original template for partials path' do
4040
visit '/foospace/bar/index_partial'
41+
4142
expect(page.body.strip).to eq("Foospace::BarController\n\nBar Partial")
4243
end
43-
4444
end
4545

4646
context 'render with filter' do
@@ -56,4 +56,12 @@
5656
expect(page.body).to eq("Application Layout\nLiquid on Rails\nThis is a long section of text")
5757
end
5858
end
59+
60+
context 'render html within an erb template' do
61+
it 'does not escape the html' do
62+
visit '/erb_with_html_liquid_partial'
63+
64+
expect(page.body.strip).to eq("Application Layout\n<p>Partial Content</p>")
65+
end
66+
end
5967
end

0 commit comments

Comments
 (0)