Reputation: 109
I am going through the rails guides: https://guides.rubyonrails.org/layouts_and_rendering.html
Can someone say what's the difference between a view, a layout and a template in a rails application? With examples and code snippets if necessary. Thank you ahead of time!
Upvotes: 0
Views: 322
Reputation: 191
What is a layout?
All pages of a website, usually have the same header and footer. For example, all pages on StackOverflow have the logo and search bar on top. And the same footer in the bottom.
So, instead of defining it for every page that makes up your application, you define it once in a layout. And you use that layout for all your pages.
Note: You do have the ability to have more than 1 layout.
What Is A View?
Another word for "View" is screen. It's one screen of your application. For example, on StackOverflow, there is an "answer view" or "answer screen". The details of the answer might change. Those details are pulled from the DB based on the URL. But, the whole output that fills up the screen is a View.
There might be other screens/views like "Login", "Help", "Posting A Question" etc.
What is a template?
This is any piece of HTML that you might want to use over and over. For example, all "comments" have the same HTML code. But, the data in them might differ. So, all comments share the same "template".
I hope this is clear.
Upvotes: 1