Note that there are some explanatory texts on larger screens.

plurals
  1. POPut HTML in javascript using Ruby
    primarykey
    data
    text
    <p><strong>Note:</strong> This is a very strange and unique use case so I apologise in advance if it seems a bit ass-backwards.</p> <p>I have a haml file <code>content.haml</code> and a coffeescript file <code>main.coffee</code>. I wish to somehow get the html resulting from rendering content.haml into a variable in the coffeescript/resulting javascript.</p> <p>The end result should be a javascript file rendered to the browser.</p> <p>let's say they look like this:</p> <pre><code># content.haml .container .some_content blah blah blah </code></pre> <p>-</p> <pre><code># main.coffee html_content = ??? do_something_with_html_content(html_content) </code></pre> <p>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.</p> <p>Short of manually reconstructing the haml in the coffeescript file by joining an array of strings like this:</p> <pre><code>html_content = [ '&lt;div class"container"&gt;', '&lt;div class"some_content"&gt;', 'blah blah blah', '&lt;/div&gt;', '&lt;/div&gt;', ] </code></pre> <p>I'm not sure the best way of doing this.</p> <p>Another way I though of was to put something like this in the coffee file:</p> <pre><code>html_content = '###CONTENT###' </code></pre> <p>Then render the haml to html in ruby, render the coffeescript to js and then replace <code>###CONTENT###</code> with the rendered html before serving to the client. However the html is a multi-line string so it completely destroys the javascript.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload