k0kubun / hamlit

High Performance Haml Implementation
https://rubygems.org/gems/hamlit
Other
981 stars 59 forks source link

How to use hamlit with views of sinatra? #109

Closed znz closed 7 years ago

znz commented 7 years ago

I try to use hamlit with sinatra, but = yield in layout escapes HTML.

How to use hamlit with views of sinatra? When I use inline templates, I get expected results.

Example app: https://github.com/znz/sinatra-hamlit-example

How to reproduce:

  1. run rackup
  2. open http://localhost:9292/ and see <p>Hello</p>

Another result:

  1. run ruby app.rb
  2. open http://localhost:4567/ and see Hello without tags

With haml:

  1. replace hamlit with haml in Gemfile
  2. run bundle update
  3. run rackup
  4. open http://localhost:9292/ and see Hello without tags
k0kubun commented 7 years ago

Thank you for very helpful issue reporting.

Why this issue happens

In Sinatra environment, there's very big difference in default config between Hamlit and Haml. https://github.com/k0kubun/hamlit#sinatra

While Haml disables escape_html option by default, Hamlit enables it for security. If you want to disable it, please write: set :haml, { escape_html: false }

If you bundle haml gem, the option is set { escape_html: false } by default. If you call set :haml, { escape_html: true }, this issue will happen in haml gem too.

So, the problem here is that you are writing layout that escapes partial view with { escape_html: true }.

How to solve this issue

Disable HTML escape

set :haml, { escape_html: false }

Don't escape partial view in layout

Of course, this is recommended.

 @@ layout
 !!! 5
 %html
   %head
     %title= 'App'
   %body
-    = yield
+    != yield

 @@ debug
 %p= "Hello"
znz commented 7 years ago

Thank you for your explanation! I solved with != yield.

Slim has == yield (same feature of haml's != yield) on top of site, but haml does not in top or tutorial. And it is written around the end of reference. So I feel hard to find it.