├── .gitignore ├── .gitmodules ├── README.md ├── assets ├── _custom.scss └── _variables.scss ├── config ├── _default │ └── config.toml ├── pdf │ └── config.toml └── website │ └── config.toml ├── content ├── _index.md ├── all │ └── _index.md ├── docs │ ├── example │ │ ├── _index.md │ │ ├── collapsed │ │ │ ├── 3rd-level │ │ │ │ ├── 4th-level.md │ │ │ │ └── _index.md │ │ │ └── _index.md │ │ ├── hidden.md │ │ └── table-of-contents │ │ │ ├── _index.md │ │ │ ├── with-toc.md │ │ │ └── without-toc.md │ └── shortcodes │ │ ├── _index.md │ │ ├── buttons.md │ │ ├── columns.md │ │ ├── details.md │ │ ├── expand.md │ │ ├── hints.md │ │ ├── katex.md │ │ ├── mermaid.md │ │ └── tabs.md ├── menu │ └── index.md └── posts │ ├── _index.md │ ├── creating-a-new-theme.md │ ├── goisforlovers.md │ ├── hugoisforlovers.md │ └── migrate-from-jekyll.md ├── layouts-pdf ├── all │ └── list.html └── pageinpdf.html ├── layouts └── partials │ └── docs │ └── menu.html ├── netlify.toml ├── package-lock.json ├── package.json ├── resources └── _gen │ └── assets │ └── scss │ ├── book.scss_50fc8c04e12a2f59027287995557ceff.content │ ├── book.scss_50fc8c04e12a2f59027287995557ceff.json │ └── example │ ├── book.scss_50fc8c04e12a2f59027287995557ceff.content │ └── book.scss_50fc8c04e12a2f59027287995557ceff.json └── testbook.Rproj /.gitignore: -------------------------------------------------------------------------------- 1 | .Rhistory 2 | .RData 3 | .Rproj.user 4 | public 5 | public2 6 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "themes/hugo-book"] 2 | path = themes/hugo-book 3 | url = https://github.com/alex-shpak/hugo-book.git 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # testbook 3 | 4 | 5 | [![Netlify Status](https://api.netlify.com/api/v1/badges/9a2052c9-6b2c-42b2-bc92-32715076e447/deploy-status)](https://app.netlify.com/sites/hugo-pagedjs-book/deploys) 6 | [![Project Status: Concept – Minimal or no implementation has been done yet, or the repository is only intended to be a limited example, demo, or proof-of-concept.](https://www.repostatus.org/badges/latest/concept.svg)](https://www.repostatus.org/#concept) 7 | 8 | 9 | The goal of testbook is to try and generate a beautiful book as html and PDF using [Hugo](https://gohugo.io/) and [paged.js](https://www.pagedjs.org/). 10 | It's a proof-of-concept. 11 | 12 | The .md files could be created from .Rmd, therefore making this an alternative to the excellent [`bookdown`](https://github.com/rstudio/bookdown). 13 | Compared to creating a gitbook with `bookdown`, one could customize the html website more easily, and use any Hugo feature. 14 | 15 | This proof-of-concept is based on the great [hugo-book theme](https://github.com/alex-shpak/hugo-book). 16 | This repo just adds a bit of setup magic (Netlify config, config/ dir) to work with existing great tools. 17 | 18 | The idea is to run two Hugo builds with [different configurations](https://gohugo.io/getting-started/configuration/) 19 | 20 | * One of them, `hugo -d public --environment 'website'`, creates the html website with a link to "book.pdf" somewhere, in the public/ folder. 21 | * The second one, `hugo -d public2 --environment 'pdf'`, creates a website with a special page containing _all_ the book content, and potentially a print CSS stylesheet. 22 | * Then that page is printed to PDF using pagedjs-cli and copied as "book.pdf" to public/. `pagedjs-cli public2/all/index.html -o public/book.pdf"` 23 | * The live website uses public so it has the website and the PDF. 24 | 25 | Netlify can run all these commands 26 | 27 | ```toml 28 | [build] 29 | publish = "/public" 30 | command = "hugo -d public --environment 'website' && hugo -d public2 --environment 'pdf' && pagedjs-cli public2/all/index.html -o public/book.pdf" 31 | ``` 32 | 33 | The dependency on pagedjs-cli is indicated with packages.json. 34 | 35 | ## Further work 36 | 37 | If I decide to take this further, that is! 38 | 39 | * Actually work on the CSS print sheet. 40 | * Make Katex, Mermaid etc. work for the PDF version. 41 | * Make hugo-book features for ordering/including content work. 42 | * Try and embed a tweet. 43 | * Try the idea with another book theme? 44 | * Write some content from Rmd. 45 | 46 | ## Related work 47 | 48 | To get a paged.js button in your Hugo website (i.e. not pre-generating the PDF, letting the reader do that from their browser) check out the [Hugo paged-js theme](https://gitlab.pagedmedia.org/julientaq/pagedjs-hugo) that you could use as a [theme component](https://gohugo.io/hugo-modules/theme-components/). 49 | -------------------------------------------------------------------------------- /assets/_custom.scss: -------------------------------------------------------------------------------- 1 | /* You can add custom styles here. */ 2 | 3 | // @import "plugins/numbered"; 4 | // @import "plugins/scrollbars"; 5 | -------------------------------------------------------------------------------- /assets/_variables.scss: -------------------------------------------------------------------------------- 1 | /* You can override SASS variables here. */ 2 | 3 | // @import "plugins/dark"; 4 | -------------------------------------------------------------------------------- /config/_default/config.toml: -------------------------------------------------------------------------------- 1 | # hugo server --minify --themesDir ... --baseURL=http://0.0.0.0:1313/theme/hugo-book/ 2 | 3 | baseURL = 'https://hugo-pagedjs-book.netlify.app/' 4 | title = 'Hugo Book' 5 | theme = 'hugo-book' 6 | 7 | # Book configuration 8 | disablePathToLower = true 9 | enableGitInfo = true 10 | 11 | # Needed for mermaid/katex shortcodes 12 | [markup] 13 | [markup.goldmark.renderer] 14 | unsafe = true 15 | 16 | [markup.tableOfContents] 17 | startLevel = 1 18 | 19 | # Multi-lingual mode config 20 | # There are different options to translate files 21 | # See https://gohugo.io/content-management/multilingual/#translation-by-filename 22 | # And https://gohugo.io/content-management/multilingual/#translation-by-content-directory 23 | [languages] 24 | [languages.en] 25 | languageName = 'English' 26 | contentDir = 'content' 27 | weight = 1 28 | 29 | [menu] 30 | # [[menu.before]] 31 | [[menu.after]] 32 | name = "Github" 33 | url = "https://github.com/alex-shpak/hugo-book" 34 | weight = 10 35 | 36 | [[menu.after]] 37 | name = "Hugo Themes" 38 | url = "https://themes.gohugo.io/hugo-book/" 39 | weight = 20 40 | 41 | [params] 42 | # (Optional, default true) Controls table of contents visibility on right side of pages. 43 | # Start and end levels can be controlled with markup.tableOfContents setting. 44 | # You can also specify this parameter per page in front matter. 45 | BookToC = true 46 | 47 | # (Optional, default none) Set the path to a logo for the book. If the logo is 48 | # /static/logo.png then the path would be logo.png 49 | # BookLogo = 'logo.png' 50 | 51 | # (Optional, default none) Set leaf bundle to render as side menu 52 | # When not specified file structure and weights will be used 53 | # BookMenuBundle = '/menu' 54 | 55 | # (Optional, default docs) Specify root page to render child pages as menu. 56 | # Page is resoled by .GetPage function: https://gohugo.io/functions/getpage/ 57 | # For backward compatibility you can set '*' to render all sections to menu. Acts same as '/' 58 | BookSection = 'docs' 59 | -------------------------------------------------------------------------------- /config/pdf/config.toml: -------------------------------------------------------------------------------- 1 | layoutDir = 'layouts-pdf' -------------------------------------------------------------------------------- /config/website/config.toml: -------------------------------------------------------------------------------- 1 | layoutDir = 'layouts' 2 | 3 | # Set source repository location. 4 | # Used for 'Last Modified' and 'Edit this page' links. 5 | BookRepo = 'https://github.com/alex-shpak/hugo-book' 6 | 7 | # Enable "Edit this page" links for 'doc' page type. 8 | # Disabled by default. Uncomment to enable. Requires 'BookRepo' param. 9 | # Edit path must point to root directory of repo. 10 | BookEditPath = 'edit/master/' 11 | 12 | # Configure the date format used on the pages 13 | # - In git information 14 | # - In blog posts 15 | BookDateFormat = 'January 2, 2006' 16 | 17 | # (Optional, default true) Enables search function with flexsearch, 18 | # Index is built on fly, therefore it might slowdown your website. 19 | # Configuration for indexing can be adjusted in i18n folder per language. 20 | BookSearch = true 21 | 22 | # (Optional, default true) Enables comments template on pages 23 | # By default partals/docs/comments.html includes Disqus template 24 | # See https://gohugo.io/content-management/comments/#configure-disqus 25 | # Can be overwritten by same param in page frontmatter 26 | BookComments = true 27 | 28 | # /!\ This is an experimental feature, might be removed or changed at any time 29 | # (Optional, experimental, default false) Enables portable links and link checks in markdown pages. 30 | # Portable links meant to work with text editors and let you write markdown without {{< relref >}} shortcode 31 | # Theme will print warning if page referenced in markdown does not exists. 32 | BookPortableLinks = true 33 | 34 | # /!\ This is an experimental feature, might be removed or changed at any time 35 | # (Optional, experimental, default false) Enables service worker that caches visited pages and resources for offline use. 36 | BookServiceWorker = true 37 | -------------------------------------------------------------------------------- /content/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Introduction 3 | type: docs 4 | --- 5 | 6 | # Writing a book with Hugo hugo-book and Paged.js 7 | 8 | {{< columns >}} 9 | ## Hugo 10 | 11 | Such a cool framework! 12 | 13 | Using the [hugo-book theme](https://github.com/alex-shpak/hugo-book). 14 | 15 | Markdown files could be generated from R Markdown files. 16 | 17 | <---> 18 | 19 | ## paged.js 20 | 21 | Paged content, thus PDF, without LaTeX! 22 | {{< /columns >}} 23 | 24 | 25 | ## Beware 26 | 27 | This is a conceptual thing. -------------------------------------------------------------------------------- /content/all/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: See PDF 3 | --- 4 | -------------------------------------------------------------------------------- /content/docs/example/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | weight: 1 3 | bookFlatSection: true 4 | title: "Example Site" 5 | --- 6 | 7 | # Introduction 8 | 9 | ## Ferre hinnitibus erat accipitrem dixi Troiae tollens 10 | 11 | Lorem markdownum, a quoque nutu est *quodcumque mandasset* veluti. Passim 12 | inportuna totidemque nympha fert; repetens pendent, poenarum guttura sed vacet 13 | non, mortali undas. Omnis pharetramque gramen portentificisque membris servatum 14 | novabis fallit de nubibus atque silvas mihi. **Dixit repetitaque Quid**; verrit 15 | longa; sententia [mandat](http://pastor-ad.io/questussilvas) quascumque nescio 16 | solebat [litore](http://lacrimas-ab.net/); noctes. *Hostem haerentem* circuit 17 | [plenaque tamen](http://www.sine.io/in). 18 | 19 | - Pedum ne indigenae finire invergens carpebat 20 | - Velit posses summoque 21 | - De fumos illa foret 22 | 23 | ## Est simul fameque tauri qua ad 24 | 25 | Locum nullus nisi vomentes. Ab Persea sermone vela, miratur aratro; eandem 26 | Argolicas gener. 27 | 28 | ## Me sol 29 | 30 | Nec dis certa fuit socer, Nonacria **dies** manet tacitaque sibi? Sucis est 31 | iactata Castrumque iudex, et iactato quoque terraeque es tandem et maternos 32 | vittis. Lumina litus bene poenamque animos callem ne tuas in leones illam dea 33 | cadunt genus, et pleno nunc in quod. Anumque crescentesque sanguinis 34 | [progenies](http://www.late.net/alimentavirides) nuribus rustica tinguet. Pater 35 | omnes liquido creditis noctem. 36 | 37 | if (mirrored(icmp_dvd_pim, 3, smbMirroredHard) != lion(clickImportQueue, 38 | viralItunesBalancing, bankruptcy_file_pptp)) { 39 | file += ip_cybercrime_suffix; 40 | } 41 | if (runtimeSmartRom == netMarketingWord) { 42 | virusBalancingWin *= scriptPromptBespoke + raster(post_drive, 43 | windowsSli); 44 | cd = address_hertz_trojan; 45 | soap_ccd.pcbServerGigahertz(asp_hardware_isa, offlinePeopleware, nui); 46 | } else { 47 | megabyte.api = modem_flowchart - web + syntaxHalftoneAddress; 48 | } 49 | if (3 < mebibyteNetworkAnimated) { 50 | pharming_regular_error *= jsp_ribbon + algorithm * recycleMediaKindle( 51 | dvrSyntax, cdma); 52 | adf_sla *= hoverCropDrive; 53 | templateNtfs = -1 - vertical; 54 | } else { 55 | expressionCompressionVariable.bootMulti = white_eup_javascript( 56 | table_suffix); 57 | guidPpiPram.tracerouteLinux += rtfTerabyteQuicktime(1, 58 | managementRosetta(webcamActivex), 740874); 59 | } 60 | var virusTweetSsl = nullGigo; 61 | 62 | ## Trepident sitimque 63 | 64 | Sentiet et ferali errorem fessam, coercet superbus, Ascaniumque in pennis 65 | mediis; dolor? Vidit imi **Aeacon** perfida propositos adde, tua Somni Fluctibus 66 | errante lustrat non. 67 | 68 | Tamen inde, vos videt e flammis Scythica parantem rupisque pectora umbras. Haec 69 | ficta canistris repercusso simul ego aris Dixit! Esse Fama trepidare hunc 70 | crescendo vigor ululasse vertice *exspatiantur* celer tepidique petita aversata 71 | oculis iussa est me ferro. 72 | -------------------------------------------------------------------------------- /content/docs/example/collapsed/3rd-level/4th-level.md: -------------------------------------------------------------------------------- 1 | # 4th Level of Menu 2 | 3 | ## Caesorum illa tu sentit micat vestes papyriferi 4 | 5 | Inde aderam facti; Theseus vis de tauri illa peream. Oculos **uberaque** non 6 | regisque vobis cursuque, opus venit quam vulnera. Et maiora necemque, lege modo; 7 | gestanda nitidi, vero? Dum ne pectoraque testantur. 8 | 9 | Venasque repulsa Samos qui, exspectatum eram animosque hinc, [aut 10 | manes](http://www.creveratnon.net/apricaaetheriis), Assyrii. Cupiens auctoribus 11 | pariter rubet, profana magni super nocens. Vos ius sibilat inpar turba visae 12 | iusto! Sedes ante dum superest **extrema**. 13 | -------------------------------------------------------------------------------- /content/docs/example/collapsed/3rd-level/_index.md: -------------------------------------------------------------------------------- 1 | # 3rd Level of Menu 2 | 3 | Nefas discordemque domino montes numen tum humili nexilibusque exit, Iove. Quae 4 | miror esse, scelerisque Melaneus viribus. Miseri laurus. Hoc est proposita me 5 | ante aliquid, aura inponere candidioribus quidque accendit bella, sumpta. 6 | Intravit quam erat figentem hunc, motus de fontes parvo tempestate. 7 | 8 | iscsi_virus = pitch(json_in_on(eupViral), 9 | northbridge_services_troubleshooting, personal( 10 | firmware_rw.trash_rw_crm.device(interactive_gopher_personal, 11 | software, -1), megabit, ergonomicsSoftware(cmyk_usb_panel, 12 | mips_whitelist_duplex, cpa))); 13 | if (5) { 14 | managementNetwork += dma - boolean; 15 | kilohertz_token = 2; 16 | honeypot_affiliate_ergonomics = fiber; 17 | } 18 | mouseNorthbridge = byte(nybble_xmp_modem.horse_subnet( 19 | analogThroughputService * graphicPoint, drop(daw_bit, dnsIntranet), 20 | gateway_ospf), repository.domain_key.mouse(serverData(fileNetwork, 21 | trim_duplex_file), cellTapeDirect, token_tooltip_mashup( 22 | ripcordingMashup))); 23 | module_it = honeypot_driver(client_cold_dvr(593902, ripping_frequency) + 24 | coreLog.joystick(componentUdpLink), windows_expansion_touchscreen); 25 | bashGigabit.external.reality(2, server_hardware_codec.flops.ebookSampling( 26 | ciscNavigationBacklink, table + cleanDriver), indexProtocolIsp); 27 | -------------------------------------------------------------------------------- /content/docs/example/collapsed/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | bookCollapseSection: true 3 | weight: 20 4 | --- 5 | 6 | # Collapsed Level of Menu 7 | 8 | ## Cognita laeva illo fracta 9 | 10 | Lorem markdownum pavent auras, surgit nunc cingentibus libet **Laomedonque que** 11 | est. Pastor [An](http://est.org/ire.aspx) arbor filia foedat, ne [fugit 12 | aliter](http://www.indiciumturbam.org/moramquid.php), per. Helicona illas et 13 | callida neptem est *Oresitrophos* caput, dentibus est venit. Tenet reddite 14 | [famuli](http://www.antro-et.net/) praesentem fortibus, quaeque vis foret si 15 | frondes *gelidos* gravidae circumtulit [inpulit armenta 16 | nativum](http://incurvasustulit.io/illi-virtute.html). 17 | 18 | 1. Te at cruciabere vides rubentis manebo 19 | 2. Maturuit in praetemptat ruborem ignara postquam habitasse 20 | 3. Subitarum supplevit quoque fontesque venabula spretis modo 21 | 4. Montis tot est mali quasque gravis 22 | 5. Quinquennem domus arsit ipse 23 | 6. Pellem turis pugnabant locavit 24 | -------------------------------------------------------------------------------- /content/docs/example/hidden.md: -------------------------------------------------------------------------------- 1 | --- 2 | bookHidden: true 3 | --- 4 | 5 | # This page is hidden in menu 6 | 7 | # Quondam non pater est dignior ille Eurotas 8 | 9 | ## Latent te facies 10 | 11 | Lorem markdownum arma ignoscas vocavit quoque ille texit mandata mentis ultimus, 12 | frementes, qui in vel. Hippotades Peleus [pennas 13 | conscia](http://gratia.net/tot-qua.php) cuiquam Caeneus quas. 14 | 15 | - Pater demittere evincitque reddunt 16 | - Maxime adhuc pressit huc Danaas quid freta 17 | - Soror ego 18 | - Luctus linguam saxa ultroque prior Tatiumque inquit 19 | - Saepe liquitur subita superata dederat Anius sudor 20 | 21 | ## Cum honorum Latona 22 | 23 | O fallor [in sustinui 24 | iussorum](http://www.spectataharundine.org/aquas-relinquit.html) equidem. 25 | Nymphae operi oris alii fronde parens dumque, in auro ait mox ingenti proxima 26 | iamdudum maius? 27 | 28 | reality(burnDocking(apache_nanometer), 29 | pad.property_data_programming.sectorBrowserPpga(dataMask, 37, 30 | recycleRup)); 31 | intellectualVaporwareUser += -5 * 4; 32 | traceroute_key_upnp /= lag_optical(android.smb(thyristorTftp)); 33 | surge_host_golden = mca_compact_device(dual_dpi_opengl, 33, 34 | commerce_add_ppc); 35 | if (lun_ipv) { 36 | verticalExtranet(1, thumbnail_ttl, 3); 37 | bar_graphics_jpeg(chipset - sector_xmp_beta); 38 | } 39 | 40 | ## Fronde cetera dextrae sequens pennis voce muneris 41 | 42 | Acta cretus diem restet utque; move integer, oscula non inspirat, noctisque 43 | scelus! Nantemque in suas vobis quamvis, et labori! 44 | 45 | var runtimeDiskCompiler = home - array_ad_software; 46 | if (internic > disk) { 47 | emoticonLockCron += 37 + bps - 4; 48 | wan_ansi_honeypot.cardGigaflops = artificialStorageCgi; 49 | simplex -= downloadAccess; 50 | } 51 | var volumeHardeningAndroid = pixel + tftp + onProcessorUnmount; 52 | sector(memory(firewire + interlaced, wired)); -------------------------------------------------------------------------------- /content/docs/example/table-of-contents/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | weight: 10 3 | --- 4 | 5 | # Ubi loqui 6 | 7 | ## Mentem genus facietque salire tempus bracchia 8 | 9 | Lorem markdownum partu paterno Achillem. Habent amne generosi aderant ad pellem 10 | nec erat sustinet merces columque haec et, dixit minus nutrit accipiam subibis 11 | subdidit. Temeraria servatum agros qui sed fulva facta. Primum ultima, dedit, 12 | suo quisque linguae medentes fixo: tum petis. 13 | 14 | ## Rapit vocant si hunc siste adspice 15 | 16 | Ora precari Patraeque Neptunia, dixit Danae [Cithaeron 17 | armaque](http://mersis-an.org/litoristum) maxima in **nati Coniugis** templis 18 | fluidove. Effugit usus nec ingreditur agmen *ac manus* conlato. Nullis vagis 19 | nequiquam vultibus aliquos altera *suum venis* teneas fretum. Armos [remotis 20 | hoc](http://tutum.io/me) sine ferrea iuncta quam! 21 | 22 | ## Locus fuit caecis 23 | 24 | Nefas discordemque domino montes numen tum humili nexilibusque exit, Iove. Quae 25 | miror esse, scelerisque Melaneus viribus. Miseri laurus. Hoc est proposita me 26 | ante aliquid, aura inponere candidioribus quidque accendit bella, sumpta. 27 | Intravit quam erat figentem hunc, motus de fontes parvo tempestate. 28 | 29 | iscsi_virus = pitch(json_in_on(eupViral), 30 | northbridge_services_troubleshooting, personal( 31 | firmware_rw.trash_rw_crm.device(interactive_gopher_personal, 32 | software, -1), megabit, ergonomicsSoftware(cmyk_usb_panel, 33 | mips_whitelist_duplex, cpa))); 34 | if (5) { 35 | managementNetwork += dma - boolean; 36 | kilohertz_token = 2; 37 | honeypot_affiliate_ergonomics = fiber; 38 | } 39 | mouseNorthbridge = byte(nybble_xmp_modem.horse_subnet( 40 | analogThroughputService * graphicPoint, drop(daw_bit, dnsIntranet), 41 | gateway_ospf), repository.domain_key.mouse(serverData(fileNetwork, 42 | trim_duplex_file), cellTapeDirect, token_tooltip_mashup( 43 | ripcordingMashup))); 44 | module_it = honeypot_driver(client_cold_dvr(593902, ripping_frequency) + 45 | coreLog.joystick(componentUdpLink), windows_expansion_touchscreen); 46 | bashGigabit.external.reality(2, server_hardware_codec.flops.ebookSampling( 47 | ciscNavigationBacklink, table + cleanDriver), indexProtocolIsp); 48 | 49 | ## Placabilis coactis nega ingemuit ignoscat nimia non 50 | 51 | Frontis turba. Oculi gravis est Delphice; *inque praedaque* sanguine manu non. 52 | 53 | if (ad_api) { 54 | zif += usb.tiffAvatarRate(subnet, digital_rt) + exploitDrive; 55 | gigaflops(2 - bluetooth, edi_asp_memory.gopher(queryCursor, laptop), 56 | panel_point_firmware); 57 | spyware_bash.statePopApplet = express_netbios_digital( 58 | insertion_troubleshooting.brouter(recordFolderUs), 65); 59 | } 60 | recursionCoreRay = -5; 61 | if (hub == non) { 62 | portBoxVirus = soundWeb(recursive_card(rwTechnologyLeopard), 63 | font_radcab, guidCmsScalable + reciprocalMatrixPim); 64 | left.bug = screenshot; 65 | } else { 66 | tooltipOpacity = raw_process_permalink(webcamFontUser, -1); 67 | executable_router += tape; 68 | } 69 | if (tft) { 70 | bandwidthWeb *= social_page; 71 | } else { 72 | regular += 611883; 73 | thumbnail /= system_lag_keyboard; 74 | } 75 | 76 | ## Caesorum illa tu sentit micat vestes papyriferi 77 | 78 | Inde aderam facti; Theseus vis de tauri illa peream. Oculos **uberaque** non 79 | regisque vobis cursuque, opus venit quam vulnera. Et maiora necemque, lege modo; 80 | gestanda nitidi, vero? Dum ne pectoraque testantur. 81 | 82 | Venasque repulsa Samos qui, exspectatum eram animosque hinc, [aut 83 | manes](http://www.creveratnon.net/apricaaetheriis), Assyrii. Cupiens auctoribus 84 | pariter rubet, profana magni super nocens. Vos ius sibilat inpar turba visae 85 | iusto! Sedes ante dum superest **extrema**. 86 | -------------------------------------------------------------------------------- /content/docs/example/table-of-contents/with-toc.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: With ToC 3 | weight: 1 4 | --- 5 | # Caput vino delphine in tamen vias 6 | 7 | ## Cognita laeva illo fracta 8 | 9 | Lorem markdownum pavent auras, surgit nunc cingentibus libet **Laomedonque que** 10 | est. Pastor [An](http://est.org/ire.aspx) arbor filia foedat, ne [fugit 11 | aliter](http://www.indiciumturbam.org/moramquid.php), per. Helicona illas et 12 | callida neptem est *Oresitrophos* caput, dentibus est venit. Tenet reddite 13 | [famuli](http://www.antro-et.net/) praesentem fortibus, quaeque vis foret si 14 | frondes *gelidos* gravidae circumtulit [inpulit armenta 15 | nativum](http://incurvasustulit.io/illi-virtute.html). 16 | 17 | 1. Te at cruciabere vides rubentis manebo 18 | 2. Maturuit in praetemptat ruborem ignara postquam habitasse 19 | 3. Subitarum supplevit quoque fontesque venabula spretis modo 20 | 4. Montis tot est mali quasque gravis 21 | 5. Quinquennem domus arsit ipse 22 | 6. Pellem turis pugnabant locavit 23 | 24 | ## Natus quaerere 25 | 26 | Pectora et sine mulcere, coniuge dum tincta incurvae. Quis iam; est dextra 27 | Peneosque, metuis a verba, primo. Illa sed colloque suis: magno: gramen, aera 28 | excutiunt concipit. 29 | 30 | > Phrygiae petendo suisque extimuit, super, pars quod audet! Turba negarem. 31 | > Fuerat attonitus; et dextra retinet sidera ulnas undas instimulat vacuae 32 | > generis? *Agnus* dabat et ignotis dextera, sic tibi pacis **feriente at mora** 33 | > euhoeque *comites hostem* vestras Phineus. Vultuque sanguine dominoque [metuit 34 | > risi](http://iuvat.org/eundem.php) fama vergit summaque meus clarissimus 35 | > artesque tinguebat successor nominis cervice caelicolae. 36 | 37 | ## Limitibus misere sit 38 | 39 | Aurea non fata repertis praerupit feruntur simul, meae hosti lentaque *citius 40 | levibus*, cum sede dixit, Phaethon texta. *Albentibus summos* multifidasque 41 | iungitur loquendi an pectore, mihi ursaque omnia adfata, aeno parvumque in animi 42 | perlucentes. Epytus agis ait vixque clamat ornum adversam spondet, quid sceptra 43 | ipsum **est**. Reseret nec; saeva suo passu debentia linguam terga et aures et 44 | cervix [de](http://www.amnem.io/pervenit.aspx) ubera. Coercet gelidumque manus, 45 | doluit volvitur induta? 46 | 47 | ## Enim sua 48 | 49 | Iuvenilior filia inlustre templa quidem herbis permittat trahens huic. In 50 | cruribus proceres sole crescitque *fata*, quos quos; merui maris se non tamen 51 | in, mea. 52 | 53 | ## Germana aves pignus tecta 54 | 55 | Mortalia rudibusque caelum cognosceret tantum aquis redito felicior texit, nec, 56 | aris parvo acre. Me parum contulerant multi tenentem, gratissime suis; vultum tu 57 | occupat deficeret corpora, sonum. E Actaea inplevit Phinea concepit nomenque 58 | potest sanguine captam nulla et, in duxisses campis non; mercede. Dicere cur 59 | Leucothoen obitum? 60 | 61 | Postibus mittam est *nubibus principium pluma*, exsecratur facta et. Iunge 62 | Mnemonidas pallamque pars; vere restitit alis flumina quae **quoque**, est 63 | ignara infestus Pyrrha. Di ducis terris maculatum At sede praemia manes 64 | nullaque! 65 | -------------------------------------------------------------------------------- /content/docs/example/table-of-contents/without-toc.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Without ToC 3 | weight: 2 4 | bookToc: false 5 | --- 6 | 7 | # At me ipso nepotibus nunc celebratior genus 8 | 9 | ## Tanto oblite 10 | 11 | Lorem markdownum pectora novis patenti igne sua opus aurae feras materiaque 12 | illic demersit imago et aristas questaque posset. Vomit quoque suo inhaesuro 13 | clara. Esse cumque, per referri triste. Ut exponit solisque communis in tendens 14 | vincetis agisque iamque huic bene ante vetat omina Thebae rates. Aeacus servat 15 | admonitu concidit, ad resimas vultus et rugas vultu **dignamque** Siphnon. 16 | 17 | Quam iugulum regia simulacra, plus meruit humo pecorumque haesit, ab discedunt 18 | dixit: ritu pharetramque. Exul Laurenti orantem modo, per densum missisque labor 19 | manibus non colla unum, obiectat. Tu pervia collo, fessus quae Cretenque Myconon 20 | crate! Tegumenque quae invisi sudore per vocari quaque plus ventis fluidos. Nodo 21 | perque, fugisse pectora sorores. 22 | 23 | ## Summe promissa supple vadit lenius 24 | 25 | Quibus largis latebris aethera versato est, ait sentiat faciemque. Aequata alis 26 | nec Caeneus exululat inclite corpus est, ire **tibi** ostendens et tibi. Rigent 27 | et vires dique possent lumina; **eadem** dixit poma funeribus paret et felix 28 | reddebant ventis utile lignum. 29 | 30 | 1. Remansit notam Stygia feroxque 31 | 2. Et dabit materna 32 | 3. Vipereas Phrygiaeque umbram sollicito cruore conlucere suus 33 | 4. Quarum Elis corniger 34 | 5. Nec ieiunia dixit 35 | 36 | Vertitur mos ortu ramosam contudit dumque; placabat ac lumen. Coniunx Amoris 37 | spatium poenamque cavernis Thebae Pleiadasque ponunt, rapiare cum quae parum 38 | nimium rima. 39 | 40 | ## Quidem resupinus inducto solebat una facinus quae 41 | 42 | Credulitas iniqua praepetibus paruit prospexit, voce poena, sub rupit sinuatur, 43 | quin suum ventorumque arcadiae priori. Soporiferam erat formamque, fecit, 44 | invergens, nymphae mutat fessas ait finge. 45 | 46 | 1. Baculum mandataque ne addere capiti violentior 47 | 2. Altera duas quam hoc ille tenues inquit 48 | 3. Sicula sidereus latrantis domoque ratae polluit comites 49 | 4. Possit oro clausura namque se nunc iuvenisque 50 | 5. Faciem posuit 51 | 6. Quodque cum ponunt novercae nata vestrae aratra 52 | 53 | Ite extrema Phrygiis, patre dentibus, tonso perculit, enim blanda, manibus fide 54 | quos caput armis, posse! Nocendo fas Alcyonae lacertis structa ferarum manus 55 | fulmen dubius, saxa caelum effuge extremis fixum tumor adfecit **bella**, 56 | potentes? Dum nec insidiosa tempora tegit 57 | [spirarunt](http://mihiferre.net/iuvenes-peto.html). Per lupi pars foliis, 58 | porreximus humum negant sunt subposuere Sidone steterant auro. Memoraverit sine: 59 | ferrum idem Orion caelum heres gerebat fixis? 60 | -------------------------------------------------------------------------------- /content/docs/shortcodes/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | bookFlatSection: true 3 | --- 4 | -------------------------------------------------------------------------------- /content/docs/shortcodes/buttons.md: -------------------------------------------------------------------------------- 1 | # Buttons 2 | 3 | Buttons are styled links that can lead to local page or external link. 4 | 5 | ## Example 6 | 7 | ```tpl 8 | {{}}Get Home{{}} 9 | {{}}Contribute{{}} 10 | ``` 11 | 12 | {{< button relref="/" >}}Get Home{{< /button >}} 13 | {{< button href="https://github.com/alex-shpak/hugo-book" >}}Contribute{{< /button >}} 14 | -------------------------------------------------------------------------------- /content/docs/shortcodes/columns.md: -------------------------------------------------------------------------------- 1 | # Columns 2 | 3 | Columns help organize shorter pieces of content horizontally for readability. 4 | 5 | 6 | ```html 7 | {{}} 8 | # Left Content 9 | Lorem markdownum insigne... 10 | 11 | <---> 12 | 13 | # Mid Content 14 | Lorem markdownum insigne... 15 | 16 | <---> 17 | 18 | # Right Content 19 | Lorem markdownum insigne... 20 | {{}} 21 | ``` 22 | 23 | ## Example 24 | 25 | {{< columns >}} 26 | ## Left Content 27 | Lorem markdownum insigne. Olympo signis Delphis! Retexi Nereius nova develat 28 | stringit, frustra Saturnius uteroque inter! Oculis non ritibus Telethusa 29 | protulit, sed sed aere valvis inhaesuro Pallas animam: qui _quid_, ignes. 30 | Miseratus fonte Ditis conubia. 31 | 32 | <---> 33 | 34 | ## Mid Content 35 | Lorem markdownum insigne. Olympo signis Delphis! Retexi Nereius nova develat 36 | stringit, frustra Saturnius uteroque inter! 37 | 38 | <---> 39 | 40 | ## Right Content 41 | Lorem markdownum insigne. Olympo signis Delphis! Retexi Nereius nova develat 42 | stringit, frustra Saturnius uteroque inter! Oculis non ritibus Telethusa 43 | protulit, sed sed aere valvis inhaesuro Pallas animam: qui _quid_, ignes. 44 | Miseratus fonte Ditis conubia. 45 | {{< /columns >}} 46 | -------------------------------------------------------------------------------- /content/docs/shortcodes/details.md: -------------------------------------------------------------------------------- 1 | # Details 2 | 3 | Details shortcode is a helper for `details` html5 element. It is going to replace `expand` shortcode. 4 | 5 | ## Example 6 | ```tpl 7 | {{}} 8 | ## Markdown content 9 | Lorem markdownum insigne... 10 | {{}} 11 | ``` 12 | ```tpl 13 | {{}} 14 | ## Markdown content 15 | Lorem markdownum insigne... 16 | {{}} 17 | ``` 18 | 19 | {{< details "Title" open >}} 20 | ## Markdown content 21 | Lorem markdownum insigne... 22 | {{< /details >}} 23 | -------------------------------------------------------------------------------- /content/docs/shortcodes/expand.md: -------------------------------------------------------------------------------- 1 | # Expand 2 | 3 | Expand shortcode can help to decrease clutter on screen by hiding part of text. Expand content by clicking on it. 4 | 5 | ## Example 6 | ### Default 7 | 8 | ```tpl 9 | {{}} 10 | ## Markdown content 11 | Lorem markdownum insigne... 12 | {{}} 13 | ``` 14 | 15 | {{< expand >}} 16 | ## Markdown content 17 | Lorem markdownum insigne... 18 | {{< /expand >}} 19 | 20 | ### With Custom Label 21 | 22 | ```tpl 23 | {{}} 24 | ## Markdown content 25 | Lorem markdownum insigne... 26 | {{}} 27 | ``` 28 | 29 | {{< expand "Custom Label" "..." >}} 30 | ## Markdown content 31 | Lorem markdownum insigne. Olympo signis Delphis! Retexi Nereius nova develat 32 | stringit, frustra Saturnius uteroque inter! Oculis non ritibus Telethusa 33 | protulit, sed sed aere valvis inhaesuro Pallas animam: qui _quid_, ignes. 34 | Miseratus fonte Ditis conubia. 35 | {{< /expand >}} 36 | -------------------------------------------------------------------------------- /content/docs/shortcodes/hints.md: -------------------------------------------------------------------------------- 1 | # Hints 2 | 3 | Hint shortcode can be used as hint/alerts/notification block. 4 | There are 3 colors to choose: `info`, `warning` and `danger`. 5 | 6 | ```tpl 7 | {{}} 8 | **Markdown content** 9 | Lorem markdownum insigne. Olympo signis Delphis! Retexi Nereius nova develat 10 | stringit, frustra Saturnius uteroque inter! Oculis non ritibus Telethusa 11 | {{}} 12 | ``` 13 | 14 | ## Example 15 | 16 | {{< hint info >}} 17 | **Markdown content** 18 | Lorem markdownum insigne. Olympo signis Delphis! Retexi Nereius nova develat 19 | stringit, frustra Saturnius uteroque inter! Oculis non ritibus Telethusa 20 | {{< /hint >}} 21 | 22 | {{< hint warning >}} 23 | **Markdown content** 24 | Lorem markdownum insigne. Olympo signis Delphis! Retexi Nereius nova develat 25 | stringit, frustra Saturnius uteroque inter! Oculis non ritibus Telethusa 26 | {{< /hint >}} 27 | 28 | {{< hint danger >}} 29 | **Markdown content** 30 | Lorem markdownum insigne. Olympo signis Delphis! Retexi Nereius nova develat 31 | stringit, frustra Saturnius uteroque inter! Oculis non ritibus Telethusa 32 | {{< /hint >}} 33 | -------------------------------------------------------------------------------- /content/docs/shortcodes/katex.md: -------------------------------------------------------------------------------- 1 | # KaTeX 2 | 3 | KaTeX shortcode let you render math typesetting in markdown document. See [KaTeX](https://katex.org/) 4 | 5 | ## Example 6 | {{< columns >}} 7 | 8 | ```latex 9 | {{}} 10 | f(x) = \int_{-\infty}^\infty\hat f(\xi)\,e^{2 \pi i \xi x}\,d\xi 11 | {{}} 12 | ``` 13 | 14 | <---> 15 | 16 | {{< katex display >}} 17 | f(x) = \int_{-\infty}^\infty\hat f(\xi)\,e^{2 \pi i \xi x}\,d\xi 18 | {{< /katex >}} 19 | 20 | {{< /columns >}} 21 | 22 | ## Display Mode Example 23 | 24 | Here is some inline example: {{< katex >}}\pi(x){{< /katex >}}, rendered in the same line. And below is `display` example, having `display: block` 25 | {{< katex display >}} 26 | f(x) = \int_{-\infty}^\infty\hat f(\xi)\,e^{2 \pi i \xi x}\,d\xi 27 | {{< /katex >}} 28 | Text continues here. 29 | -------------------------------------------------------------------------------- /content/docs/shortcodes/mermaid.md: -------------------------------------------------------------------------------- 1 | # Mermaid Chart 2 | 3 | [Mermaid](https://mermaidjs.github.io/) is library for generating svg charts and diagrams from text. 4 | 5 | ## Example 6 | 7 | {{< columns >}} 8 | ```tpl 9 | {{}} 10 | sequenceDiagram 11 | Alice->>Bob: Hello Bob, how are you? 12 | alt is sick 13 | Bob->>Alice: Not so good :( 14 | else is well 15 | Bob->>Alice: Feeling fresh like a daisy 16 | end 17 | opt Extra response 18 | Bob->>Alice: Thanks for asking 19 | end 20 | {{}} 21 | ``` 22 | 23 | <---> 24 | 25 | {{< mermaid >}} 26 | sequenceDiagram 27 | Alice->>Bob: Hello Bob, how are you? 28 | alt is sick 29 | Bob->>Alice: Not so good :( 30 | else is well 31 | Bob->>Alice: Feeling fresh like a daisy 32 | end 33 | opt Extra response 34 | Bob->>Alice: Thanks for asking 35 | end 36 | {{< /mermaid >}} 37 | 38 | {{< /columns >}} 39 | -------------------------------------------------------------------------------- /content/docs/shortcodes/tabs.md: -------------------------------------------------------------------------------- 1 | # Tabs 2 | 3 | Tabs let you organize content by context, for example installation instructions for each supported platform. 4 | 5 | ```tpl 6 | {{}} 7 | {{}} # MacOS Content {{}} 8 | {{}} # Linux Content {{}} 9 | {{}} # Windows Content {{}} 10 | {{}} 11 | ``` 12 | 13 | ## Example 14 | 15 | {{< tabs "uniqueid" >}} 16 | {{< tab "MacOS" >}} 17 | # MacOS 18 | 19 | This is tab **MacOS** content. 20 | 21 | Lorem markdownum insigne. Olympo signis Delphis! Retexi Nereius nova develat 22 | stringit, frustra Saturnius uteroque inter! Oculis non ritibus Telethusa 23 | protulit, sed sed aere valvis inhaesuro Pallas animam: qui _quid_, ignes. 24 | Miseratus fonte Ditis conubia. 25 | {{< /tab >}} 26 | 27 | {{< tab "Linux" >}} 28 | 29 | # Linux 30 | 31 | This is tab **Linux** content. 32 | 33 | Lorem markdownum insigne. Olympo signis Delphis! Retexi Nereius nova develat 34 | stringit, frustra Saturnius uteroque inter! Oculis non ritibus Telethusa 35 | protulit, sed sed aere valvis inhaesuro Pallas animam: qui _quid_, ignes. 36 | Miseratus fonte Ditis conubia. 37 | {{< /tab >}} 38 | 39 | {{< tab "Windows" >}} 40 | 41 | # Windows 42 | 43 | This is tab **Windows** content. 44 | 45 | Lorem markdownum insigne. Olympo signis Delphis! Retexi Nereius nova develat 46 | stringit, frustra Saturnius uteroque inter! Oculis non ritibus Telethusa 47 | protulit, sed sed aere valvis inhaesuro Pallas animam: qui _quid_, ignes. 48 | Miseratus fonte Ditis conubia. 49 | {{< /tab >}} 50 | {{< /tabs >}} 51 | -------------------------------------------------------------------------------- /content/menu/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | headless: true 3 | --- 4 | 5 | - [**Example Site**]({{< relref "/docs/example" >}}) 6 | - [Table of Contents]({{< relref "/docs/example/table-of-contents" >}}) 7 | - [With ToC]({{< relref "/docs/example/table-of-contents/with-toc" >}}) 8 | - [Without ToC]({{< relref "/docs/example/table-of-contents/without-toc" >}}) 9 | - [Collapsed]({{< relref "/docs/example/collapsed" >}}) 10 | - [3rd]({{< relref "/docs/example/collapsed/3rd-level" >}}) 11 | - [4th]({{< relref "/docs/example/collapsed/3rd-level/4th-level" >}}) 12 |
13 | 14 | - **Shortcodes** 15 | - [Buttons]({{< relref "/docs/shortcodes/buttons" >}}) 16 | - [Columns]({{< relref "/docs/shortcodes/columns" >}}) 17 | - [Expand]({{< relref "/docs/shortcodes/expand" >}}) 18 | - [Hints]({{< relref "/docs/shortcodes/hints" >}}) 19 | - [Katex]({{< relref "/docs/shortcodes/katex" >}}) 20 | - [Mermaid]({{< relref "/docs/shortcodes/mermaid" >}}) 21 | - [Tabs]({{< relref "/docs/shortcodes/tabs" >}}) 22 |
23 | -------------------------------------------------------------------------------- /content/posts/_index.md: -------------------------------------------------------------------------------- 1 | --- 2 | menu: 3 | after: 4 | name: blog 5 | weight: 5 6 | title: Blog 7 | --- 8 | -------------------------------------------------------------------------------- /content/posts/creating-a-new-theme.md: -------------------------------------------------------------------------------- 1 | --- 2 | author: "Michael Henderson" 3 | date: 2014-09-28 4 | linktitle: Creating a New Theme 5 | menu: 6 | main: 7 | parent: tutorials 8 | next: /tutorials/github-pages-blog 9 | prev: /tutorials/automated-deployments 10 | title: Creating a New Theme 11 | weight: 10 12 | --- 13 | 14 | 15 | ## Introduction 16 | 17 | This tutorial will show you how to create a simple theme in Hugo. I assume that you are familiar with HTML, the bash command line, and that you are comfortable using Markdown to format content. I'll explain how Hugo uses templates and how you can organize your templates to create a theme. I won't cover using CSS to style your theme. 18 | 19 | We'll start with creating a new site with a very basic template. Then we'll add in a few pages and posts. With small variations on that, you will be able to create many different types of web sites. 20 | 21 | In this tutorial, commands that you enter will start with the "$" prompt. The output will follow. Lines that start with "#" are comments that I've added to explain a point. When I show updates to a file, the ":wq" on the last line means to save the file. 22 | 23 | Here's an example: 24 | 25 | ``` 26 | ## this is a comment 27 | $ echo this is a command 28 | this is a command 29 | 30 | ## edit the file 31 | $ vi foo.md 32 | +++ 33 | date = "2014-09-28" 34 | title = "creating a new theme" 35 | +++ 36 | 37 | bah and humbug 38 | :wq 39 | 40 | ## show it 41 | $ cat foo.md 42 | +++ 43 | date = "2014-09-28" 44 | title = "creating a new theme" 45 | +++ 46 | 47 | bah and humbug 48 | $ 49 | ``` 50 | 51 | 52 | ## Some Definitions 53 | 54 | There are a few concepts that you need to understand before creating a theme. 55 | 56 | ### Skins 57 | 58 | Skins are the files responsible for the look and feel of your site. It’s the CSS that controls colors and fonts, it’s the Javascript that determines actions and reactions. It’s also the rules that Hugo uses to transform your content into the HTML that the site will serve to visitors. 59 | 60 | You have two ways to create a skin. The simplest way is to create it in the ```layouts/``` directory. If you do, then you don’t have to worry about configuring Hugo to recognize it. The first place that Hugo will look for rules and files is in the ```layouts/``` directory so it will always find the skin. 61 | 62 | Your second choice is to create it in a sub-directory of the ```themes/``` directory. If you do, then you must always tell Hugo where to search for the skin. It’s extra work, though, so why bother with it? 63 | 64 | The difference between creating a skin in ```layouts/``` and creating it in ```themes/``` is very subtle. A skin in ```layouts/``` can’t be customized without updating the templates and static files that it is built from. A skin created in ```themes/```, on the other hand, can be and that makes it easier for other people to use it. 65 | 66 | The rest of this tutorial will call a skin created in the ```themes/``` directory a theme. 67 | 68 | Note that you can use this tutorial to create a skin in the ```layouts/``` directory if you wish to. The main difference will be that you won’t need to update the site’s configuration file to use a theme. 69 | 70 | ### The Home Page 71 | 72 | The home page, or landing page, is the first page that many visitors to a site see. It is the index.html file in the root directory of the web site. Since Hugo writes files to the public/ directory, our home page is public/index.html. 73 | 74 | ### Site Configuration File 75 | 76 | When Hugo runs, it looks for a configuration file that contains settings that override default values for the entire site. The file can use TOML, YAML, or JSON. I prefer to use TOML for my configuration files. If you prefer to use JSON or YAML, you’ll need to translate my examples. You’ll also need to change the name of the file since Hugo uses the extension to determine how to process it. 77 | 78 | Hugo translates Markdown files into HTML. By default, Hugo expects to find Markdown files in your ```content/``` directory and template files in your ```themes/``` directory. It will create HTML files in your ```public/``` directory. You can change this by specifying alternate locations in the configuration file. 79 | 80 | ### Content 81 | 82 | Content is stored in text files that contain two sections. The first section is the “front matter,” which is the meta-information on the content. The second section contains Markdown that will be converted to HTML. 83 | 84 | #### Front Matter 85 | 86 | The front matter is information about the content. Like the configuration file, it can be written in TOML, YAML, or JSON. Unlike the configuration file, Hugo doesn’t use the file’s extension to know the format. It looks for markers to signal the type. TOML is surrounded by “`+++`”, YAML by “`---`”, and JSON is enclosed in curly braces. I prefer to use TOML, so you’ll need to translate my examples if you prefer YAML or JSON. 87 | 88 | The information in the front matter is passed into the template before the content is rendered into HTML. 89 | 90 | #### Markdown 91 | 92 | Content is written in Markdown which makes it easier to create the content. Hugo runs the content through a Markdown engine to create the HTML which will be written to the output file. 93 | 94 | ### Template Files 95 | 96 | Hugo uses template files to render content into HTML. Template files are a bridge between the content and presentation. Rules in the template define what content is published, where it's published to, and how it will rendered to the HTML file. The template guides the presentation by specifying the style to use. 97 | 98 | There are three types of templates: single, list, and partial. Each type takes a bit of content as input and transforms it based on the commands in the template. 99 | 100 | Hugo uses its knowledge of the content to find the template file used to render the content. If it can’t find a template that is an exact match for the content, it will shift up a level and search from there. It will continue to do so until it finds a matching template or runs out of templates to try. If it can’t find a template, it will use the default template for the site. 101 | 102 | Please note that you can use the front matter to influence Hugo’s choice of templates. 103 | 104 | #### Single Template 105 | 106 | A single template is used to render a single piece of content. For example, an article or post would be a single piece of content and use a single template. 107 | 108 | #### List Template 109 | 110 | A list template renders a group of related content. That could be a summary of recent postings or all articles in a category. List templates can contain multiple groups. 111 | 112 | The homepage template is a special type of list template. Hugo assumes that the home page of your site will act as the portal for the rest of the content in the site. 113 | 114 | #### Partial Template 115 | 116 | A partial template is a template that can be included in other templates. Partial templates must be called using the “partial” template command. They are very handy for rolling up common behavior. For example, your site may have a banner that all pages use. Instead of copying the text of the banner into every single and list template, you could create a partial with the banner in it. That way if you decide to change the banner, you only have to change the partial template. 117 | 118 | ## Create a New Site 119 | 120 | Let's use Hugo to create a new web site. I'm a Mac user, so I'll create mine in my home directory, in the Sites folder. If you're using Linux, you might have to create the folder first. 121 | 122 | The "new site" command will create a skeleton of a site. It will give you the basic directory structure and a useable configuration file. 123 | 124 | ``` 125 | $ hugo new site ~/Sites/zafta 126 | $ cd ~/Sites/zafta 127 | $ ls -l 128 | total 8 129 | drwxr-xr-x 7 quoha staff 238 Sep 29 16:49 . 130 | drwxr-xr-x 3 quoha staff 102 Sep 29 16:49 .. 131 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 archetypes 132 | -rw-r--r-- 1 quoha staff 82 Sep 29 16:49 config.toml 133 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 content 134 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 layouts 135 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 static 136 | $ 137 | ``` 138 | 139 | Take a look in the content/ directory to confirm that it is empty. 140 | 141 | The other directories (archetypes/, layouts/, and static/) are used when customizing a theme. That's a topic for a different tutorial, so please ignore them for now. 142 | 143 | ### Generate the HTML For the New Site 144 | 145 | Running the `hugo` command with no options will read all the available content and generate the HTML files. It will also copy all static files (that's everything that's not content). Since we have an empty site, it won't do much, but it will do it very quickly. 146 | 147 | ``` 148 | $ hugo --verbose 149 | INFO: 2014/09/29 Using config file: config.toml 150 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 151 | WARN: 2014/09/29 Unable to locate layout: [index.html _default/list.html _default/single.html] 152 | WARN: 2014/09/29 Unable to locate layout: [404.html] 153 | 0 draft content 154 | 0 future content 155 | 0 pages created 156 | 0 tags created 157 | 0 categories created 158 | in 2 ms 159 | $ 160 | ``` 161 | 162 | The "`--verbose`" flag gives extra information that will be helpful when we build the template. Every line of the output that starts with "INFO:" or "WARN:" is present because we used that flag. The lines that start with "WARN:" are warning messages. We'll go over them later. 163 | 164 | We can verify that the command worked by looking at the directory again. 165 | 166 | ``` 167 | $ ls -l 168 | total 8 169 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 archetypes 170 | -rw-r--r-- 1 quoha staff 82 Sep 29 16:49 config.toml 171 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 content 172 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 layouts 173 | drwxr-xr-x 4 quoha staff 136 Sep 29 17:02 public 174 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 static 175 | $ 176 | ``` 177 | 178 | See that new public/ directory? Hugo placed all generated content there. When you're ready to publish your web site, that's the place to start. For now, though, let's just confirm that we have what we'd expect from a site with no content. 179 | 180 | ``` 181 | $ ls -l public 182 | total 16 183 | -rw-r--r-- 1 quoha staff 416 Sep 29 17:02 index.xml 184 | -rw-r--r-- 1 quoha staff 262 Sep 29 17:02 sitemap.xml 185 | $ 186 | ``` 187 | 188 | Hugo created two XML files, which is standard, but there are no HTML files. 189 | 190 | 191 | 192 | ### Test the New Site 193 | 194 | Verify that you can run the built-in web server. It will dramatically shorten your development cycle if you do. Start it by running the "server" command. If it is successful, you will see output similar to the following: 195 | 196 | ``` 197 | $ hugo server --verbose 198 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 199 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 200 | WARN: 2014/09/29 Unable to locate layout: [index.html _default/list.html _default/single.html] 201 | WARN: 2014/09/29 Unable to locate layout: [404.html] 202 | 0 draft content 203 | 0 future content 204 | 0 pages created 205 | 0 tags created 206 | 0 categories created 207 | in 2 ms 208 | Serving pages from /Users/quoha/Sites/zafta/public 209 | Web Server is available at http://localhost:1313 210 | Press Ctrl+C to stop 211 | ``` 212 | 213 | Connect to the listed URL (it's on the line that starts with "Web Server"). If everything is working correctly, you should get a page that shows the following: 214 | 215 | ``` 216 | index.xml 217 | sitemap.xml 218 | ``` 219 | 220 | That's a listing of your public/ directory. Hugo didn't create a home page because our site has no content. When there's no index.html file in a directory, the server lists the files in the directory, which is what you should see in your browser. 221 | 222 | Let’s go back and look at those warnings again. 223 | 224 | ``` 225 | WARN: 2014/09/29 Unable to locate layout: [index.html _default/list.html _default/single.html] 226 | WARN: 2014/09/29 Unable to locate layout: [404.html] 227 | ``` 228 | 229 | That second warning is easier to explain. We haven’t created a template to be used to generate “page not found errors.” The 404 message is a topic for a separate tutorial. 230 | 231 | Now for the first warning. It is for the home page. You can tell because the first layout that it looked for was “index.html.” That’s only used by the home page. 232 | 233 | I like that the verbose flag causes Hugo to list the files that it's searching for. For the home page, they are index.html, _default/list.html, and _default/single.html. There are some rules that we'll cover later that explain the names and paths. For now, just remember that Hugo couldn't find a template for the home page and it told you so. 234 | 235 | At this point, you've got a working installation and site that we can build upon. All that’s left is to add some content and a theme to display it. 236 | 237 | ## Create a New Theme 238 | 239 | Hugo doesn't ship with a default theme. There are a few available (I counted a dozen when I first installed Hugo) and Hugo comes with a command to create new themes. 240 | 241 | We're going to create a new theme called "zafta." Since the goal of this tutorial is to show you how to fill out the files to pull in your content, the theme will not contain any CSS. In other words, ugly but functional. 242 | 243 | All themes have opinions on content and layout. For example, Zafta uses "post" over "blog". Strong opinions make for simpler templates but differing opinions make it tougher to use themes. When you build a theme, consider using the terms that other themes do. 244 | 245 | 246 | ### Create a Skeleton 247 | 248 | Use the hugo "new" command to create the skeleton of a theme. This creates the directory structure and places empty files for you to fill out. 249 | 250 | ``` 251 | $ hugo new theme zafta 252 | 253 | $ ls -l 254 | total 8 255 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 archetypes 256 | -rw-r--r-- 1 quoha staff 82 Sep 29 16:49 config.toml 257 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 content 258 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 layouts 259 | drwxr-xr-x 4 quoha staff 136 Sep 29 17:02 public 260 | drwxr-xr-x 2 quoha staff 68 Sep 29 16:49 static 261 | drwxr-xr-x 3 quoha staff 102 Sep 29 17:31 themes 262 | 263 | $ find themes -type f | xargs ls -l 264 | -rw-r--r-- 1 quoha staff 1081 Sep 29 17:31 themes/zafta/LICENSE.md 265 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/archetypes/default.md 266 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/list.html 267 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/single.html 268 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/index.html 269 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/partials/footer.html 270 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/partials/header.html 271 | -rw-r--r-- 1 quoha staff 93 Sep 29 17:31 themes/zafta/theme.toml 272 | $ 273 | ``` 274 | 275 | The skeleton includes templates (the files ending in .html), license file, a description of your theme (the theme.toml file), and an empty archetype. 276 | 277 | Please take a minute to fill out the theme.toml and LICENSE.md files. They're optional, but if you're going to be distributing your theme, it tells the world who to praise (or blame). It's also nice to declare the license so that people will know how they can use the theme. 278 | 279 | ``` 280 | $ vi themes/zafta/theme.toml 281 | author = "michael d henderson" 282 | description = "a minimal working template" 283 | license = "MIT" 284 | name = "zafta" 285 | source_repo = "" 286 | tags = ["tags", "categories"] 287 | :wq 288 | 289 | ## also edit themes/zafta/LICENSE.md and change 290 | ## the bit that says "YOUR_NAME_HERE" 291 | ``` 292 | 293 | Note that the the skeleton's template files are empty. Don't worry, we'll be changing that shortly. 294 | 295 | ``` 296 | $ find themes/zafta -name '*.html' | xargs ls -l 297 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/list.html 298 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/single.html 299 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/index.html 300 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/partials/footer.html 301 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/partials/header.html 302 | $ 303 | ``` 304 | 305 | 306 | 307 | ### Update the Configuration File to Use the Theme 308 | 309 | Now that we've got a theme to work with, it's a good idea to add the theme name to the configuration file. This is optional, because you can always add "-t zafta" on all your commands. I like to put it the configuration file because I like shorter command lines. If you don't put it in the configuration file or specify it on the command line, you won't use the template that you're expecting to. 310 | 311 | Edit the file to add the theme, add a title for the site, and specify that all of our content will use the TOML format. 312 | 313 | ``` 314 | $ vi config.toml 315 | theme = "zafta" 316 | baseurl = "" 317 | languageCode = "en-us" 318 | title = "zafta - totally refreshing" 319 | MetaDataFormat = "toml" 320 | :wq 321 | 322 | $ 323 | ``` 324 | 325 | ### Generate the Site 326 | 327 | Now that we have an empty theme, let's generate the site again. 328 | 329 | ``` 330 | $ hugo --verbose 331 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 332 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 333 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 334 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 335 | 0 draft content 336 | 0 future content 337 | 0 pages created 338 | 0 tags created 339 | 0 categories created 340 | in 2 ms 341 | $ 342 | ``` 343 | 344 | Did you notice that the output is different? The warning message for the home page has disappeared and we have an additional information line saying that Hugo is syncing from the theme's directory. 345 | 346 | Let's check the public/ directory to see what Hugo's created. 347 | 348 | ``` 349 | $ ls -l public 350 | total 16 351 | drwxr-xr-x 2 quoha staff 68 Sep 29 17:56 css 352 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:56 index.html 353 | -rw-r--r-- 1 quoha staff 407 Sep 29 17:56 index.xml 354 | drwxr-xr-x 2 quoha staff 68 Sep 29 17:56 js 355 | -rw-r--r-- 1 quoha staff 243 Sep 29 17:56 sitemap.xml 356 | $ 357 | ``` 358 | 359 | Notice four things: 360 | 361 | 1. Hugo created a home page. This is the file public/index.html. 362 | 2. Hugo created a css/ directory. 363 | 3. Hugo created a js/ directory. 364 | 4. Hugo claimed that it created 0 pages. It created a file and copied over static files, but didn't create any pages. That's because it considers a "page" to be a file created directly from a content file. It doesn't count things like the index.html files that it creates automatically. 365 | 366 | #### The Home Page 367 | 368 | Hugo supports many different types of templates. The home page is special because it gets its own type of template and its own template file. The file, layouts/index.html, is used to generate the HTML for the home page. The Hugo documentation says that this is the only required template, but that depends. Hugo's warning message shows that it looks for three different templates: 369 | 370 | ``` 371 | WARN: 2014/09/29 Unable to locate layout: [index.html _default/list.html _default/single.html] 372 | ``` 373 | 374 | If it can't find any of these, it completely skips creating the home page. We noticed that when we built the site without having a theme installed. 375 | 376 | When Hugo created our theme, it created an empty home page template. Now, when we build the site, Hugo finds the template and uses it to generate the HTML for the home page. Since the template file is empty, the HTML file is empty, too. If the template had any rules in it, then Hugo would have used them to generate the home page. 377 | 378 | ``` 379 | $ find . -name index.html | xargs ls -l 380 | -rw-r--r-- 1 quoha staff 0 Sep 29 20:21 ./public/index.html 381 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 ./themes/zafta/layouts/index.html 382 | $ 383 | ``` 384 | 385 | #### The Magic of Static 386 | 387 | Hugo does two things when generating the site. It uses templates to transform content into HTML and it copies static files into the site. Unlike content, static files are not transformed. They are copied exactly as they are. 388 | 389 | Hugo assumes that your site will use both CSS and JavaScript, so it creates directories in your theme to hold them. Remember opinions? Well, Hugo's opinion is that you'll store your CSS in a directory named css/ and your JavaScript in a directory named js/. If you don't like that, you can change the directory names in your theme directory or even delete them completely. Hugo's nice enough to offer its opinion, then behave nicely if you disagree. 390 | 391 | ``` 392 | $ find themes/zafta -type d | xargs ls -ld 393 | drwxr-xr-x 7 quoha staff 238 Sep 29 17:38 themes/zafta 394 | drwxr-xr-x 3 quoha staff 102 Sep 29 17:31 themes/zafta/archetypes 395 | drwxr-xr-x 5 quoha staff 170 Sep 29 17:31 themes/zafta/layouts 396 | drwxr-xr-x 4 quoha staff 136 Sep 29 17:31 themes/zafta/layouts/_default 397 | drwxr-xr-x 4 quoha staff 136 Sep 29 17:31 themes/zafta/layouts/partials 398 | drwxr-xr-x 4 quoha staff 136 Sep 29 17:31 themes/zafta/static 399 | drwxr-xr-x 2 quoha staff 68 Sep 29 17:31 themes/zafta/static/css 400 | drwxr-xr-x 2 quoha staff 68 Sep 29 17:31 themes/zafta/static/js 401 | $ 402 | ``` 403 | 404 | ## The Theme Development Cycle 405 | 406 | When you're working on a theme, you will make changes in the theme's directory, rebuild the site, and check your changes in the browser. Hugo makes this very easy: 407 | 408 | 1. Purge the public/ directory. 409 | 2. Run the built in web server in watch mode. 410 | 3. Open your site in a browser. 411 | 4. Update the theme. 412 | 5. Glance at your browser window to see changes. 413 | 6. Return to step 4. 414 | 415 | I’ll throw in one more opinion: never work on a theme on a live site. Always work on a copy of your site. Make changes to your theme, test them, then copy them up to your site. For added safety, use a tool like Git to keep a revision history of your content and your theme. Believe me when I say that it is too easy to lose both your mind and your changes. 416 | 417 | Check the main Hugo site for information on using Git with Hugo. 418 | 419 | ### Purge the public/ Directory 420 | 421 | When generating the site, Hugo will create new files and update existing ones in the ```public/``` directory. It will not delete files that are no longer used. For example, files that were created in the wrong directory or with the wrong title will remain. If you leave them, you might get confused by them later. I recommend cleaning out your site prior to generating it. 422 | 423 | Note: If you're building on an SSD, you should ignore this. Churning on a SSD can be costly. 424 | 425 | ### Hugo's Watch Option 426 | 427 | Hugo's "`--watch`" option will monitor the content/ and your theme directories for changes and rebuild the site automatically. 428 | 429 | ### Live Reload 430 | 431 | Hugo's built in web server supports live reload. As pages are saved on the server, the browser is told to refresh the page. Usually, this happens faster than you can say, "Wow, that's totally amazing." 432 | 433 | ### Development Commands 434 | 435 | Use the following commands as the basis for your workflow. 436 | 437 | ``` 438 | ## purge old files. hugo will recreate the public directory. 439 | ## 440 | $ rm -rf public 441 | ## 442 | ## run hugo in watch mode 443 | ## 444 | $ hugo server --watch --verbose 445 | ``` 446 | 447 | Here's sample output showing Hugo detecting a change to the template for the home page. Once generated, the web browser automatically reloaded the page. I've said this before, it's amazing. 448 | 449 | 450 | ``` 451 | $ rm -rf public 452 | $ hugo server --watch --verbose 453 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 454 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 455 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 456 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 457 | 0 draft content 458 | 0 future content 459 | 0 pages created 460 | 0 tags created 461 | 0 categories created 462 | in 2 ms 463 | Watching for changes in /Users/quoha/Sites/zafta/content 464 | Serving pages from /Users/quoha/Sites/zafta/public 465 | Web Server is available at http://localhost:1313 466 | Press Ctrl+C to stop 467 | INFO: 2014/09/29 File System Event: ["/Users/quoha/Sites/zafta/themes/zafta/layouts/index.html": MODIFY|ATTRIB] 468 | Change detected, rebuilding site 469 | 470 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 471 | 0 draft content 472 | 0 future content 473 | 0 pages created 474 | 0 tags created 475 | 0 categories created 476 | in 1 ms 477 | ``` 478 | 479 | ## Update the Home Page Template 480 | 481 | The home page is one of a few special pages that Hugo creates automatically. As mentioned earlier, it looks for one of three files in the theme's layout/ directory: 482 | 483 | 1. index.html 484 | 2. _default/list.html 485 | 3. _default/single.html 486 | 487 | We could update one of the default templates, but a good design decision is to update the most specific template available. That's not a hard and fast rule (in fact, we'll break it a few times in this tutorial), but it is a good generalization. 488 | 489 | ### Make a Static Home Page 490 | 491 | Right now, that page is empty because we don't have any content and we don't have any logic in the template. Let's change that by adding some text to the template. 492 | 493 | ``` 494 | $ vi themes/zafta/layouts/index.html 495 | 496 | 497 | 498 |

hugo says hello!

499 | 500 | 501 | :wq 502 | 503 | $ 504 | ``` 505 | 506 | Build the web site and then verify the results. 507 | 508 | ``` 509 | $ hugo --verbose 510 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 511 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 512 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 513 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 514 | 0 draft content 515 | 0 future content 516 | 0 pages created 517 | 0 tags created 518 | 0 categories created 519 | in 2 ms 520 | 521 | $ find public -type f -name '*.html' | xargs ls -l 522 | -rw-r--r-- 1 quoha staff 78 Sep 29 21:26 public/index.html 523 | 524 | $ cat public/index.html 525 | 526 | 527 | 528 |

hugo says hello!

529 | 530 | ``` 531 | 532 | #### Live Reload 533 | 534 | Note: If you're running the server with the `--watch` option, you'll see different content in the file: 535 | 536 | ``` 537 | $ cat public/index.html 538 | 539 | 540 | 541 |

hugo says hello!

542 | 546 | 547 | ``` 548 | 549 | When you use `--watch`, the Live Reload script is added by Hugo. Look for live reload in the documentation to see what it does and how to disable it. 550 | 551 | ### Build a "Dynamic" Home Page 552 | 553 | "Dynamic home page?" Hugo's a static web site generator, so this seems an odd thing to say. I mean let's have the home page automatically reflect the content in the site every time Hugo builds it. We'll use iteration in the template to do that. 554 | 555 | #### Create New Posts 556 | 557 | Now that we have the home page generating static content, let's add some content to the site. We'll display these posts as a list on the home page and on their own page, too. 558 | 559 | Hugo has a command to generate a skeleton post, just like it does for sites and themes. 560 | 561 | ``` 562 | $ hugo --verbose new post/first.md 563 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 564 | INFO: 2014/09/29 attempting to create post/first.md of post 565 | INFO: 2014/09/29 curpath: /Users/quoha/Sites/zafta/themes/zafta/archetypes/default.md 566 | ERROR: 2014/09/29 Unable to Cast to map[string]interface{} 567 | 568 | $ 569 | ``` 570 | 571 | That wasn't very nice, was it? 572 | 573 | The "new" command uses an archetype to create the post file. Hugo created an empty default archetype file, but that causes an error when there's a theme. For me, the workaround was to create an archetypes file specifically for the post type. 574 | 575 | ``` 576 | $ vi themes/zafta/archetypes/post.md 577 | +++ 578 | Description = "" 579 | Tags = [] 580 | Categories = [] 581 | +++ 582 | :wq 583 | 584 | $ find themes/zafta/archetypes -type f | xargs ls -l 585 | -rw-r--r-- 1 quoha staff 0 Sep 29 21:53 themes/zafta/archetypes/default.md 586 | -rw-r--r-- 1 quoha staff 51 Sep 29 21:54 themes/zafta/archetypes/post.md 587 | 588 | $ hugo --verbose new post/first.md 589 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 590 | INFO: 2014/09/29 attempting to create post/first.md of post 591 | INFO: 2014/09/29 curpath: /Users/quoha/Sites/zafta/themes/zafta/archetypes/post.md 592 | INFO: 2014/09/29 creating /Users/quoha/Sites/zafta/content/post/first.md 593 | /Users/quoha/Sites/zafta/content/post/first.md created 594 | 595 | $ hugo --verbose new post/second.md 596 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 597 | INFO: 2014/09/29 attempting to create post/second.md of post 598 | INFO: 2014/09/29 curpath: /Users/quoha/Sites/zafta/themes/zafta/archetypes/post.md 599 | INFO: 2014/09/29 creating /Users/quoha/Sites/zafta/content/post/second.md 600 | /Users/quoha/Sites/zafta/content/post/second.md created 601 | 602 | $ ls -l content/post 603 | total 16 604 | -rw-r--r-- 1 quoha staff 104 Sep 29 21:54 first.md 605 | -rw-r--r-- 1 quoha staff 105 Sep 29 21:57 second.md 606 | 607 | $ cat content/post/first.md 608 | +++ 609 | Categories = [] 610 | Description = "" 611 | Tags = [] 612 | date = "2014-09-29T21:54:53-05:00" 613 | title = "first" 614 | 615 | +++ 616 | my first post 617 | 618 | $ cat content/post/second.md 619 | +++ 620 | Categories = [] 621 | Description = "" 622 | Tags = [] 623 | date = "2014-09-29T21:57:09-05:00" 624 | title = "second" 625 | 626 | +++ 627 | my second post 628 | 629 | $ 630 | ``` 631 | 632 | Build the web site and then verify the results. 633 | 634 | ``` 635 | $ rm -rf public 636 | $ hugo --verbose 637 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 638 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 639 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 640 | INFO: 2014/09/29 found taxonomies: map[string]string{"category":"categories", "tag":"tags"} 641 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 642 | 0 draft content 643 | 0 future content 644 | 2 pages created 645 | 0 tags created 646 | 0 categories created 647 | in 4 ms 648 | $ 649 | ``` 650 | 651 | The output says that it created 2 pages. Those are our new posts: 652 | 653 | ``` 654 | $ find public -type f -name '*.html' | xargs ls -l 655 | -rw-r--r-- 1 quoha staff 78 Sep 29 22:13 public/index.html 656 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:13 public/post/first/index.html 657 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:13 public/post/index.html 658 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:13 public/post/second/index.html 659 | $ 660 | ``` 661 | 662 | The new files are empty because because the templates used to generate the content are empty. The homepage doesn't show the new content, either. We have to update the templates to add the posts. 663 | 664 | ### List and Single Templates 665 | 666 | In Hugo, we have three major kinds of templates. There's the home page template that we updated previously. It is used only by the home page. We also have "single" templates which are used to generate output for a single content file. We also have "list" templates that are used to group multiple pieces of content before generating output. 667 | 668 | Generally speaking, list templates are named "list.html" and single templates are named "single.html." 669 | 670 | There are three other types of templates: partials, content views, and terms. We will not go into much detail on these. 671 | 672 | ### Add Content to the Homepage 673 | 674 | The home page will contain a list of posts. Let's update its template to add the posts that we just created. The logic in the template will run every time we build the site. 675 | 676 | ``` 677 | $ vi themes/zafta/layouts/index.html 678 | 679 | 680 | 681 | {{ range first 10 .Data.Pages }} 682 |

{{ .Title }}

683 | {{ end }} 684 | 685 | 686 | :wq 687 | 688 | $ 689 | ``` 690 | 691 | Hugo uses the Go template engine. That engine scans the template files for commands which are enclosed between "{{" and "}}". In our template, the commands are: 692 | 693 | 1. range 694 | 2. .Title 695 | 3. end 696 | 697 | The "range" command is an iterator. We're going to use it to go through the first ten pages. Every HTML file that Hugo creates is treated as a page, so looping through the list of pages will look at every file that will be created. 698 | 699 | The ".Title" command prints the value of the "title" variable. Hugo pulls it from the front matter in the Markdown file. 700 | 701 | The "end" command signals the end of the range iterator. The engine loops back to the top of the iteration when it finds "end." Everything between the "range" and "end" is evaluated every time the engine goes through the iteration. In this file, that would cause the title from the first ten pages to be output as heading level one. 702 | 703 | It's helpful to remember that some variables, like .Data, are created before any output files. Hugo loads every content file into the variable and then gives the template a chance to process before creating the HTML files. 704 | 705 | Build the web site and then verify the results. 706 | 707 | ``` 708 | $ rm -rf public 709 | $ hugo --verbose 710 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 711 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 712 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 713 | INFO: 2014/09/29 found taxonomies: map[string]string{"tag":"tags", "category":"categories"} 714 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 715 | 0 draft content 716 | 0 future content 717 | 2 pages created 718 | 0 tags created 719 | 0 categories created 720 | in 4 ms 721 | $ find public -type f -name '*.html' | xargs ls -l 722 | -rw-r--r-- 1 quoha staff 94 Sep 29 22:23 public/index.html 723 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:23 public/post/first/index.html 724 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:23 public/post/index.html 725 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:23 public/post/second/index.html 726 | $ cat public/index.html 727 | 728 | 729 | 730 | 731 |

second

732 | 733 |

first

734 | 735 | 736 | 737 | $ 738 | ``` 739 | 740 | Congratulations, the home page shows the title of the two posts. The posts themselves are still empty, but let's take a moment to appreciate what we've done. Your template now generates output dynamically. Believe it or not, by inserting the range command inside of those curly braces, you've learned everything you need to know to build a theme. All that's really left is understanding which template will be used to generate each content file and becoming familiar with the commands for the template engine. 741 | 742 | And, if that were entirely true, this tutorial would be much shorter. There are a few things to know that will make creating a new template much easier. Don't worry, though, that's all to come. 743 | 744 | ### Add Content to the Posts 745 | 746 | We're working with posts, which are in the content/post/ directory. That means that their section is "post" (and if we don't do something weird, their type is also "post"). 747 | 748 | Hugo uses the section and type to find the template file for every piece of content. Hugo will first look for a template file that matches the section or type name. If it can't find one, then it will look in the _default/ directory. There are some twists that we'll cover when we get to categories and tags, but for now we can assume that Hugo will try post/single.html, then _default/single.html. 749 | 750 | Now that we know the search rule, let's see what we actually have available: 751 | 752 | ``` 753 | $ find themes/zafta -name single.html | xargs ls -l 754 | -rw-r--r-- 1 quoha staff 132 Sep 29 17:31 themes/zafta/layouts/_default/single.html 755 | ``` 756 | 757 | We could create a new template, post/single.html, or change the default. Since we don't know of any other content types, let's start with updating the default. 758 | 759 | Remember, any content that we haven't created a template for will end up using this template. That can be good or bad. Bad because I know that we're going to be adding different types of content and we're going to end up undoing some of the changes we've made. It's good because we'll be able to see immediate results. It's also good to start here because we can start to build the basic layout for the site. As we add more content types, we'll refactor this file and move logic around. Hugo makes that fairly painless, so we'll accept the cost and proceed. 760 | 761 | Please see the Hugo documentation on template rendering for all the details on determining which template to use. And, as the docs mention, if you're building a single page application (SPA) web site, you can delete all of the other templates and work with just the default single page. That's a refreshing amount of joy right there. 762 | 763 | #### Update the Template File 764 | 765 | ``` 766 | $ vi themes/zafta/layouts/_default/single.html 767 | 768 | 769 | 770 | {{ .Title }} 771 | 772 | 773 |

{{ .Title }}

774 | {{ .Content }} 775 | 776 | 777 | :wq 778 | 779 | $ 780 | ``` 781 | 782 | Build the web site and verify the results. 783 | 784 | ``` 785 | $ rm -rf public 786 | $ hugo --verbose 787 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 788 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 789 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 790 | INFO: 2014/09/29 found taxonomies: map[string]string{"tag":"tags", "category":"categories"} 791 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 792 | 0 draft content 793 | 0 future content 794 | 2 pages created 795 | 0 tags created 796 | 0 categories created 797 | in 4 ms 798 | 799 | $ find public -type f -name '*.html' | xargs ls -l 800 | -rw-r--r-- 1 quoha staff 94 Sep 29 22:40 public/index.html 801 | -rw-r--r-- 1 quoha staff 125 Sep 29 22:40 public/post/first/index.html 802 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:40 public/post/index.html 803 | -rw-r--r-- 1 quoha staff 128 Sep 29 22:40 public/post/second/index.html 804 | 805 | $ cat public/post/first/index.html 806 | 807 | 808 | 809 | first 810 | 811 | 812 |

first

813 |

my first post

814 | 815 | 816 | 817 | 818 | $ cat public/post/second/index.html 819 | 820 | 821 | 822 | second 823 | 824 | 825 |

second

826 |

my second post

827 | 828 | 829 | 830 | $ 831 | ``` 832 | 833 | Notice that the posts now have content. You can go to localhost:1313/post/first to verify. 834 | 835 | ### Linking to Content 836 | 837 | The posts are on the home page. Let's add a link from there to the post. Since this is the home page, we'll update its template. 838 | 839 | ``` 840 | $ vi themes/zafta/layouts/index.html 841 | 842 | 843 | 844 | {{ range first 10 .Data.Pages }} 845 |

{{ .Title }}

846 | {{ end }} 847 | 848 | 849 | ``` 850 | 851 | Build the web site and verify the results. 852 | 853 | ``` 854 | $ rm -rf public 855 | $ hugo --verbose 856 | INFO: 2014/09/29 Using config file: /Users/quoha/Sites/zafta/config.toml 857 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/themes/zafta/static/ to /Users/quoha/Sites/zafta/public/ 858 | INFO: 2014/09/29 syncing from /Users/quoha/Sites/zafta/static/ to /Users/quoha/Sites/zafta/public/ 859 | INFO: 2014/09/29 found taxonomies: map[string]string{"tag":"tags", "category":"categories"} 860 | WARN: 2014/09/29 Unable to locate layout: [404.html theme/404.html] 861 | 0 draft content 862 | 0 future content 863 | 2 pages created 864 | 0 tags created 865 | 0 categories created 866 | in 4 ms 867 | 868 | $ find public -type f -name '*.html' | xargs ls -l 869 | -rw-r--r-- 1 quoha staff 149 Sep 29 22:44 public/index.html 870 | -rw-r--r-- 1 quoha staff 125 Sep 29 22:44 public/post/first/index.html 871 | -rw-r--r-- 1 quoha staff 0 Sep 29 22:44 public/post/index.html 872 | -rw-r--r-- 1 quoha staff 128 Sep 29 22:44 public/post/second/index.html 873 | 874 | $ cat public/index.html 875 | 876 | 877 | 878 | 879 |

second

880 | 881 |

first

882 | 883 | 884 | 885 | 886 | $ 887 | ``` 888 | 889 | ### Create a Post Listing 890 | 891 | We have the posts displaying on the home page and on their own page. We also have a file public/post/index.html that is empty. Let's make it show a list of all posts (not just the first ten). 892 | 893 | We need to decide which template to update. This will be a listing, so it should be a list template. Let's take a quick look and see which list templates are available. 894 | 895 | ``` 896 | $ find themes/zafta -name list.html | xargs ls -l 897 | -rw-r--r-- 1 quoha staff 0 Sep 29 17:31 themes/zafta/layouts/_default/list.html 898 | ``` 899 | 900 | As with the single post, we have to decide to update _default/list.html or create post/list.html. We still don't have multiple content types, so let's stay consistent and update the default list template. 901 | 902 | ## Creating Top Level Pages 903 | 904 | Let's add an "about" page and display it at the top level (as opposed to a sub-level like we did with posts). 905 | 906 | The default in Hugo is to use the directory structure of the content/ directory to guide the location of the generated html in the public/ directory. Let's verify that by creating an "about" page at the top level: 907 | 908 | ``` 909 | $ vi content/about.md 910 | +++ 911 | title = "about" 912 | description = "about this site" 913 | date = "2014-09-27" 914 | slug = "about time" 915 | +++ 916 | 917 | ## about us 918 | 919 | i'm speechless 920 | :wq 921 | ``` 922 | 923 | Generate the web site and verify the results. 924 | 925 | ``` 926 | $ find public -name '*.html' | xargs ls -l 927 | -rw-rw-r-- 1 mdhender staff 334 Sep 27 15:08 public/about-time/index.html 928 | -rw-rw-r-- 1 mdhender staff 527 Sep 27 15:08 public/index.html 929 | -rw-rw-r-- 1 mdhender staff 358 Sep 27 15:08 public/post/first-post/index.html 930 | -rw-rw-r-- 1 mdhender staff 0 Sep 27 15:08 public/post/index.html 931 | -rw-rw-r-- 1 mdhender staff 342 Sep 27 15:08 public/post/second-post/index.html 932 | ``` 933 | 934 | Notice that the page wasn't created at the top level. It was created in a sub-directory named 'about-time/'. That name came from our slug. Hugo will use the slug to name the generated content. It's a reasonable default, by the way, but we can learn a few things by fighting it for this file. 935 | 936 | One other thing. Take a look at the home page. 937 | 938 | ``` 939 | $ cat public/index.html 940 | 941 | 942 | 943 |

creating a new theme

944 |

about

945 |

second

946 |

first

947 | 951 | 952 | ``` 953 | 954 | Notice that the "about" link is listed with the posts? That's not desirable, so let's change that first. 955 | 956 | ``` 957 | $ vi themes/zafta/layouts/index.html 958 | 959 | 960 | 961 |

posts

962 | {{ range first 10 .Data.Pages }} 963 | {{ if eq .Type "post"}} 964 |

{{ .Title }}

965 | {{ end }} 966 | {{ end }} 967 | 968 |

pages

969 | {{ range .Data.Pages }} 970 | {{ if eq .Type "page" }} 971 |

{{ .Title }}

972 | {{ end }} 973 | {{ end }} 974 | 975 | 976 | :wq 977 | ``` 978 | 979 | Generate the web site and verify the results. The home page has two sections, posts and pages, and each section has the right set of headings and links in it. 980 | 981 | But, that about page still renders to about-time/index.html. 982 | 983 | ``` 984 | $ find public -name '*.html' | xargs ls -l 985 | -rw-rw-r-- 1 mdhender staff 334 Sep 27 15:33 public/about-time/index.html 986 | -rw-rw-r-- 1 mdhender staff 645 Sep 27 15:33 public/index.html 987 | -rw-rw-r-- 1 mdhender staff 358 Sep 27 15:33 public/post/first-post/index.html 988 | -rw-rw-r-- 1 mdhender staff 0 Sep 27 15:33 public/post/index.html 989 | -rw-rw-r-- 1 mdhender staff 342 Sep 27 15:33 public/post/second-post/index.html 990 | ``` 991 | 992 | Knowing that hugo is using the slug to generate the file name, the simplest solution is to change the slug. Let's do it the hard way and change the permalink in the configuration file. 993 | 994 | ``` 995 | $ vi config.toml 996 | [permalinks] 997 | page = "/:title/" 998 | about = "/:filename/" 999 | ``` 1000 | 1001 | Generate the web site and verify that this didn't work. Hugo lets "slug" or "URL" override the permalinks setting in the configuration file. Go ahead and comment out the slug in content/about.md, then generate the web site to get it to be created in the right place. 1002 | 1003 | ## Sharing Templates 1004 | 1005 | If you've been following along, you probably noticed that posts have titles in the browser and the home page doesn't. That's because we didn't put the title in the home page's template (layouts/index.html). That's an easy thing to do, but let's look at a different option. 1006 | 1007 | We can put the common bits into a shared template that's stored in the themes/zafta/layouts/partials/ directory. 1008 | 1009 | ### Create the Header and Footer Partials 1010 | 1011 | In Hugo, a partial is a sugar-coated template. Normally a template reference has a path specified. Partials are different. Hugo searches for them along a TODO defined search path. This makes it easier for end-users to override the theme's presentation. 1012 | 1013 | ``` 1014 | $ vi themes/zafta/layouts/partials/header.html 1015 | 1016 | 1017 | 1018 | {{ .Title }} 1019 | 1020 | 1021 | :wq 1022 | 1023 | $ vi themes/zafta/layouts/partials/footer.html 1024 | 1025 | 1026 | :wq 1027 | ``` 1028 | 1029 | ### Update the Home Page Template to Use the Partials 1030 | 1031 | The most noticeable difference between a template call and a partials call is the lack of path: 1032 | 1033 | ``` 1034 | {{ template "theme/partials/header.html" . }} 1035 | ``` 1036 | versus 1037 | ``` 1038 | {{ partial "header.html" . }} 1039 | ``` 1040 | Both pass in the context. 1041 | 1042 | Let's change the home page template to use these new partials. 1043 | 1044 | ``` 1045 | $ vi themes/zafta/layouts/index.html 1046 | {{ partial "header.html" . }} 1047 | 1048 |

posts

1049 | {{ range first 10 .Data.Pages }} 1050 | {{ if eq .Type "post"}} 1051 |

{{ .Title }}

1052 | {{ end }} 1053 | {{ end }} 1054 | 1055 |

pages

1056 | {{ range .Data.Pages }} 1057 | {{ if or (eq .Type "page") (eq .Type "about") }} 1058 |

{{ .Type }} - {{ .Title }} - {{ .RelPermalink }}

1059 | {{ end }} 1060 | {{ end }} 1061 | 1062 | {{ partial "footer.html" . }} 1063 | :wq 1064 | ``` 1065 | 1066 | Generate the web site and verify the results. The title on the home page is now "your title here", which comes from the "title" variable in the config.toml file. 1067 | 1068 | ### Update the Default Single Template to Use the Partials 1069 | 1070 | ``` 1071 | $ vi themes/zafta/layouts/_default/single.html 1072 | {{ partial "header.html" . }} 1073 | 1074 |

{{ .Title }}

1075 | {{ .Content }} 1076 | 1077 | {{ partial "footer.html" . }} 1078 | :wq 1079 | ``` 1080 | 1081 | Generate the web site and verify the results. The title on the posts and the about page should both reflect the value in the markdown file. 1082 | 1083 | ## Add “Date Published” to Posts 1084 | 1085 | It's common to have posts display the date that they were written or published, so let's add that. The front matter of our posts has a variable named "date." It's usually the date the content was created, but let's pretend that's the value we want to display. 1086 | 1087 | ### Add “Date Published” to the Template 1088 | 1089 | We'll start by updating the template used to render the posts. The template code will look like: 1090 | 1091 | ``` 1092 | {{ .Date.Format "Mon, Jan 2, 2006" }} 1093 | ``` 1094 | 1095 | Posts use the default single template, so we'll change that file. 1096 | 1097 | ``` 1098 | $ vi themes/zafta/layouts/_default/single.html 1099 | {{ partial "header.html" . }} 1100 | 1101 |

{{ .Title }}

1102 |

{{ .Date.Format "Mon, Jan 2, 2006" }}

1103 | {{ .Content }} 1104 | 1105 | {{ partial "footer.html" . }} 1106 | :wq 1107 | ``` 1108 | 1109 | Generate the web site and verify the results. The posts now have the date displayed in them. There's a problem, though. The "about" page also has the date displayed. 1110 | 1111 | As usual, there are a couple of ways to make the date display only on posts. We could do an "if" statement like we did on the home page. Another way would be to create a separate template for posts. 1112 | 1113 | The "if" solution works for sites that have just a couple of content types. It aligns with the principle of "code for today," too. 1114 | 1115 | Let's assume, though, that we've made our site so complex that we feel we have to create a new template type. In Hugo-speak, we're going to create a section template. 1116 | 1117 | Let's restore the default single template before we forget. 1118 | 1119 | ``` 1120 | $ mkdir themes/zafta/layouts/post 1121 | $ vi themes/zafta/layouts/_default/single.html 1122 | {{ partial "header.html" . }} 1123 | 1124 |

{{ .Title }}

1125 | {{ .Content }} 1126 | 1127 | {{ partial "footer.html" . }} 1128 | :wq 1129 | ``` 1130 | 1131 | Now we'll update the post's version of the single template. If you remember Hugo's rules, the template engine will use this version over the default. 1132 | 1133 | ``` 1134 | $ vi themes/zafta/layouts/post/single.html 1135 | {{ partial "header.html" . }} 1136 | 1137 |

{{ .Title }}

1138 |

{{ .Date.Format "Mon, Jan 2, 2006" }}

1139 | {{ .Content }} 1140 | 1141 | {{ partial "footer.html" . }} 1142 | :wq 1143 | 1144 | ``` 1145 | 1146 | Note that we removed the date logic from the default template and put it in the post template. Generate the web site and verify the results. Posts have dates and the about page doesn't. 1147 | 1148 | ### Don't Repeat Yourself 1149 | 1150 | DRY is a good design goal and Hugo does a great job supporting it. Part of the art of a good template is knowing when to add a new template and when to update an existing one. While you're figuring that out, accept that you'll be doing some refactoring. Hugo makes that easy and fast, so it's okay to delay splitting up a template. 1151 | -------------------------------------------------------------------------------- /content/posts/goisforlovers.md: -------------------------------------------------------------------------------- 1 | +++ 2 | title = "(Hu)go Template Primer" 3 | description = "" 4 | tags = [ 5 | "go", 6 | "golang", 7 | "templates", 8 | "themes", 9 | "development", 10 | ] 11 | date = "2014-04-02" 12 | categories = [ 13 | "Development", 14 | "golang", 15 | ] 16 | menu = "main" 17 | +++ 18 | 19 | Hugo uses the excellent [Go][] [html/template][gohtmltemplate] library for 20 | its template engine. It is an extremely lightweight engine that provides a very 21 | small amount of logic. In our experience that it is just the right amount of 22 | logic to be able to create a good static website. If you have used other 23 | template systems from different languages or frameworks you will find a lot of 24 | similarities in Go templates. 25 | 26 | This document is a brief primer on using Go templates. The [Go docs][gohtmltemplate] 27 | provide more details. 28 | 29 | ## Introduction to Go Templates 30 | 31 | Go templates provide an extremely simple template language. It adheres to the 32 | belief that only the most basic of logic belongs in the template or view layer. 33 | One consequence of this simplicity is that Go templates parse very quickly. 34 | 35 | A unique characteristic of Go templates is they are content aware. Variables and 36 | content will be sanitized depending on the context of where they are used. More 37 | details can be found in the [Go docs][gohtmltemplate]. 38 | 39 | ## Basic Syntax 40 | 41 | Golang templates are HTML files with the addition of variables and 42 | functions. 43 | 44 | **Go variables and functions are accessible within {{ }}** 45 | 46 | Accessing a predefined variable "foo": 47 | 48 | {{ foo }} 49 | 50 | **Parameters are separated using spaces** 51 | 52 | Calling the add function with input of 1, 2: 53 | 54 | {{ add 1 2 }} 55 | 56 | **Methods and fields are accessed via dot notation** 57 | 58 | Accessing the Page Parameter "bar" 59 | 60 | {{ .Params.bar }} 61 | 62 | **Parentheses can be used to group items together** 63 | 64 | {{ if or (isset .Params "alt") (isset .Params "caption") }} Caption {{ end }} 65 | 66 | 67 | ## Variables 68 | 69 | Each Go template has a struct (object) made available to it. In hugo each 70 | template is passed either a page or a node struct depending on which type of 71 | page you are rendering. More details are available on the 72 | [variables](/layout/variables) page. 73 | 74 | A variable is accessed by referencing the variable name. 75 | 76 | {{ .Title }} 77 | 78 | Variables can also be defined and referenced. 79 | 80 | {{ $address := "123 Main St."}} 81 | {{ $address }} 82 | 83 | 84 | ## Functions 85 | 86 | Go template ship with a few functions which provide basic functionality. The Go 87 | template system also provides a mechanism for applications to extend the 88 | available functions with their own. [Hugo template 89 | functions](/layout/functions) provide some additional functionality we believe 90 | are useful for building websites. Functions are called by using their name 91 | followed by the required parameters separated by spaces. Template 92 | functions cannot be added without recompiling hugo. 93 | 94 | **Example:** 95 | 96 | {{ add 1 2 }} 97 | 98 | ## Includes 99 | 100 | When including another template you will pass to it the data it will be 101 | able to access. To pass along the current context please remember to 102 | include a trailing dot. The templates location will always be starting at 103 | the /layout/ directory within Hugo. 104 | 105 | **Example:** 106 | 107 | {{ template "chrome/header.html" . }} 108 | 109 | 110 | ## Logic 111 | 112 | Go templates provide the most basic iteration and conditional logic. 113 | 114 | ### Iteration 115 | 116 | Just like in Go, the Go templates make heavy use of range to iterate over 117 | a map, array or slice. The following are different examples of how to use 118 | range. 119 | 120 | **Example 1: Using Context** 121 | 122 | {{ range array }} 123 | {{ . }} 124 | {{ end }} 125 | 126 | **Example 2: Declaring value variable name** 127 | 128 | {{range $element := array}} 129 | {{ $element }} 130 | {{ end }} 131 | 132 | **Example 2: Declaring key and value variable name** 133 | 134 | {{range $index, $element := array}} 135 | {{ $index }} 136 | {{ $element }} 137 | {{ end }} 138 | 139 | ### Conditionals 140 | 141 | If, else, with, or, & and provide the framework for handling conditional 142 | logic in Go Templates. Like range, each statement is closed with `end`. 143 | 144 | 145 | Go Templates treat the following values as false: 146 | 147 | * false 148 | * 0 149 | * any array, slice, map, or string of length zero 150 | 151 | **Example 1: If** 152 | 153 | {{ if isset .Params "title" }}

{{ index .Params "title" }}

{{ end }} 154 | 155 | **Example 2: If -> Else** 156 | 157 | {{ if isset .Params "alt" }} 158 | {{ index .Params "alt" }} 159 | {{else}} 160 | {{ index .Params "caption" }} 161 | {{ end }} 162 | 163 | **Example 3: And & Or** 164 | 165 | {{ if and (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}} 166 | 167 | **Example 4: With** 168 | 169 | An alternative way of writing "if" and then referencing the same value 170 | is to use "with" instead. With rebinds the context `.` within its scope, 171 | and skips the block if the variable is absent. 172 | 173 | The first example above could be simplified as: 174 | 175 | {{ with .Params.title }}

{{ . }}

{{ end }} 176 | 177 | **Example 5: If -> Else If** 178 | 179 | {{ if isset .Params "alt" }} 180 | {{ index .Params "alt" }} 181 | {{ else if isset .Params "caption" }} 182 | {{ index .Params "caption" }} 183 | {{ end }} 184 | 185 | ## Pipes 186 | 187 | One of the most powerful components of Go templates is the ability to 188 | stack actions one after another. This is done by using pipes. Borrowed 189 | from unix pipes, the concept is simple, each pipeline's output becomes the 190 | input of the following pipe. 191 | 192 | Because of the very simple syntax of Go templates, the pipe is essential 193 | to being able to chain together function calls. One limitation of the 194 | pipes is that they only can work with a single value and that value 195 | becomes the last parameter of the next pipeline. 196 | 197 | A few simple examples should help convey how to use the pipe. 198 | 199 | **Example 1 :** 200 | 201 | {{ if eq 1 1 }} Same {{ end }} 202 | 203 | is the same as 204 | 205 | {{ eq 1 1 | if }} Same {{ end }} 206 | 207 | It does look odd to place the if at the end, but it does provide a good 208 | illustration of how to use the pipes. 209 | 210 | **Example 2 :** 211 | 212 | {{ index .Params "disqus_url" | html }} 213 | 214 | Access the page parameter called "disqus_url" and escape the HTML. 215 | 216 | **Example 3 :** 217 | 218 | {{ if or (or (isset .Params "title") (isset .Params "caption")) (isset .Params "attr")}} 219 | Stuff Here 220 | {{ end }} 221 | 222 | Could be rewritten as 223 | 224 | {{ isset .Params "caption" | or isset .Params "title" | or isset .Params "attr" | if }} 225 | Stuff Here 226 | {{ end }} 227 | 228 | 229 | ## Context (aka. the dot) 230 | 231 | The most easily overlooked concept to understand about Go templates is that {{ . }} 232 | always refers to the current context. In the top level of your template this 233 | will be the data set made available to it. Inside of a iteration it will have 234 | the value of the current item. When inside of a loop the context has changed. . 235 | will no longer refer to the data available to the entire page. If you need to 236 | access this from within the loop you will likely want to set it to a variable 237 | instead of depending on the context. 238 | 239 | **Example:** 240 | 241 | {{ $title := .Site.Title }} 242 | {{ range .Params.tags }} 243 |
  • {{ . }} - {{ $title }}
  • 244 | {{ end }} 245 | 246 | Notice how once we have entered the loop the value of {{ . }} has changed. We 247 | have defined a variable outside of the loop so we have access to it from within 248 | the loop. 249 | 250 | # Hugo Parameters 251 | 252 | Hugo provides the option of passing values to the template language 253 | through the site configuration (for sitewide values), or through the meta 254 | data of each specific piece of content. You can define any values of any 255 | type (supported by your front matter/config format) and use them however 256 | you want to inside of your templates. 257 | 258 | 259 | ## Using Content (page) Parameters 260 | 261 | In each piece of content you can provide variables to be used by the 262 | templates. This happens in the [front matter](/content/front-matter). 263 | 264 | An example of this is used in this documentation site. Most of the pages 265 | benefit from having the table of contents provided. Sometimes the TOC just 266 | doesn't make a lot of sense. We've defined a variable in our front matter 267 | of some pages to turn off the TOC from being displayed. 268 | 269 | Here is the example front matter: 270 | 271 | ``` 272 | --- 273 | title: "Permalinks" 274 | date: "2013-11-18" 275 | aliases: 276 | - "/doc/permalinks/" 277 | groups: ["extras"] 278 | groups_weight: 30 279 | notoc: true 280 | --- 281 | ``` 282 | 283 | Here is the corresponding code inside of the template: 284 | 285 | {{ if not .Params.notoc }} 286 |
    287 | {{ .TableOfContents }} 288 |
    289 | {{ end }} 290 | 291 | 292 | 293 | ## Using Site (config) Parameters 294 | In your top-level configuration file (eg, `config.yaml`) you can define site 295 | parameters, which are values which will be available to you in chrome. 296 | 297 | For instance, you might declare: 298 | 299 | ```yaml 300 | params: 301 | CopyrightHTML: "Copyright © 2013 John Doe. All Rights Reserved." 302 | TwitterUser: "spf13" 303 | SidebarRecentLimit: 5 304 | ``` 305 | 306 | Within a footer layout, you might then declare a `