5

I'm working on a Rails 3.1 app using JavascriptMVC and ejs templates within the client to do some complicated features on my application. Unfortunately ejs syntax is very similar to erb syntax, to the point where I can't keep the code in the same file (although if someone knows of a good way to do this, I'd be ecstatic). Ultimately I want to be able to apply some rails code within the ejs template (say for I18n) but at this point I will just settle for getting this to work

Following the example from this question I have created a custom template handler that looks like this:

module CommonModel
    class Handler < ActionView::Template::Handler
        include ActionView::Template::Handlers::Compilable

        def compile(template)
            template.source.inspect
        end

    end
end

ActionView::Template.register_template_handler :ejs, CommonModel::Handler

Then I created a partial template that has my ejs code in it: _jmvc_templates.html.ejs

<script type="text/ejs" id="my_ejs_template">
    <div>Some ejs here</div>
</script>

Within my existing template, I attempt to include my partial:

<%= render 'path/to/my/ejs/templates/jmvc_templates' %>

At this point, the file is included and my handler is used, but everything is escaped, so my div in my template gets rendered to the page like this:

&lt;div%gt;

I'm sure I'm missing something obvious here, but I don't know what it could be... How can I get this template handler to just include my ejs based template without escaping all of the html in it?

Edit:

I've found that calling render with html_safe works:

<%= render('path/to/my/ejs/templates/jmvc_templates').html_safe %>

This seems like a kludge though - there has to be a way to get the erb renderer to treat text from my handler as html safe text.

Community
  • 1
  • 1
Tony Dorie
  • 114
  • 7
  • So something between your `def compile` and the `<%= render ... %>` is doing something to the string which sends it back into "not HTML safe" mode. – mu is too short Sep 30 '11 at 03:41
  • Makes sense, but what would that be? I'm using Rails 3.1 out of the box otherwise... – Tony Dorie Sep 30 '11 at 03:44
  • Maybee you should be using raw. Check [raw vs html_safe vs h][1] [1]: http://stackoverflow.com/questions/4251284/raw-vs-html-safe-vs-h-to-unescape-html – charlysisto Oct 12 '11 at 10:57

1 Answers1

0

Maybee you should be using raw. Check this

def compile(template)
  raw template.source.inspect
end
Community
  • 1
  • 1
charlysisto
  • 3,700
  • 17
  • 30
  • Yeah, you would think so - unfortunately, it doesn't work either. It is almost as if Rails is automatically escaping the output from my template handler, no matter what I do. I'm wondering if there isn't some sort of config that I need to locate at this point... – Tony Dorie Oct 13 '11 at 18:36