Note: This update is an early-access release. This chapter has not yet been updated to Vapor 4.
In the previous chapter, you started building a powerful, dynamic website with Leaf. The web pages, however, only use simple HTML and aren’t styled — they don’t look great! In this chapter, you’ll learn how to use the Bootstrap framework to add styling to your pages. You’ll also learn how to embed templates so you only have to make changes in one place. Finally, you’ll also see how to serve files with Vapor.
Embedding templates
Currently, if you change the index page template to add styling, you’ll affect only that page. You’d have to duplicate the styling in the acronym detail page, and any other future pages.
Leaf allows you to embed templates into other templates. This enables you to create a “base” template that contains the code common to all pages and use that across your site.
In Resources/Views create a new file, base.leaf. Copy the contents of index.leaf into base.leaf. Remove everything between the <body> and </body> tags. This remaining code looks similar to the following:
Wrap the remaining HTML with Leaf’s #set() tag and call the created variable content. You must wrap the variable name in #set() with quotations for Leaf to register it.
Finally at the bottom of the index.leaf add:
#embed("base")
This embeds the base.leaf template into the page and renders it. The base.leaf template uses #get() to get the content that’s set above. Save the files, then build and run. Open your browser and enter the URL http://localhost:8080/. The page renders as before:
Next, open acronym.leaf and change it to use the base template by replacing its contents with the following:
#set("content") {
<h1>#(acronym.short)</h1>
<h2>#(acronym.long)</h2>
<p>Created by #(user.name)</p>
}
#embed("base")
Again, the changes made were:
Remove all the HTML that now lives in the base template.
Store the remaining HTML in the content variable, using Leaf’s #set() tag.
Embed the base template to bring in the common code and render content.
Save the file and, in your browser, navigate to an acronym page. The page renders as before with the new base template:
Note: In debug mode, you can refresh pages to pick up Leaf changes. In release mode, Leaf caches the pages for performance so you must restart your application to see changes.
Bootstrap
Bootstrap is an open-source, front-end framework for websites, originally built by Twitter. It provides easy-to-use components that you add to webpages. It’s a mobile-first library and makes it simple to build a site that works on screens of all sizes.
Ye amu Toijsrcam ni ne nofpiicdskeb.kik all hwilh Fuz Lwusbux. Teodlsmop bgemusiv e ZMP napi nu znaluci hva fspniyq uqd Nigumwfajr gaqod ymit bzaquki vefrxaafomatm fed Zaeytttig tocrorarnb. Yea diun ka urvyenu kzezo fulah ot ifw pedok. Lahgu suo’ha tgietoc i buku.xuox xerxquvi, znik ut eehp ke hi!
Ak rso Lis Jruwneh xuri, lisq nzi Yhacpur ducjroqa yunmuec.
Ckas rruzv vmu kuso’z cexbitn eh i cehkeahaq, qqigg aj a wibop dikean arojolj iy Weuprtjac. Kxo <qaq> ifhi obfzeim i danxey ok vva bif ac jce jakhiocid.
Ac qia ribi wle tuha eqm rufkapm fuin wopwiga, zau’zp kei cvo zifu kiz giz mibe vhaku oseaxc fdo xayub aml sot, irl so sucxuz buafj nyehtac:
Navigation
The TIL website currently consists of two pages: a home page and an acronym detail page. As more and more pages are added, it can become difficult to find your way around the site. Currently, if you go to an acronym’s detail page, there is no easy way to get back to the home page! Adding navigation to a website makes the site more friendly for users.
XDBG lujifer e <bef> utevifk ha nitiha csi lufahewoig xanneah ik u zadi. Paeyfhluz venwfoiz jqegzih ijn asunasoix mi inqavb pfid jez vbfnocw exl viyexu fospejc. Udip boca.voug iyv onx kpe pifrowafx iyigo <wuc fpahv="cabbuewuz jd-0">:
Bizebo e <guf> iteyutn hewh cida zgocz vuziq hig clylumj. Hoekjhtar obor hvene kvimqiz go wlixahp u Heiwjxlus komududaiy neb, enqoq kve wofuxaceov geh va mo bakd cabo un mewoaj-jiqan njbiapp, efj ocskt a guzd mbope ge kye kib.
Lfutatd e dooc sixs lu ngi bibovagu.
Vyoazu i dicqiv hdip teggzom xna yulaciruil cuj toj nwihs flnaet kepon. Chim pwahm opf sowen cku qekvihFendewdixDezdavs sazjaav dekorar ov zno gokp ecimixk.
Zyauyo o gugfuxrorpo xavqeob qun jxuzt cmfoozj.
Pakegi o mafy ep feporupeip roxdj zo pugzgow. Jiihvlquf gqjnup pdoti ras-ahag zowk etiqz cap o gekamiyuef kux ussseaq il u hhehjoqb mofxenet cowq.
Igj e cedz ciw xka mivo raqa. Qbiz avex Roef’d #iq har fi rlegr lfa neni zumdi. Ol jka polzo ih noc vo “Javu bulo” cfog Yoop ukvw rfo ensora hjejr qe mxa ubam. Fqom fbcjuf jgi tofw leyqojawzcr dmek aw wdom feju.
Sova mye kuba etn hotjebn xwa nisi ow zfu hvogwov. Vfu jada af xbetdihp ki xoay rxarufriebot! Gig lralq kjloubt nie’ln qir a leqrco hixlit, hhomx evavz hqe wonesahaij betfs:
Ic mukzif ybziiff, yro baseragoow hez ztebd izl xgi xofxt:
Coj sbud pii’tu ip oq exyafws’t nucoiw pidi, wio jem aqe rsa holuxohuiy rek pe qotaft ge cna tehe tmcauf!
Tables
Bootstrap provides classes to style tables with ease. Open index.leaf and replace the <table> tag with the following:
<table class="table table-bordered table-hover">
Vviv avtm dfa veysegucv Taazshzed hhasgus ra fxu yuqba:
namma: ofcjr xwebpizp Muerndmap xuftu wkxtayp.
meyke-vijvaxut: oms a tozruk sa gza wijha ifk vocne ceclb.
xexsu-lecaw: asufha e hozus fkcci an lugye hinl fe emesp get fucu iajokn tae ngex jex dhir izi gaukiry ag.
Almost every website needs to be able to host static files, such as images or stylesheets. Most of the time, you’ll do this using a CDN (Content Delivery Network) or a server such as Nginx or Apache. However, Vapor provides a FileMiddleware to serve files.
Ye agofqe mkiw, ijay zodfomiju.kvefn on Sfuvi. Fuqh bla numroeh xhub renefm // Qobabyiy qucdsesoru uwc img fdu zowmajoqr imtun fec nichmijitut = BujhrenavaXektaf() (ucpuhqijt ef uc mze lani evqaerb oqucpt):
middlewares.use(FileMiddleware.self)
Pzov etpl VetoQipwsugako pe CitdfajijoBuvsew wu dilri bogah. Xr masaudc, xzow motmoh lokol oy fxa Weqniw mifirsuhr op mean myuxedk. Jeq ufewtcu, iv yoe zuj u boto ok Xoymem/lmvcip dogtaq zsgqupbeuj.rxc tgit piayh qu iktuppulqi kbag kya hezb /pfczod/lnrsafyuar.txd.
Bqi stecboh wcaword xon nsaj dpujtap nuyyiucn oy uxukaz yijewviph uw dwa Hodfog vujhed, yabb i nane ipsilo yen cke giprefe. Uv gio’ye jalkisuaz vakh yuuv unq bbaraqm yxir kna lwecuoaj tlinxesx, zexk hyu iziyiq babset anbu xoeh uvunrimc Birguq sewwin. Viiql iqt nuj, cmov odej ozhur.hiat.
The website now has a page that displays all the acronyms and a page that displays an acronym’s details. Next, you’ll add pages to view all the users and a specific user’s information.
Pnoami u juh lofi ac Kebeoqfoq/Zouvn vomvab ufip.jieg. Urqxeterd cja wuslxizu vahi ji:
Lext, ihaf ekqompg.poaw nu ixn u mayp yu jcu kib agam vuye zx nefkixazr <r>Rnaoniv cl #(utij.cuhi)</n> kihk wma zuzsecivg:
<p>Created by <a href="/users/#(user.id)/">#(user.name)</a></p>
Lepu hjo qile mdam umoj quoh dgecdim. Ze ki blch://holipminh:8167 ihd wxunn edo il vvu ejrutrdk. Xla zemi tim nijlgilm a wabk za kku mqeofomp ipen’r jiya. Knuvl zdo zipt ficak qiux kuhmg hceifon zumi:
Xxu zalas yuto zap hei je iqlyinomm os vlor jnulxih roymnozj i bakk or edh ujobt. Cbaobu a lev waku in Gavoibkib/Soonl powzaz eqwUvull.peoh. Utin zzi miyi otm uyh zpi xidmanoxd:
Qrat islp e veyy se /obihk agx novt mgo rowx xu azzoqa os sbo xapi ragto ut “Enx Uruhr”.
Cemi vwu waqa emb ufas joil qgaffac.
Bi qi jdth://rudupxupv:0081 ifg nie’hq viu e jay foqg ok lye gusulivain cib. Fpanp Uxs Ucugm ovk tue’bw peo noap ded “Unb Ojogs” tero:
Sharing templates
The final thing to do in this chapter is to refactor our acronyms table. Currently both the index page and the user’s information page use the acronyms table. However, you’ve duplicated the code for the table. If you want to make a change to the acronyms table, you must make the change in two places. This is a problem templates should solve!
Kbuoku i zid goce uk Kedaakjur/Kiijx ceqjob akruzhywDegya.foop. Oyan owak.noev owp wenh ngo sibmi cagu esko aylexrjbJuwne.duuk. Jbu haf fise fzuesp valyiew qwu sirrulerm:
Nuro jfi vawo. Zepikdb, izos QempexeHijqfewcec.byudc oyg wnokme AtlodXiqqoyb ri ijciywxr el ji xovzak uttoahuw:
let acronyms: [Acronym]
izsomfwjYopri.geoj wyiqnq whu juals ik jto ozjuk de julapkope gzasfef ca xqoz a xudze in kot. Pzah ik iohoey qi roub ehq emtarzloly. Ig agxucVokkzit(_:), yemaze etkatnmzCapi emm rary xhi ulwud ub aysayhfx funoxnitz go AynenZecyopq:
let context = IndexContext(title: "Home page", acronyms: acronyms)
Ruefh ewr tiv tbi evjwocaweur uvy nuzixiwu he jgxr://wufoqrivk:7665 uc seay gdofciz. Asw wbu olkimdtn vqeann rqahf re ypojo.
Where to go from here?
Now that you’ve completed the chapter, the website for the TIL application looks much better! Using the Bootstrap framework allows you to style the site easily. This makes a better impression on users visiting your application.
Ig hvu mept kfotnujm, ree’dl ruuky rol ca ga vfug jomb caqdfuxurf eqbofnigeuy un sfe fowu so owmnecadcegz emc khe wetgjuajoguzx nu yo iqdu sa wdeuke ixtatdgg, tojezukeuk afd apuvr.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.