I am working on a style that displays code as well as output. Currently, it is structured in such a way that the code should be described only once and is displayed both in its source and in interpreted versions, for example:
<% code = <<PLACE_THE_EXAMPLE_CODE_BETWEEN_THESE_TWO_LINES_EXACTLY_AS_YOU_WANT_IT_TO_APPEAR <div> #{ image_tag 'image.png' } </div> PLACE_THE_EXAMPLE_CODE_BETWEEN_THESE_TWO_LINES_EXACTLY_AS_YOU_WANT_IT_TO_APPEAR %> <%= raw code %> <%= content_tag :pre, code, class: "prettyprint linenums" %>
This is great and fairly easy to maintain. The problem occurs with rail assistants, for example, image_tag in the example above. The example view correctly displays the image in a div, and the corresponding HTML displays the image in the sample code. In this case, the corresponding HTML code contains an anchor tag - the results of the image_tag method, and not the call itself.
I would prefer code examples to display helper methods rather than their results. I can do this work by specifying sample code in a file, and either rendering or reading the file. I would prefer to do this work by specifying the code in a variable as above, but I cannot get the ERB delimiter to work inside a string inside an erb block. Even the simplest case <% foo = '<%= bar %>' %> does not work at all. I tried playing with syntax (e.g. <%% %%> and <%% %%> ) using data from official documentation without much success.
The only information I could find on this was here using <%= "<" + "%=" %> link_to <%= image.css_tag.humanize %> <%= "%" + ">" %> %> , which does not work in this use case (if at all).
So, is there a way to specify a string containing the trailing ERB delimiter ( %> ) in the ERB string, or am I stuck using a slightly clunkier file reading method? Thanks!
Edit:
What I would like to get is a working version of this:
<%# Idealized code - does not work %> <% code = <<PLACE_THE_EXAMPLE_CODE_BETWEEN_THESE_TWO_LINES_EXACTLY_AS_YOU_WANT_IT_TO_APPEAR <div> <% image_tag 'image.png' %> </div> PLACE_THE_EXAMPLE_CODE_BETWEEN_THESE_TWO_LINES_EXACTLY_AS_YOU_WANT_IT_TO_APPEAR %>
So, <%= raw code %> will (continue) output:
<div> <img src="/images/image.png" alt="Image" /> </div>
And <%= content_tag :pre, code, class: "prettyprint linenums" %> outputs:
<pre class="prettyprint linenums"> <div> <% image_tag 'image.png' %> </div> </pre>
Instead of what it is currently doing when using a variable that:
<pre class="prettyprint linenums"> <div> <img src="/images/image.png" alt="Image" /> </div> </pre>
I want users to be able to copy the sample code and paste it into a new view, without having to translate the HTML back into the helpers that produce them. I think I basically need an alternative ERB delimiter, just as ' and " (or even %q{} ) are different for strings. It seems that although the final ERB delimiter occurs inside a string, it is actually treated as the end of the block. The simplest the case <% foo = '<%= bar %>' %> somewhat demonstrates what I want to do. In the generator you can use <% foo = '<%%= bar %>' %> (or something like that ), to say that it is not processed like ERB right there and there. All this works fine when reading from a file or even in a pure rb file (for example, an assistant), but It makes sense to insert him into submission, as it should be easily manipulated by our designers.