Hugo vs Jekyll: en episk kamp om temaer for statisk stedgenerator

I denne artikel sammenligner vi nuancerne ved at oprette temaer til de to øverste statiske stedgeneratorer.

For nylig påtog jeg mig opgaven med at oprette et dokumentationssite-tema til to projekter. Begge projekter havde brug for de samme grundlæggende funktioner, men den ene bruger Jekyll, mens den anden bruger Hugo.

I typisk udviklerrationalitet var der tydeligvis kun en mulighed. Jeg besluttede at skabe det samme tema i begge rammer og give dig, kære læser, en sammenligning side om side.

Dette indlæg er ikke en omfattende guide til temabygning, men er snarere beregnet til at gøre dig bekendt med processen med at opbygge et tema i begge generatorer. Her er hvad vi dækker:

  • Hvordan temafiler er organiseret
  • Hvor skal du placere indhold
  • Sådan fungerer skabeloner
  • Oprettelse af en menu på øverste niveau med pagesobjektet
  • Oprettelse af en menu med indlejrede links fra en dataliste
  • Sætte skabelonen sammen
  • Oprettelse af stilarter
  • Sådan konfigureres og implementeres til GitHub Pages

Her er en skør trådramme af det tema, jeg vil oprette.

Hvis du planlægger at bygge sammen, kan det være nyttigt at tjene temaet lokalt, mens du bygger det - og begge generatorer tilbyder denne funktionalitet. For Jekyll, køre jekyll serve, og for Hugo, hugo serve.

Der er to hovedelementer: hovedindholdsområdet og den meget vigtige sidebarmenu. For at oprette dem skal du bruge skabelonfiler, der fortæller stedgeneratoren, hvordan man genererer HTML-siden. For at organisere temaskabelonfiler på en fornuftig måde skal du først vide, hvilken biblioteksstruktur webstedsgeneratoren forventer.

Hvordan temafiler er organiseret

Jekyll understøtter perlebaserede temaer, som brugerne kan installere som alle andre Ruby-perler. Denne metode skjuler temafiler i perlen, så i forbindelse med denne sammenligning bruger vi ikke perlebaserede temaer.

Når du løber jekyll new-theme , stiller Jekyll et nyt tema til dig. Sådan ser disse filer ud:

. ├── assets ├── Gemfile ├── _includes ├── _layouts │   ├── default.html │   ├── page.html │   └── post.html ├── LICENSE.txt ├── README.md ├── _sass └── .gemspec 

Katalognavne er passende beskrivende. Den _includesmappe er for små stumper af kode, som du genbruge i forskellige steder, på samme måde, som du ville sætte smør på alt. (Kun mig?)

Den _layoutsmappe indeholder skabeloner til forskellige typer af sider på dit websted. Den _sassmappe er for Sass filer, der bruges til at bygge dit websted stylesheet.

Du kan stilladsere et nyt Hugo-tema ved at køre hugo new theme . Det har disse filer:

. ├── archetypes │   └── default.md ├── layouts │   ├── 404.html │   ├── _default │   │   ├── baseof.html │   │   ├── list.html │   │   └── single.html │   ├── index.html │   └── partials │   ├── footer.html │   ├── header.html │   └── head.html ├── LICENSE ├── static │   ├── css │   └── js └── theme.toml 

Du kan se nogle ligheder. Hugos sideskabelonfiler er gemt i layouts/. Bemærk, at _defaultsidetypen har filer til a list.htmlog a single.html.

I modsætning til Jekyll bruger Hugo disse specifikke filnavne til at skelne mellem lister (som en side med links til alle dine blogindlæg på den) og enkelt sider (som et af dine blogindlæg). Den layouts/partials/mappe indeholder smøragtig genanvendelige bits, og stylesheet filer har en plet plukket ud i static/css/.

Disse bibliotekstrukturer er ikke sat i sten, da begge webstedsgeneratorer tillader en vis tilpasning. For eksempel lader Jekyll dig definere samlinger, og Hugo gør brug af sidebundter. Disse funktioner giver dig mulighed for at organisere dit indhold på flere måder, men lad os nu se på, hvor du kan placere nogle enkle sider.

Hvor skal du placere indhold

Sådan opretter du en webstedsmenu, der ser sådan ud:

Introduction Getting Started Configuration Deploying Advanced Usage All Configuration Settings Customizing Help and Support 

Du skal bruge to sektioner ("Introduktion" og "Avanceret brug"), der indeholder deres respektive underafsnit.

Jekyll er ikke streng med sin placering af indhold. Det forventer sider i roden på dit websted og vil bygge det, der er der. Sådan organiserer du disse sider i din Jekyll-webstedsrod:

. ├── 404.html ├── assets ├── Gemfile ├── _includes ├── index.markdown ├── intro │   ├── config.md │   ├── deploy.md │   ├── index.md │   └── quickstart.md ├── _layouts │   ├── default.html │   ├── page.html │   └── post.html ├── LICENSE.txt ├── README.md ├── _sass ├── .gemspec └── usage ├── customizing.md ├── index.md ├── settings.md └── support.md 

Du kan ændre placeringen af ​​webstedskilden i din Jekyll-konfiguration.

I Hugo forventes alt gengivet indhold i content/mappen. Dette forhindrer Hugo i at forsøge at gengive sider, du ikke ønsker, såsom 404.htmlwebstedsindhold. Sådan organiserer du din content/mappe i Hugo:

. ├── _index.md ├── intro │ ├── config.md │ ├── deploy.md │ ├── _index.md │ └── quickstart.md └── usage ├── customizing.md ├── _index.md ├── settings.md └── support.md 

For Hugo _index.mdog index.mdbetyde forskellige ting. Det kan være nyttigt at vide, hvilken slags sidepakke du vil have for hver sektion: Blad, der ikke har nogen børn, eller gren.

Nu hvor du har en ide om, hvor du skal placere ting, lad os se på, hvordan du bygger en sideskabelon.

Sådan fungerer skabeloner

Jekyll-sideskabeloner er bygget med Liquid-skabelonsproget. Det bruger seler til output variabel indhold til en side, som sidens titel: {{ page.title }}.

Hugos skabeloner bruger også seler, men de er bygget med Go-skabeloner. Syntaksen er den samme, men anderledes: {{ .Title }}.

Både Liquid og Go-skabeloner kan håndtere logik. Flydende anvendelser tags syntaks til at betegne logiske operationer:

{% if user %} Hello {{ user.name }}! {% endif %} 

Og Go-skabeloner placerer sine funktioner og argumenter i dens seler-syntaks:

{{ if .User }} Hello {{ .User }}! {{ end }} 

Skabelonsprog giver dig mulighed for at oprette en skelet-HTML-side og derefter fortælle stedgeneratoren at placere variabelt indhold i områder, du definerer. Lad os sammenligne to mulige defaultsideskabeloner til Jekyll og Hugo.

Jekyll’s scaffold default theme is bare, so we’ll look at their starter theme Minima. Here’s _layouts/default.html in Jekyll (Liquid):

  {%- include head.html -%}  {%- include header.html -%} {{ content }} {%- include footer.html -%}   

Here’s Hugo’s scaffold theme layouts/_default/baseof.html (Go Templates):

  {{- partial "head.html" . -}}  {{- partial "header.html" . -}} {{- block "main" . }}{{- end }} {{- partial "footer.html" . -}}   

Different syntax, same idea. Both templates pull in reusable bits for head.html, header.html, and footer.html.  These show up on a lot of pages, so it makes sense not to have to  repeat yourself.

Both templates also have a spot for the main content, though the Jekyll template uses a variable ({{ content }}) while Hugo uses a block ({{- block "main" . }}{{- end }}). Blocks are just another way Hugo lets you define reusable bits.

Now that you know how templating works, you can build the sidebar menu for the theme.

Creating a top-level menu with the pages object

You can programmatically create a top-level menu from your pages. It will look like this:

Introduction Advanced Usage 

Let’s start with Jekyll. You can display links to site pages in your Liquid template by iterating through the site.pages object that Jekyll provides and building a list:


    
    {% for page in site.pages %}
  • {{ page.title }}
  • {% endfor %}

This returns all of the site’s pages, including all the ones that you might not want, like 404.html. You can filter for the pages you actually want with a couple more tags, such as conditionally including pages if they have a section: true parameter set:


    
    {% for page in site.pages %} {%- if page.section -%}
  • {{ page.title }}
  • {%- endif -%} {% endfor %}

You can achieve the same effect with slightly less code in Hugo. Loop through Hugo’s .Pages object using Go Template’s range action:


    
    {{ range .Pages }}
  • {{.Title}}
  • {{ end }}

This template uses the .Pages object to return all the top-level pages in content/ of your Hugo site. Since Hugo uses a specific folder for the site content you want rendered, there’s no additional filtering necessary to build a simple menu of site pages.

Creating a menu with nested links from a data list

Both site generators can use a separately defined data list of links to render a menu in your template. This is more suitable for creating nested links, like this:

Introduction Getting Started Configuration Deploying Advanced Usage All Configuration Settings Customizing Help and Support 

Jekyll supports data files in a few formats, including YAML. Here’s the definition for the menu above in _data/menu.yml:

section: - page: Introduction url: /intro subsection: - page: Getting Started url: /intro/quickstart - page: Configuration url: /intro/config - page: Deploying url: /intro/deploy - page: Advanced Usage url: /usage subsection: - page: Customizing url: /usage/customizing - page: All Configuration Settings url: /usage/settings - page: Help and Support url: /usage/support 

Here’s how to render the data in the sidebar template:

{% for a in site.data.menu.section %} {{ a.page }} 
    
    {% for b in a.subsection %}
  • {{ b.page }}
  • {% endfor %}
{% endfor %}

This method allows you to build a custom menu, two nesting levels deep. The nesting levels are limited by the for loops in the template. For a recursive version that handles further levels of nesting, see Nested tree navigation with recursion.

Hugo does something similar with its menu templates. You can define menu links in your Hugo site config, and even add useful properties that Hugo understands, like weighting. Here’s a definition of the menu above in config.yaml:

sectionPagesMenu: main menu: main: - identifier: intro name: Introduction url: /intro/ weight: 1 - name: Getting Started parent: intro url: /intro/quickstart/ weight: 1 - name: Configuration parent: intro url: /intro/config/ weight: 2 - name: Deploying parent: intro url: /intro/deploy/ weight: 3 - identifier: usage name: Advanced Usage url: /usage/ - name: Customizing parent: usage url: /usage/customizing/ weight: 2 - name: All Configuration Settings parent: usage url: /usage/settings/ weight: 1 - name: Help and Support parent: usage url: /usage/support/ weight: 3 

Hugo uses the identifier, which must match the section name, along with the parent variable to handle nesting. Here’s how to render the menu in the sidebar template:


    
    {{ range .Site.Menus.main }} {{ if .HasChildren }}
  • {{ .Name }}
    • {{ range .Children }}
    • {{ .Name }}
    • {{ end }}
    {{ else }}
  • {{ .Name }}
  • {{ end }} {{ end }}

The range function iterates over the menu data, and Hugo’s .Children variable handles nested pages for you.

Putting the template together

With your menu in your reusable sidebar bit (_includes/sidebar.html for Jekyll and partials/sidebar.html for Hugo), you can add it to the default.html template.

In Jekyll:

  {%- include head.html -%}  {%- include sidebar.html -%} {%- include header.html -%} {{ content }} {%- include footer.html -%}   

In Hugo:

  {{- partial "head.html" . -}}  {{- partial "sidebar.html" . -}} {{- partial "header.html" . -}} {{- block "main" . }}{{- end }} {{- partial "footer.html" . -}}   

When the site is generated, each page will contain all the code from your sidebar.html.

Create a stylesheet

Both site generators accept Sass for creating CSS stylesheets. Jekyll has Sass processing built in, and Hugo uses Hugo Pipes. Both options have some quirks.

Sass and CSS in Jekyll

To process a Sass file in Jekyll, create your style definitions in the _sass directory. For example, in a file at _sass/style-definitions.scss:

$background-color: #eef !default; $text-color: #111 !default; body { background-color: $background-color; color: $text-color; } 

Jekyll won’t generate this file directly, as it only processes files with front matter. To create the end-result  filepath for your site’s stylesheet, use a placeholder with empty front matter where you want the .css file to appear. For example, assets/css/style.scss. In this file, simply import your styles:

--- --- @import "style-definitions"; 

This rather hackish configuration has an upside: you can use Liquid template tags and variables in your placeholder file. This is a nice way to allow users to set variables from the site _config.yml, for example.

The resulting CSS stylesheet in your generated site has the path /assets/css/style.css. You can link to it in your site’s head.html using:

Sass and Hugo Pipes in Hugo

Hugo uses Hugo Pipes to process Sass to CSS. You can achieve this by using Hugo’s asset processing function, resources.ToCSS, which expects a source in the assets/ directory. It takes the SCSS file as an argument.

With your style definitions in a Sass file at assets/sass/style.scss, here’s how to get, process, and link your Sass in your theme’s head.html:

{ $style := resources.Get "/sass/style.scss" }  

Hugo asset processing requires extended Hugo, which you may not have by default. You can get extended Hugo from the releases page.

Configure and deploy to GitHub Pages

Before your site generator can build your site, it needs a  configuration file to set some necessary parameters. Configuration files  live in the site root directory. Among other settings, you can declare  the name of the theme to use when building the site.

Configure Jekyll

Here’s a minimal _config.yml for Jekyll:

title: Your awesome title description: >- # this means to ignore newlines until "baseurl:" Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description. baseurl: "" # the subpath of your site, e.g. /blog url: "" # the base hostname & protocol for your site, e.g. //example.com theme: # for gem-based themes remote_theme: # for themes hosted on GitHub, when used with GitHub Pages 

With remote_theme, any Jekyll theme hosted on GitHub can be used with sites hosted on GitHub Pages.

Jekyll has a default configuration, so any parameters added to your configuration file will override the defaults. Here are additional configuration settings.

Configure Hugo

Here’s a minimal example of Hugo’s config.yml:

baseURL: //example.com/ # The full domain your site will live at languageCode: en-us title: Hugo Docs Site theme: # theme name 

Hugo makes no assumptions, so if a necessary parameter is missing, you’ll see a warning when building or serving your site. Here are all configuration settings for Hugo.

Deploy to GitHub Pages

Both generators build your site with a command.

For Jekyll, use jekyll build. See further build options here.

For Hugo, use hugo. You can run hugo help or see further build options here.

You’ll have to choose the source for your GitHub Pages site. Once done, your site will update each time you push a new build. Of course, you can also automate your GitHub Pages build using GitHub Actions. Here’s one for building and deploying with Hugo, and one for building and deploying Jekyll.

Showtime!

All the substantial differences between these two generators are under the hood. All the same, let’s take a look at the finished themes, in two color variations.

Here’s Hugo:

Here's Jekyll:

Wait who won?

?

Both Hugo and Jekyll have their quirks and conveniences.

From this developer’s perspective, Jekyll is a workable choice for simple sites without complicated organizational needs. If you’re looking to render some one-page posts in an available theme and host with GitHub Pages, Jekyll will get you up and running fairly quickly.

Personligt bruger jeg Hugo. Jeg kan godt lide de organisatoriske kapaciteter i sine Page Bundles, og den understøttes af et dedikeret og samvittighedsfuldt team, der virkelig synes at stræbe efter at gøre det lettere for deres brugere. Dette er tydeligt i Hugos mange funktioner og praktiske tricks som billedbehandling og kortkoder. De ser ud til at frigive nye rettelser og versioner så ofte, som jeg laver en ny kop kaffe - som afhængigt af din brugssag kan være fantastisk eller irriterende.

Hvis du stadig ikke kan beslutte, skal du ikke bekymre dig. OpenGitDocs-dokumentationstemaet, jeg oprettede, er tilgængeligt for både Hugo og Jekyll. Start med en, skift senere, hvis du vil. Det er fordelen ved at have muligheder.