Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4636

How to make ERB or ERUBI escape HTML in the template without escaping the entire template - NOT RAILS

$
0
0

I'm using ERB or ERUB or Tilt here outside of Rails and I want to make my HTML templates work like rails where stuff is HTML escaped by default.

e.g.

<!-- file.html.erb --><h1>Hello <%= name %></h1>

When I render this, if name is <b>Pat</b>, then the HTML sent to the browser is:

<h1>Hello <b>Pat</b></h1>

I'd like it to render this:

<h1>Hello &lt;b&gt;Pat&lt;/b&gt;</h1>

And I'd like that behavior to be the default anytime <%= is used.

I'm trying Tilt, but when I do Tilt.new("file.html.erb",escape_html: true) it escapes the entire file, rendering &lt;h1&gt;Hello &lt;b&gt;Pat&lt;/b&gt;&lt;/h1&gt;

I tried reading Rails' source to figure this out, but it's highly abstracted and wondering if anyone reading this happens to know how to achieve this.


Viewing all articles
Browse latest Browse all 4636

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>