Performance difference between plain HTML and Rails/ERB helper templates - ruby-on-rails

I am new to rails and trying to understand this concept, as there are many things which we write using helpers(erb/rails tags) can also be written using simple plain html , is there any other advantage to using rails/erb helper than enabling to write more simple and readable code.
As the end result of writing the erb/rails template is always going to be a plain html , so initially by writing plain html do we reduce load on server or reduce servers efforts of converting the rails/erb templates into plain html.
Note: I am specifically asking for more of static templates e.g web forms , links , form contents,etc.

Here're some of merits.
Ability to Collaborate with models and helpers
It automatically generates post url and form labels etc. So, say you're changing the name of a model or url, if you were writing plain html for all templates, you'll have to manually replace all of occurrences on your own, whereas the "rails-way" can handle them all just with one line of modification or one command execution.
Can take advantage of template libraries.
There're lots of awesome template libraries that generate html from ruby code.
https://github.com/plataformatec/simple_form
https://github.com/justinfrench/formtastic
Gives you better abstraction
It gives you good abstraction in the way that it makes you write what you want instead of how you do. For example in my previous project, I was using bootstrap2 and decided to move to bootstrap3. If I were writing plain html, I had to see all html files, and inspect sometimes intricately structured html tags, classes, and find all bootstrap2 specific elements and change them all. But thanks to the template generation gem I was using, all I had to do was basically to upgrade the gem and add a few lines to some config files.

Related

How can I use a WYSIWYG editor with rails, but also have the data sanitized server side?

There's no scarcity of WYSIWYG editors, but it seems like there's no simple path to having one and keeping some semblance of protection from bypassing client side validation and including script and object tags.
My initial thought was to find a WYSIWYG editor which would output markdown, then store markdown formatted text in the db and parse on display. This would protect me from storing potentially dangerous code in the db, but also keep me from needing to whitelist every possible tag that the editor would put out as I would need to if it were HTML.
Am I missing some really easy path here? How does everyone else balance having a usable editor but not opening themselves wide open to attacks?
Ryan Grove's sanitize gem is very customizable, and I think the basic or relaxed modes would work for sanitizing raw html from the WYSIWYG editor (and you wouldn't have to whitelist a bunch of tags).

Using handlebarsjs as a template for Rails instead of HAML or Slim?

I'm potentially looking to standardize view/design templates for projects across the board with a singular template framework. Given that we are starting to see work for JS frameworks that can leverage Handlebars, it would seem potentially prudent to likewise begin using Handlebars to replace our usage of HAML (via Handlebars.rb and handlebars_assets).
Does this, experientially, work effectively? Or can you provide a link to some explanation of why or why not this will work or an example of such a use case?
Obviously HAML and (more sexy) Slim offer a cleaner template syntax for the most part, but Handlebars.js does have advantages that it carries from Liquid in that it's much more like plain HTML and therefore more "designer friendly."

Pure HTML template solution for Rails?

I love HAML, however recently our projects have come under some scrutiny with regards to reliance on non-html structured templates. I thought I'd take it a step further by asking the question, "How can we use pure HTML design based templates in Ruby on Rails?"
The closest thing I've found so far is a very interesting project that has it's most recent update from 2010 called Kwartz from the author of Erubis.
Is there a project that upholds this pure HTML isolation for designers that is up-to-date and viable on Rails 3.2.x?
Your designers are correct that HAML does not have widespread HTML tool support.
A really excellent solution IMHO is Handlebars. It is simpler than HAML, and will work with more HTML tools because Handlebars emphasizes moving code out of the page template and into the controller. This also is good for writing maintainable pages with designers and also for security.
Handlebars is led by Yehuda Katz, who helped write Rails 3, is a core contributor to JavaScript, and is currently working on Ember.js which also leverages Handlebars.
http://handlebarsjs.com/
You are either going to do something with the templates, right? As in, the designer gives you the template, you strip out the parts that already belong to app/views/layouts/application, inject the necessary ruby to get your data into the view etc.
So what's the problem? Let the designers provide their templates in HTML, you convert them to HAML when using in the app, instead of converting them to ERB.

Graceful degradation/progressive enhancement for Action Mailer templates?

Is there some gem or technique that will let us write only .html.erb templates for our Rails 3 mailers, and gracefully degrade them by stripping HTML tags for the text/plain version, rather than having to create each partial twice?
Google is seriously failing me, so I must be searching for the wrong terms.
take a look at premailer. It can generate text from html.
In general, this is not an easy problem. It might be easier if we were able to write semantic html markup and then it'd be easier to detect intent and convert the html into reasonable looking plain text.
But assuming your html emails are intended for a wide audience, it's full of all sorts of hacks that make the layout work in multiple email clients. This sort of dirty markup will make it harder to generate good looking plain text.
Another issue are things that won't clearly translate into text. Links that say "click here", will look funny in text, for example.
Well, if your html e-mail is simple and can be expressed as Markdown syntax you can use Markerb. It allows you to render multipart e-mails from a single template.

Best practice for rendering data in the Backbone view templates

I was using backbone standalone for some time but currently I am trying to integrate it with Rails. Until now I used underscore templates and the question would be if it is possible to use Rails view helpers inside the template and if it is smart thing to do at all?
Update: Here is a simple example what I am talking about.
I have a list of messages and I have a MessageView for each message, I want to render the avatar thumbnail of the message author, link to his profile and description when the message was posted. Also I use markdown for the message content. With underscore templates I don't have access to the helpers to achieve this so I am forced to create methods on the model itself which feels really wrong...
You should take a look at the EJS Embedded JavaScript Framework, which provides rails-like standard view helpers like link_to, url_for, and other form tags.
Of course, you will have to translate your custom rails templates in js, but it's a start !
I ran into the same problem where I wanted to reuse my templates between Backbone and Rails. I ran into stache before: https://github.com/agoragames/stache
You can read more about the setup here: http://slainer68.wordpress.com/2011/09/20/partial-reuse-between-rails-js-the-easy-way/
Right out of the box, your underscore templates are pure javascript, so, in that sense, you can't really embed rails helpers into them. You can, however, make those templates ejb's (or whatever templating system you use) and have rails render them. With so little information, it's impossible to figure out what your app does, but it does feel weird to me to do that. I think, typically, your javascript templates are used for rendering html on the host side after some js functionality. Maybe a better description of what you are trying to accomplish?
Update ...
So you have some set of relationships between messages and authors in your rails models correct? You'd do a similar thing in your backbone models. So, you've got a User model, and a Message model. User has_many Messages, and Message has_one User. You can model that out in backbone as well... see my answer here:
Backbone set collection attribute (for the url)
You just need to describe the relationship on the backbone side.

Resources