Reducing Maintenance with Layouts and Partials

So far in this chapter we’ve looked at templates as isolated chunks of code and HTML. But one of the driving ideas behind Rails is honoring the DRY principle and eliminating the need for duplication. The average website, though, has lots of duplication:

  • Many pages share the same tops, tails, and sidebars.

  • Multiple pages may contain the same snippets of rendered HTML (a blog site, for example, may display an article in multiple places).

  • The same functionality may appear in multiple places. Many sites have a standard search component or a polling component that appears in most of the sites’ sidebars.

Rails provides both layouts and partials that reduce the need for duplication in these three ...

Get Agile Web Development with Rails 6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.