Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between a helper and a partial?

Functionally speaking, both seem like subroutines that generate HTML based on certain parameters.

Is the decision to use one or the other in a given circumstance purely aesthetic?

like image 373
Tom Lehman Avatar asked Mar 11 '09 05:03

Tom Lehman


2 Answers

Helpers and partials are both intended to be used in your view layer. The difference is that helpers are primarily ruby code, intended to be used as a library of methods to call from your templates. Partials are .html.erb files containing partial templates that can be inserted into your main templates. So in short, logic should go into helpers and reusable html snippets (with minimal embedded logic) should be in partials. Hope this helps!

like image 161
Adam Alexander Avatar answered Nov 04 '22 16:11

Adam Alexander


The decision is not purely aesthetic. You want to use helpers inside your views. For example, you would use a partial to generate the html for a product, but within that partial (or any other view), you could use a helper to generate part of the partial, that maybe needs some logic to determine exactly what to display.

like image 26
danengle Avatar answered Nov 04 '22 17:11

danengle