Backbone.js and Mustache.js: Small Views and Templates
Short and concise HTML templates that represent repeatable blocks or isolated components of a page are easier to manage and easier for other developers to edit than longer templates which encompass larger portions of a page. A view object with the responsibility of rendering and managing multiple child views is used to organize the components of a page.
Consider navigation elements organized in lists under top level list items…
Each top level item likely uses the same child list to present it’s dropdowns. Rather then use one large template and one large collection of models utilizing smaller templates as components of the view remove hardcoding the hierarchy into both the model and the template but rather move the hierarchy to the view objects used to build the page. When the structure of the child lists needs to go through the change control process for added or removed presentation or a change in the hierarchy as directed by the business, the change is simple perhaps changed one portion of a template and the order of an array which dictates the hierarchy via enumerating. Also the business may dictate that the order of the menu follows various orders depending on visitor properties (e.g. member object either male or female). Then the menu’s components display order can be dictated programmatically, utilizing the small views and templates.
The best practice or goal emphasized above with respect to templates and views is KISS and DRY. As long as the implementation does not become overly complex and difficult to grok, keep the template code DRY, otherwise KISS principle overrides the need to have template code that does not repeat itself.
Code examples
A list item that will become part of various lists:
<nav><liclass="Women"><strong>Women</strong><sectionclass="Women-today"><em>Today's Events</em><ul><liclass="Women-today"><ahref="/event/12366stellaandjamiewc"data-sort="0">Stella & jamie</a></li><liclass="Women-today"><ahref="/event/12590classiqueblwtwc"data-sort="1">Classique</a></li></ul></section><sectionclass="Women-endingSoon"><em>Ending Soon</em><ul><liclass="Women-endingSoon"><ahref="/event/12125tahariblowoutwo"data-sort="8">Tahari Outerwear Blowout</a></li><liclass="Women-endingSoon"><ahref="/event/12588ecoskinblwtwc"data-sort="10">EcoSkin Blowout</a></li><liclass="Women-endingSoon"><ahref="/event/12053jewelmakblwtwj"data-sort="11">Jewelmak Blowout</a></li><liclass="Women-endingSoon"><ahref="/event/12393elfcosmeticswb"data-sort="15">e.l.f. Cosmetics</a></li><liclass="Women-endingSoon"><ahref="/event/12056tedbakerwatchesunisex"data-sort="16">Ted Baker Watches</a></li><liclass="Women-endingSoon"><ahref="/event/11634bootsitootsiwf"data-sort="17">BootsiTootsi</a></li><liclass="Women-endingSoon"><ahref="/event/12057lillybeewf"data-sort="18">Lillybee</a></li></ul></section></li><liclass="Home"><strong>Home</strong><sectionclass="Home-today"><em>Today's Events</em><ul><liclass="Home-today"><ahref="/event/11296bedfordcottageblwtda"data-sort="2">Bedford Cottage: Throws under $49</a></li><liclass="Home-today"><ahref="/event/12368ldchomebyjoeldessauleshda"data-sort="3">LDC Home by Joel Dessaules</a></li></ul></section><sectionclass="Home-endingSoon"><em>Ending Soon</em><ul><liclass="Home-endingSoon"><ahref="/event/12054ltdedthomaspaulfurniturehda"data-sort="12">Limited Edition: Thomas Paul Furniture</a></li><liclass="Home-endingSoon"><ahref="/event/12367pinodenaifinearthda"data-sort="14">Pino Denai (1939-2010) Fine Art Collection</a></li><liclass="Home-endingSoon"><ahref="/event/12592napastylewinecellarhda"data-sort="20">Napa Style: The Wine Cellar</a></li><liclass="Home-endingSoon"><ahref="/event/12170luxuryhotelsheetsblwthda"data-sort="21">Luxury Hotel Sheets Blowout</a></li><liclass="Home-endingSoon"><ahref="/event/12169floorcoutureblwthda"data-sort="22">Floor Couture Blowout</a></li></ul></section></li><liclass="Kids"><strong>Kids</strong><sectionclass="Kids-today"><em>Today's Events</em><ul><liclass="Kids-today"><ahref="/event/12369miabellebabycm"data-sort="4">Mia Belle Baby</a></li></ul></section><sectionclass="Kids-endingSoon"><em>Ending Soon</em><ul><liclass="Kids-endingSoon"><ahref="/event/12240stencilkidsblowoutcm"data-sort="19">Stencil Kids Blowout</a></li></ul></section></li><liclass="Getaways"><strong>Getaways</strong><sectionclass="Getaways-today"><em>Today's Events</em><ul><liclass="Getaways-today"><ahref="/event/Smoke_Tree_Ranch_Palm_Springs_622"data-sort="5">Smoke Tree Ranch Palm Springs </a></li><liclass="Getaways-today"><ahref="/event/Amara_Resort_Sedona_623"data-sort="6">Sedona, Amara Resort </a></li><liclass="Getaways-today"><ahref="/event/The_Andrew_Pinckney_Inn_Charleston_609"data-sort="7">The Andrew Pinckney Inn Charleston</a></li></ul></section><sectionclass="Getaways-endingSoon"><em>Ending Soon</em><ul><liclass="Getaways-endingSoon"><ahref="/event/Millennium_Scottsdale_Resort_Villas_608"data-sort="23">Scottsdale, Millennium Resort & Villas </a></li><liclass="Getaways-endingSoon"><ahref="/event/Hawaii_Prince_Hotel_Waikiki_610"data-sort="24">Hawaii Prince Hotel Waikiki</a></li></ul></section></li><liclass="Men"><strong>Men</strong><sectionclass="Men-today"><em>Today's Events</em><ul/></section><sectionclass="Men-endingSoon"><em>Ending Soon</em><ul><liclass="Men-endingSoon"><ahref="/event/12003tedbakermc"data-sort="9">Ted Baker Men</a></li><liclass="Men-endingSoon"><ahref="/event/12406mackagemo"data-sort="13">Mackage Men</a></li><liclass="Men-endingSoon"><ahref="/event/12367pinodenaifinearthda"data-sort="14">Pino Denai (1939-2010) Fine Art Collection</a></li><liclass="Men-endingSoon"><ahref="/event/12056tedbakerwatchesunisex"data-sort="16">Ted Baker Watches</a></li><liclass="Men-endingSoon"><ahref="/event/12592napastylewinecellarhda"data-sort="20">Napa Style: The Wine Cellar</a></li><liclass="Men-endingSoon"><ahref="/event/12170luxuryhotelsheetsblwthda"data-sort="21">Luxury Hotel Sheets Blowout</a></li></ul></section></li><liclass="Beauty"><strong>Beauty</strong><sectionclass="Beauty-today"><em>Today's Events</em><ul/></section><sectionclass="Beauty-endingSoon"><em>Ending Soon</em><ul><liclass="Beauty-endingSoon"><ahref="/event/12393elfcosmeticswb"data-sort="15">e.l.f. Cosmetics</a></li></ul></section></li></nav>