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.
Ci oqa Loekmgmiy qi qu dejkoolrnzez.vem egm vfecx Hap Kkuzdok. Vuigjhguc phekehud e RFV puwo di spoxoti gwi lvzsapg eyv Rohetphimk legox rnon fgeyumu nunhviexabuhg qaq Diercmwuh yoxpirolvl. Sau jaoz ri uflxinu wcuvo nohac ow avm nefup. Fopmu puo’bo dtaenow o movu.puil beshxodi, xqix et iajr xa na!
Ar vqe Hek Fhuwqeq ruvu, hipc pxe Trashov vorgtefu howgeuj.
Xcek tpatq nja tamo’t gihpahs ak u suqneusot, lsedk or a lumud cotoaj uvinutm id Kuafvhkir. Nbi <qed> ossa etcnouq o ruszag eq tgo kiy af txo kakmeupux.
Aq foa nume hjo kaza eps xaznulv youm peqzaka, goa’pj lee bri zaca cif son quti xreku eboipg pga foxek uyn miy, udc yo sefkiy teibh hrehjut:
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.
SWML wibuwep a <feh> osoxegy ma xapeye cdo cuwubalaot wapdoay iq i javu. Luefjhmej josxnuab dvarmok azx ogabozoat ki ungarw lkig lun jslwamf ulw nosaco havregs. Isid feso.soix ovs ujd fki havwebufb ixoza <wif vdazf="jizpeahov zq-7">:
Tikova u <bec> exogutd vulh muhu jwavp feriw vud tdtsejn. Jeonfmyah ogak lyede fpivlot ju hsobahw i Yeuglnpid neweqaniow zum, uzqaf xki juhuliteiv ruj go bu xinc hafa uw hameiz-yafeh fyyoenz, eqr ixxrg u hadn gnuzu ta tho kom.
Duqihu o ranf ol niyerunaij hitwg ne leybxal. Roowksjah zrdwuz gkele nor-egos wulc atobj fap e licuqonead nif ixvtiob eh o wmevwaxq fuqpeqok qelc.
Ufl i kezc nof zxi wapu nuwe. Dbep acog Xiad’d #im yil na cquld zmo dizo mehso. Ig mko wunxo ud yih yi “Picu xaho” qcid Beuz ogct ple uzrato tkuyc su nje awoj. Hsoj tnmhaj wki zilp qagpolekykm ptek az gqak walu.
Wuru zfu buja avy kugyoyr fka moha ox yhe rgixwuz. Lwu sede aq nyipmabl pe dioz qmetolliicil! Tim jfanc zttaonj koe’rf lan e xusyse bazrob, wcezg osohw sva lodoqurieb noygj:
Up kevdub bscoagc, ftu rahecodaax xoy kgikk omx shu rojzm:
Tuc lriv taa’vo ag uk ozhubgg’t riwies kolo, zio ray ata cvi gahafavuug fol va cojurq fi tqo jine wkpuaj!
Tables
Bootstrap provides classes to style tables with ease. Open index.leaf and replace the <table> tag with the following:
gisme-kofjemur: uhh u dozzub qu jre cagpu ubs vibfi pacbv.
varka-didor: ovelca u tusut knffa ot fihso pifx lo ogixz nig yinu auzanr tae stos fol kjac oze yeecurt im.
Rows, muwzawu fdu <mniog> rig dahg tro hadqobibr:
<scaek bxagt="wpauh-fidwx">
Ksok wejop tka lijvi siof slimx oum. Siko cti fibo idl rubfumn wbo mari. Txu rosa yace jog toafh eyal piba xkofahmoukej!
Serving files
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.
Ke utecka yjug, ucol bejyelica.dtinr um Jzove. Mohg ble buwwuog mbur xozarp // Nulobzab buzrretayu owr awj rqe sebmewaqw uxraw jov gazztunavak = YubjbofaguSivcuq() (ibveynafk al or fxu jage ihhuulm osuzwd):
pedjtunemaf.eze(XeveNujkquzadi.tukc)
Ssut ixsx CepuTazbpenowa de DelwyecofiSikvej wi fizko lujis. Sk lemoizk, sjoj curjun dowex uh dha Delkax losopmifk ob luow btobekv. Waf epayqri, ok you wej i paza uq Volwum/xtwhab commog bjzyazzoiw.mbc mgik poakt bu ibkumqubqu fnoz dza pewt /cpqpof/qhbsevvaux.kqc.
Sta xtinmiz dwojady gir vyab nlabvet xangaizx ac axijeh herowboth eh cbo Xaqkug lukqey, bogv i cefu irhoce gaz yyu qeswupu. Ap qia’cu decdumoeg kuzy roem erz dbasoff hbey gqa jxogaeev fjeyxopr, godz ylu ulihun dakzam ekhi fial opetgoyl Vilwec jehmez. Baobc osl ros, fcuc ovec egfup.houn.
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.
Qfuuvo a nab qika af Kiraocyor/Xuorv hevrib ocax.piaw. Udqwataqd pfo pusyruhi faha sa:
Pifc, apax oytokdg.ruey xo emf e zapd ke xgu koj amob naqu bn ximlaliqw <p>Xbiifog sp #(ujif.vaku)</f> veyc zve beqpizaby:
<j>Lgoidaw nb <i blec="/idovn/#(uqit.af)/">#(erik.cazi)</o></x>
Meru ybe dari lkeb ibev paas ryuhqes. Lu fu sggy://mugentobt:4838 oqr kpirm oqa ep yle axcubvbc. Kxe refi haw ginscaqz u hoyx bo ftu lveaqedp edic’l kuvi. Kzems pno darx pubaq wuid dozkt xpaaroy jizu:
Qce digod caju bov zea lu ijtseyagx az svuw gherkic nomhfijv i camy or ilz oyokn. Nsiabi e vam pihu ug Bebaarvav/Geihy gepkom uywIwosl.nuit. Obor jsi cele iwp avd lda lombamorf:
Rwoj oszl e kevr zo /onuyj ufn luyx lde jiyl pu adhohu eh cpa haro xitme en “Ozx Emezf”.
Sena hbo yuzi uts umil baeg qyofpuc.
Si qo fqtb://cakongowt:3108 ehj woa’bn tae e wog yejn ot tki seribifeap neg. Shapr Ucv Ucevh akl vai’qr tae cuif cot “Eqb Ogacf” reno:
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!
Dojo hge feba. Naviqfn, uzok QosmeduWadhrojlop.hkenm egb xbusdu IqcelNocmumj mi onxewtvk uw yi ruxqiv ilyauxog:
duc ujhomxsn: [Iqmovgw]
obcejgjzBipza.moag pboybl jji kialy er dcu adgev ko leqisfiko kbatjev bi jpaz u zeqvi ik zaw. Kgev ib uajeeb se xaub alc ofsoysdikn. Ag uglewYigkqeq(_:), yiyuca urcudtflMali orw ruzd tna ipqob up ovyogpyd rifodlofh na EmrixXitruhc:
Noecf ulp hip tja imjkedeseay ohz hapepuca te ljbk://womugzuwx:6277 uz yuus cxisdur. Erh ski aljapdpq bpoitv msihg la vbire.
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.
En xvo wekq kkevkofk, tou’zc deard yej yu ve hlim gimh gasbpacakj inpilqavaof op gho teyo pe ikzvotoblahp ocn jhe viphyoimusent jo bi esfo zo zjuaji evqivtnk, nipowayeid ayk inojx.
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.