Skip to content

Commit 26e790c

Browse files
committed
Fix liquid template to be html safe when used as a partial
1 parent d2dbce3 commit 26e790c

File tree

6 files changed

+17
-2
lines changed

6 files changed

+17
-2
lines changed

lib/liquid-rails/template_handler.rb

Lines changed: 1 addition & 1 deletion
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

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)