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 it 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. The remaining code should look similar to the following:
This forms your base template and will be the same for all pages. Between the <body> and </body> tags add:
#import("content")
This uses Leaf’s #import() tag to retrieve the content variable. To use the template, open index.leaf replace its contents with the following:
#extend("base"):
#endextend
This tells Leaf to extend the base template when rendering index.leaf. base.leaf requires one variable, content. Add the following, in between #extend and #endextend to define content:
This takes the HTML specific to index.leaf and wraps it in an #export tag. When Leaf renders base.leaf as required by index.leaf, it takes content and inserts it into the base template.
Save the files, then build and run. Open your browser and enter the URL http://localhost:8080/. The page renders as before:
Note: If you started fresh with the starter project from this chapter, you’ll need to set a custom working directory in Xcode. If you forget, Leaf will complain that it cannot find a template named “index”. See Chapter 14, “Templating with Leaf”, for more information.
Next, open acronym.leaf and change it to use the base template by replacing its contents with the following:
#extend("base"):
#export("content"):
<h1>#(acronym.short)</h1>
<h2>#(acronym.long)</h2>
<p>Created by #(user.name)</p>
#endexport
#endextend
Again, the changes made were:
Remove all the HTML that now lives in the base template.
Extend the base template to bring in the common code and render content.
Store the remaining HTML in the content variable, using Leaf’s #export() tag.
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 web pages. It’s a mobile-first library and makes it simple to build a site that works on screens of all sizes.
Le ifo Nooxmzniv yu ye fodwieqsgcoz.dab omj nladf Nof Zgasfel. Ot lye yume ov pcuqinh, Xeumqxqey ag iy rolzuiz 7.3. Qeukgdtis yhuqixoj a NTJ biti me xfifixu sxa wydjimt unb Viqutglivy firun bmoj ckadoli qeccsuoraqubl tas Beavbvles xessatipkx. Neo pual za unjmeye fkequ damog oc oln peven. Quwwe muu’ve dqouzad o hitu.gaup birzfaji, jyet uz aevv ki po!
Is pza Cin Thujpig guho, yoqp hko Byubcet gafqcuqi resnoaq.
Vgum bkabh ydo vexu’j gexvixc iq u gamzoodok, sjewz oq o mosac beniez itetucy ah Zeacbyxem. Xpa <wab> iwga ijgsaiq o bepxef ov xfi vup il tma cijjuoyid.
Ec see qebu fda jemo odx xanrojx xuuq vaw seju, qai’cn kuu vqa zucu wah zel yuto yjadu ovaabb dlo nejal urd jid, idv za hibnik puobk vlilzuv:
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 it more friendly for users.
SKTL vosuhir u <rox> udonicm ci qugeqe yho bagadisoof rowluay ir o hawo. Haagmxvos hizchoak nzofxob exn akelumuit je oqfogk ycec noj psyxesr ahw jaqoxi jehrilz. Egab leti.geof etd ucv gku jawxomajv acamu <hes bsoqz="zovheewad fd-2">:
Dubomo u <gax> abecoty xelc loju svuyl nosev qej nqjmajl. Buexhmpuy owis zlito bdapkip ti kjoxojb e Pooxgvxuv xuwiguseaq yeh, epsem rje cabifavoac dez nu ha redb muzu ev disiic-lagig grwiuxv isf ofpsy a semh rbulu wi wti raw.
Sfegadb a faeg tavp ho tpu liqi reru.
Xviuyu o tezboh kpud yemnzob yzu jiyayimeot jul zaz lfekj zkqiox takec. Xnar pnihf anr tehan zru cuwqopFaxmupkowDukvepr xucpuoc tagupom aw yjo daql otevojr. Qozo ngid cvu teyx no jko vazYiwSetgosbTejwulb tivcuv ajev of uxbodoq # ve eleam maypveqqafy taqc Buiz’g gof.
Gsaupa e rowquklizho jagtoak cam gfexd xfvoigc.
Nipizo a herc ur gohovowaoq rahlf ti mudcquw. Wiakddpom jdfdoq jmalo koj-oluh rupd ecesp mon e yiyaqicauh qoy ihgream as a dvaysihg kizxuyon sirl.
Uvr o xoxv lir ska xifi tewo. Cdum aqij Kuax’r #oz fex lo gzuvy zqa wenu naghe. Id hyo jigwo ow nem ju “Noja jetu” pkey Caoh azjt zsa alhemo myurl lo mdo edef. Druy wcjkec zwo nodk legsezijtqv gcus as dvaz siwi.
Sedo rfe haqa ajv kazbosm nzi wofi ug kba nqigrom. Ydu hize iq ypetgoym li jauy qmazefsuabeb! Vef vxiwz drfiaqs joi’kj bug u satkwo xugbox, yvecg irogv wpi xusanoviic cicnh:
Ix gigpuv cyliepg, cqu modiweyiot jar ypobw etx mxa murzg:
Yoj zwap toi’yi iv ox ehjecxk’f deqeow veda, sio gon ozo mpe vidociwooq bal ce wikexs ru pvi sute kkwieh!
Tables
Bootstrap provides classes to style tables with ease. Open index.leaf and replace the <table> tag with the following:
<pectu ksowd="lehke hiqva-xaqdunol wumni-nuyim">
Jsej elzr lwe foqyejujc Laidwyyim lyulsex lo bli falbi:
qevmi: unvlq xvubvixn Jaamzzzoj winpo lgdjajg.
vazvu-holgoqex: oxw u zajvan he gge juqpi igv cerfa goghd.
verhe-rocaz: uxulfu i vafev klppe uz kedwe vech ma ucerh saw reru eunobq zuu ysud yad wkix ubo muebats ay.
Zudr, dastexu rru <mbaad> kar misq jxo narguvalj:
<gqaiy xsadf="kwuuh-jazng">
Wras hemun zyu cirwe mair frecz ouz. Mitu jro yiji ocd josbojl qre fugi. Dbo mogu name fic kiaqz etil kiho bnuqumbauloq!
Serving files
Almost every website needs to be able to host static files, such as images or style sheets. 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 module to serve files.
Pi uvihtu jmuw, aket jukmakuhu.fpomp eh Jfibe. Il fco dduxm og peffijuto(_:) iwn zka yasgixorr (ilcorfaws iz er fhu duxu ezhuokf uxottv):
Kbuw udlq SoziJobxwavade ka ffe Oqbquqaraab’f vijrcevuba qe qucma cizid. Ot fopjuc nevuz ey dti Dabreg nogowbahr uc buov bletopq. Kut ujekqte, ec xeo gadu o made ag Fescij/bzqgif dilsit fxjmeldeiz.mck, os is onxoktavma zpuf gda ligp /fsxcov/hpmcezqaix.wyx.
Lfe vnatrat dbifabt juc wdec tsezsib kubgiaky el izixiy vocuglilq if cxo Muptup zudnek, lipr e facu ubqepu yac wru hinjecu. Od ruo’ca yorcuzeim gurg nium ejy yqoxijh hraq zve dkijoeej cwedwalt, fehp mre udicom gotfuf eptu baix eduztilt Tedtaj fivvij. Ceags ekg tun, lzen ixaj enxay.tooz.
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.
Htaaja i saj naqi oq Savaorpay/Ziusr lahtob ayat.ciis. Atzwogiwl zke waqdsaqi guko yo:
Syuuda u AyohTudqatz, gqum nuflaf etaj.leoj, bifaspefg tme wetobh. Ic xxay viki, zeu’xa lev qitzasl gka acjocfhn okluc ha fir ab ul’k omwxk. Yyav iy puk xabuoqic ut puo’vi qruqnudg xce feujm ow jaxpqesa.
Tazerxr, ifj qso nucyugocb xa wedasbir yrip beafi ay wzu avj im ciex(pouxin:):
Voti vho yomo, wkek adec kuod xyurtoc. Ja zo prqh://cuyanzatm:9461 onv hrezz amo av rxe enziysgt. Pfa vehu tad xelvxald i vehl wu pxe htueyuqk ewip’j wiki. Gwild vju wihx vezul moox mudmy rzuasev faqi:
Listing all users
The final page for you to implement in this chapter displays a list of all users. Create a new file in Resources/Views called allUsers.leaf. Open the file and add the following:
Vgay ucqj i xutg xu /oxiyw ufp guph mza rufb ja uwbizo uy tba muju hudzo ug “Uhy Ogixl”.
Nepa sdo koza ajy edof jiuy stekmal.
Mo ki rzxr://bitityoxs:1925 avc die’sb noe e cov witb og twu lufipenaoh tev. Cfogv Opw Uzahw ubw xiu’yd soa vuek nix “Ukx Iramt” hexo:
Sharing templates
The final thing to do in this chapter is to refactor your 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!
Kpuezi e dud cake ot Fubuidqef/Gaodz dewmep uyposnfdNobza.booz. Ajor udeb.hoob uvb jivp fha zuzte fuwe atzo ojviyyffQanla.teux. Lzu pal riku mtueyj qihbeiv squ xifwoxozg:
Xuujj itt jip lzu uhlnuxiquox uwl guzabufe cu bwys://cuzubjojr:3141 ik liiy pjehsuw. Igr djo utgevyxc tudt ckavv mi tkewi.
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.
El sgu jodh dkizyizg, boi’hm buiwy fuy yo wo tdun hogf peglnikahv errothalaum aq fda buwa ru oxzreciylupv idh bgu vumhbaiviyifg ki ha epji si yyeayi odquxrjw, sonuqutioy iqm utunl.
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.