I'm using https://foundation.zurb.com/emails.html to develop my email templates. For each and every template there are parts where I need to add eg. <%= params.someData %> but '<' gets escaped while compiling, so instead of being <%= params.someData %> it generates to <%= params.firstName %>
I see that https://foundation.zurb.com/sites/docs/panini.html is used for compiling but I'm not able to disabled this functionality in order for < character not to be escaped.
Please help.
You can use the integrated raw helper to do this.
Either use {{{{raw}}}}...{{{{/raw}}}} or <raw>...</raw>.
In gulfile.babel.js use pipe to replace <%= with <%=. I have had to do this with Adobe Campaign code, which doesn't work well with Zurb Foundation for email.
.pipe($.replace, '<%=', `<%=`)
Good luck.
Related
I'm trying to set a html user sign in a plain text rails mail view.
When I was using multi part, html_safe was working, but when I changed it to a single format view, it doesn't work, even on a html partial.
Here's the code:
send_proposals_email.text.erb -
<%= #greeting %>,
...
<%= #user.sign.html_safe %>
And it gets me a
<p>Atenciosamente,</p>
<p>Leandro Pons Malheiros</p>
Anyone knows a way to avoid spam and make this work?
Have you tried this strip_tags?
strip_tags(#greeting)
stip_tags Documentation
Essentially I want something like the following:
<code class="snippet">
<%= html_escape do %>
My markup displayed to user
<% end %>
</code>
However the html_escape method does not accept a block. If this is not built into Rails API somewhere else, perhaps using some helper, does anyone have advice on how to make a custom helper where the yield statement output is captured into a string that I can then escape myself?
Thanks,
Keith
Rails' capture and escape_once helper methods can create a String from a block in an erb template and then output an escaped version of it:
<% snippet = capture do %>
My markup displayed to user
<% end %>
<code><%= escape_once snippet %></code>
content_for is another helper that provides similar functionality to capture, that you may consider using depending on the situation.
To explain, snippet is an ActiveSupport::SafeBuffer, and is why escape_once is needed. You could achieve the same by calling snippet.to_str instead of escape_once snippet (However .to_s will not work as that is different to .to_str in ActiveSupport::SafeBuffer).
Should I be using the content_tag helper for all html tags when working with Rails?
Is it The Rails Way to use content_tag for even simple things like Header tags?
<%= content_tag :h2, :class => "bla bla" do %>
Header
<% end %>
vs.
<h2>Header</h2>
Clearly just using straight html is much 'simpler' and 'shorter', but what is the correct Rails Way of doing things?
Using content_tag when you don't have to is a waste. There's no need to use ERBisms to generate static HTML so don't do it. If some other piece of code determines what tag to use, then you'd use content_tag to construct that tag.
If you are asking the rails way of doing this, then its defiantly using 'content_tag', but using tag_helpers has its own advantages and disadvantages
Personally for me I can see these things, (Using rails helpers instead of pure HTML)
Advantages
1 - Your code will be cleaner. (with less lines)
2 - You will have more control other the elements.
Ex: You can have your own helper tags like 'big_text_box' which will return a text box more than the normal with and you can use it across all the site
3 - You will be able to add attributes like class, id dynamically in the runtime
Disadvantages
1 - If you have a separate designer (I mean UI engineer) he/she will get confuse of by the code you have use. As its not pure html
2 - Its slow than the pure html (But this will not even noticeable unless otherwise your app is a damn major one...)
So its up to you to decide what to use, personally I prefer using rails helper tags as it makes me more comfortable
HTH
cheers
sameera
One useful method is the "div_for", which is somewhat similar to the content_tag. If you find yourself marking up HTML elements with data you can reference later, "div_for" makes your life much easier.
Let's say you have a bunch of people being shown on a page and you need to wrap each with a div that has a unique ID so you can modify these elements with JS. By hand and straight HTML you would have to do:
<% #people.each do |p| %>
<div id="person_<%= p.id %>"><%= p.name %></div>
<% end %>
That would get bothersome if you were doing LOTS of this with multiple attributes (I commonly use custom IDs, classes, and some data attributes). But using div_for you could write the above as:
<% #people.each do |p| %>
<%= div_for(p) do %><%= #person.name %><% end %>
<% end %>
Makes the HTML a little easier to read when things get long and complex. I found it is much cleaner when working with javascript a lot.
http://apidock.com/rails/ActionView/Helpers/RecordTagHelper/div_for
I have a lot of comments in Rails views.
How i can prevent rendering them ?
If I understand the question correctly, you're asking about Ruby/Rails comments vs HTML comments... Give this a try in your view:
<!-- This is an HTML comment and will show up in the HTML source! -->
Now try this:
<%# This is a comment that won't show up in the HTML source! %>
<%#
You can even use this for commenting multiple lines!
How useful!
%>
Does that help?
use =begin and =end to mark the beginning and end of your comment
There is no easy way to do that. You can monkey patch ERB sources, perhaps, but it is a bit nerdy.
I'm not a Rails programmer, but a quick but of Binging brought up this link:
http://blog.brijeshshah.com/strip-tags-in-rails-javascript-and-php/
The approach he's using is one that I've used in the past where you sanitize the view's output. sanitize being the name of the function you want to use before rendering the view.
Maybe you can use Haml Comments: -# allow to comment your haml code without them appearing in the generated html.
Kind of hackish, but you can wrap it in a helper method
In your view:
<% comment do %>
<%= "this won't be executed" %>
or displayed
<% end %>
in app/helpers/application_helper.rb
module ApplicationHelper
def comment(&block)
## you can do something with the block if you want
end
end
I have a Rails app (blog) that I am creating. Very basic stuff. In my content area I have a text area for the content of the post. I am needing to include some html in the text area (links, formating, etc).
<%= f.text_area :content %>
Is there another tag that I can use instead of text_area, that will allow me to do this?
Have you tried this in the views?
<%= content.html_safe %>
This is an old question but I guess there's a pretty straight forward view helper for this: sanitize. I presume you'd want to render the HTML tags entered by the user. For that purpose, save the content as a string and render as HTML using sanitize.
Example usage:
sanitize #post, tags: %w(strong em a code pre h2 h3 p blockquote ul ol li br),
attributes: %w(href class)
The tags option allows you to specify which tags to use and same with the html attributes.
The HTML safe method is actually .html_safe. Just tested on a text field.
For example:
<%= #item.description.html_safe %>
Are you looking for something similar to the bold, italic, ... options you get when posting in stackoverflow? If so, I would suggest Markitup, a text-editor plugin for jQuery. Once you get this set-up, you'll be able to enter mark up in your text area (e.g. Markdown, bbcode, ...). When you actually display the result on the page, you simply need to have Ruby parse the mark up language you chose.
E.g.
<%= #user.bio.bb_code %>
Using this method, you allow your users enter styled text in a safe fashion.
With the forthcoming Rails 6, you will be able to use a new rich_text_area tag to create a rich text editor in your forms like this:
<%= form_with(model: article) do |f| %>
<div class="field">
<%= f.label :content %>
<%= f.rich_text_area :content %>
</div>
<% end %>
See Action Text Rails Guide
text_area_tag is probably what you want.