1

In Rails, there exist such helper as "concat" to output variables inside <% %> block for erb remplates. Which helper can I use in Sinatra to perform the same action, without using <%= %> block ? I mean, something like

<%
#code
concat "This should be rendered in HTML, not in console"
#code
%>

EDIT The code in the view is something like this (yep, too much logic, but this is my first app in Ruby):

<% Dir.glob('uploaded/*').select do |entry| %>
    <div class="singleFileItem">
    <% if File.directory? entry %>
        <img src="images/folder.png">
    <% else 
       case entry.to_s.split(".")[1]
        when "doc","docx" %>
            <img class="pictogram" src='images/word.png'>
        <% when "xls","xlsx" %>
            <img class="pictogram" src='images/excel.png'>
        <% when "pdf" %>
            <img class="pictogram" src='images/pdf.png'>
        <% when "png", "jpg", "jpeg" %>
            <img class="pictogram" class="imageRaw" src="<%= entry.to_s %>">
        <% else %>
        <% end
       end %>
       <br>
       <span class="subFileText">
        <%= entry.to_s.split("/")[1][0..14] %>...
       </span>
    </div>
<% end %>
Fallen Angel
  • 163
  • 1
  • 4
  • 15
  • Rendering of the view depends on gem/file extension you use, this happens the same way in both Sinatra/Rails. If you add a haml gem and use *.haml extension template will be compiled using haml – Bohdan Apr 17 '17 at 13:20
  • @Bohdan, thanks, but that shows, that I should change all layouts to haml. If it is one-paged app, that can be suitable, but if it will be more complex app? Based on your comment, I conclude, that my question just can't be answered because of the idea in erb – Fallen Angel Apr 17 '17 at 13:30
  • @whodini9, maybe, my question is not clear, I will try to rephrase it: In erb, inside these `<% %>` how to output value of variable? – Fallen Angel Apr 17 '17 at 14:22
  • You need to use `<%=` for erb to print the executed code. What is printed here is analagous to what would print in irb (Read-Eval-Print) http://stackoverflow.com/questions/7996695/what-is-the-difference-between-and-in-erb-in-rails – whodini9 Apr 17 '17 at 14:43
  • @whodini9, is it possible to do this in alternative way? Like here: https://thepugautomatic.com/2013/06/helpers/ – Fallen Angel Apr 17 '17 at 14:47
  • You can try building your own method and calling it with `<%-`, but I'm not sure this will work. Can you explain why you can't use the `<%=`? Normally you would put your non-print code inside `<%`, then use the `=` explicitly for what you want to print. – whodini9 Apr 17 '17 at 14:51
  • @whodini9, that's true, but I have too much logic in my view. I'm getting the directory files\folders, and making some changes to properly output it up to 18 characters. Something like in the updated question. I wanna to check if name is more than 18 characters and then shrink it and add three dots – Fallen Angel Apr 17 '17 at 15:15

1 Answers1

1

Thanks, guys, I have finally found it. I have extended the app.rb file with

set :erb, :outvar => '@output_buffer'
def concat(text)
    @output_buffer << text
end

And it works. Just type in the .erb view

<% concat "Text that should be added to render" %>

And you all done. Hope, this will help to someone with similar question

Fallen Angel
  • 163
  • 1
  • 4
  • 15