HAML -> Backbone Template, Unescaping HTML Parameters - ruby

I'm using HAML to generate templates for a Backbone.js app. I need to be able to insert <%= blah %> as an html attribute a la:
%a{:href => "myresources/<% id %>"} My Resource
and have it output
<a href='myresources/<%= id %>' >My Resource</a>
in the html template. Unfortunately, HAML escapes the html parameters leaving me with
<a href='#myresources/<%= id %>'>My Resource</a>
According to the HAML Reference the '!' operator can be used for unescaping strings, but not within the HTML attributes.
Also, I'd use plaintext to render the anchor tag, but since the anchor tag is the root for this particular view, I lose all of the benefits of using HAML.
Any help?
Update
I didn't mention, but I'm using LiveReload to actually watch my file system and run the haml compiler, and there was a setting in LiveReload to disable HTML escapes in tag attributes. < head slap > If anyone else runs into this issue outside of LiveReload, you can also set the :escape_attrs option to false when configuring your HAML setup.

You can configure HAML to not escape tag attributes using the escape_attrs option in your HAML configuration. See HAML Options.

You can try using html_safe which is a method on String objects. This will escape the html characters in the variable statement (< for example) and will leave the intact for underscore to evaluate at runtime:
%a{:href => "myresources/<% id %>".html_safe} My Resource
Found on answer to Interpolate inside html attributes with Underscore.js

Related

Slash at the end of the line in haml - should I clean it?

I'm wondering why in the source code of my project I sometimes see a / at the end of the line (Haml).
%meta{:content => "text/html; charset=utf-8", "http-equiv" => "Content-Type"}/
Maybe it's due to some HTML to Haml conversion, but why?
I cannot find any doc about that. Just wondering if I should clean this.
It creates a void self-closing tag.
The forward slash character, when placed at the end of a tag
definition, causes Haml to treat it as being an empty (or void)
element. Depending on the format, the tag will be rendered either
without a closing tag (:html4 or :html5), or as a self-closing tag
(:xhtml).
See the HAML documentation: http://haml.info/docs/yardoc/file.REFERENCE.html#empty-void-tags-

Put HTML in javascript using Ruby

Note: This is a very strange and unique use case so I apologise in advance if it seems a bit ass-backwards.
I have a haml file content.haml and a coffeescript file main.coffee.
I wish to somehow get the html resulting from rendering content.haml into a variable in the coffeescript/resulting javascript.
The end result should be a javascript file rendered to the browser.
let's say they look like this:
# content.haml
.container
.some_content
blah blah blah
-
# main.coffee
html_content = ???
do_something_with_html_content(html_content)
I know, this sounds ridiculous, 'use templates', 'fetch the HTML via ajax' etc. In this instance however, it's not possible, everything needs to be served via one JS file and I cannot fetch other resources from the server. Weird, I know.
Short of manually reconstructing the haml in the coffeescript file by joining an array of strings like this:
html_content = [
'<div class"container">',
'<div class"some_content">',
'blah blah blah',
'</div>',
'</div>',
]
I'm not sure the best way of doing this.
Another way I though of was to put something like this in the coffee file:
html_content = '###CONTENT###'
Then render the haml to html in ruby, render the coffeescript to js and then replace ###CONTENT### with the rendered html before serving to the client. However the html is a multi-line string so it completely destroys the javascript.
I'm convinced there must be some other nice way of rendering the haml into html in a variable such that it forms valid javascript, but my brain has gone blank.
Perhaps you can try something like this in one of your views:
:javascript
html_content = <%= escape_javascript(render partial: "content")%>
## your own logic follows here....
Wouldn't it be better to use a custom html data attribute and then fetch the content of it in js?
<div data-mycontent="YOUR CONTENT GOES HERE"></div>
And then in coffee, use the dataset attribute / data via jquery, if it is available.
If you set a var via writing the file directly it will render your js file uncacheable, among other drawbacks.
You can do that by using the sprockets gem, like Rails does. You just need to rename your CoffeeScript file to main.coffee.erb and use it as you would e.g. a haml template. Pass in your rendered html with an instance variable:
html_content = '<%= #html_content %>'
Edit: Added missing quotes.

How to use content_for & yield_content in middleman using slim

I've tried prepending content_for with =, == or - without luck :)
index.slim
- content_for(:senarios) do
h1 Some content
layout.slim
== yield_content(:senarios)
Hope somebody got a solution.
The example should work fine.
You capture content with content_for and insert it in the layout file with yield_content. You can omit the parentheses if you want.
If you use standard Slim settings you need two equal signs. Otherwise the output will be escaped and you will see <h1>Some content</h1> instead of Some content in the rendered output.
I also use this with Middleman and Slim. No issues. Can you please provide more code, errors, etc.?
The only thing which is looking suspicious is the filename index.slim. It should be index.html.slim.
I met same problem, and solved it.
You should change
- content_for(:eyecatch) do
to
= content_for(:eyecatch) do
nested html attributes should no longer be duplicated.

Rails 3 unwanted html escaping

I am converting my fat Rails2 application to run on Rails3. After a long intense fight with an army of bugs and my bosses yells, the page is all rendered as an escaped html string. So all the divs, images etc. are written literally for the user.
For some reason this call of a partial renders an escaped string
<%= render :partial => 'something_really_interesting' %>
As all Ruby on Rails application this instruction is not called very much! So how would I handle all these calls to not render normally not as an escaped string?
Use <%= raw bla %> in inside the partial file.
http://github.com/rails/rails/blob/3270c58ebb3143b3ab3b349fe339cdd4587468ee/actionpack/lib/action_view/helpers/raw_output_helper.rb#L13
Rails 3 automatically makes everything safe. You need to put raw to escape the behavior. That also means you don't have to use h() method to make your string safe any more.

inserting blocks of text in haml

In my Jekyll blog I use the include tag to put the contents of a file into the document. However if I attempt to do this with a HAML based document the indentation of the included text is wrong. :preserve does not work because it requires indentation. Is there a way to specify a block of text without depending on indentation?
%html
%body
- preserve do
<strong>included text from file</strong>
- end
It seems that in current version of Haml :text filter was removed. Now you can use :plain filter. No processing is performed for the text inside this block. You can paste multiline blocks of text or HTML code there. HTML will appear on the page unescaped.
:plain
Some text <b>or HTML</b>.
For text, use filters:
:text
some text
:erb
<%= render :partial ... %>
http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#filters

Resources