Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices when including/using a "partial view"?

In modern web frameworks like Rails and symfony the concept of partial includes or partial views is well documented and recommended.

What I am having trouble with lately is deciding how much design to include in the partial.

It's kind of hard to explain but I want to know what others do when creating a partial and including it in a template. Do you only display the data and position it in the template or do you put all the styling and positioning code in the partial and just include it like so.

I guess my question is, what is your thought process when deciding to create a partial and when do you use it in your own code, and how much do you put into your partial when you decide to use one.

  • http://www.symfony-project.org/book/1_2/07-Inside-the-View-Layer#Partials
  • http://api.rubyonrails.org/classes/ActionView/Partials.html
like image 372
Peter D Avatar asked May 04 '09 17:05

Peter D


2 Answers

I use partials for pieces of view code that are used in more than one place. If the code is going to be duplicated or re-used in several places, then it is a prime candidate to be DRY'd up and placed into a partial.

As far as styling code, all that should reside in your CSS files

like image 123
cpjolicoeur Avatar answered Oct 04 '22 05:10

cpjolicoeur


If you're repeating parts of your view code, a partial is a good idea. If the code you're reusing is a little different between your pages, make use of the parameters you can pass the partial.

As for the CSS, I'd suggest keeping it outside your partial/template. If you have a lot of partial specific CSS code, create a "partials" folder under web/css and name the css files to match your partials (If you think the overhead is worth the organization).

like image 43
sjobe Avatar answered Oct 04 '22 06:10

sjobe