views:

40

answers:

2

I am working on a simple Rails/jQuery HTML templater app which stores a series of pre-designed templates in a database (at the moment I've just saved these as partials to get the basic concept working) and on clicking 'Show code' alongside any one of these template records, a js.erb script should place the corresponding partial within 'pre' tags dynamically via JS on that page so the user can see the raw html code.

At the moment it's working but I get the rendered html coming back and not the raw HTML that I'm looking for. Here's the js:

$("div#template-view").html("<pre><code><%= escape_javascript( render :partial => "core_template") %></code></pre>");

So pray tell, what obvious thing am I missing!? :-)

Thanks

Allan

+3  A: 

Use

$("div#template-view").text("...")

instead. This will not parse the code

JochenJung
well that was spectacularly easy :-) Thanks!!
oh yeah, that's much better than my solution :)
Max Williams
A: 

The pre tag will show source code (or any text) in a reasonable approximation to it's original state, but it won't escape html for you. Unescaped html will always be rendered as html regardless of what tag it happens to be in. By escaped i mean that all the special characters are converted to their escaped versions. The rails method h will do this for you, so if you call h with the results of calling escape_javascript then it should work fine.

$("div#template-view").html("<pre><code><%= h(escape_javascript(render :partial => "core_template")) %></code></pre>");
Max Williams