Hugo vs Jekyll: Tema Penjana Laman Statik Epik

Dalam artikel ini, kita akan membandingkan nuansa membuat tema untuk dua penjana laman web statik teratas.

Saya baru-baru ini mengambil tugas membuat tema laman dokumentasi untuk dua projek. Kedua-dua projek memerlukan ciri asas yang sama, tetapi satu menggunakan Jekyll sementara yang lain menggunakan Hugo.

Dalam rasionaliti pemaju yang tipikal, jelas hanya ada satu pilihan. Saya memutuskan untuk membuat tema yang sama dalam kedua kerangka kerja, dan untuk memberi anda, pembaca yang dikasihi, perbandingan berdampingan.

Catatan ini bukan panduan pembinaan tema yang komprehensif, tetapi bertujuan untuk membiasakan anda dengan proses membina tema di mana-mana penjana. Inilah yang akan kita bahas:

  • Bagaimana fail tema disusun
  • Tempat meletakkan isi
  • Bagaimana templat berfungsi
  • Membuat menu tingkat atas dengan pagesobjek
  • Membuat menu dengan pautan bersarang dari senarai data
  • Menggabungkan templat
  • Membuat gaya
  • Cara mengkonfigurasi dan menyebarkan ke Halaman GitHub

Inilah kerangka wayar yang teruk dari tema yang akan saya buat.

Sekiranya anda merancang untuk membina, mungkin berguna untuk menyajikan tema secara tempatan semasa anda membinanya - dan kedua-dua penjana menawarkan fungsi ini. Untuk Jekyll, lari jekyll serve, dan untuk Hugo , hugo serve.

Terdapat dua elemen utama: kawasan kandungan utama, dan menu bar sisi yang sangat penting. Untuk membuatnya, anda memerlukan fail templat yang memberitahu penjana laman web bagaimana menghasilkan halaman HTML. Untuk mengatur fail templat tema dengan cara yang masuk akal, pertama-tama anda perlu mengetahui struktur direktori apa yang diharapkan oleh penjana laman web.

Bagaimana fail tema disusun

Jekyll menyokong tema berasaskan permata, yang pengguna boleh pasangkan seperti permata Ruby yang lain. Kaedah ini menyembunyikan fail tema di permata, jadi untuk tujuan perbandingan ini, kami tidak menggunakan tema berdasarkan permata.

Semasa anda berlari jekyll new-theme , Jekyll akan meletakkan tema baru untuk anda. Inilah rupa fail tersebut:

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

Nama-nama direktori sesuai dengan deskriptif. The _includesdirektori adalah untuk bit kecil kod yang anda gunakan semula di tempat yang berbeza, dalam banyak cara yang sama anda akan meletakkan mentega dalam semua perkara. (Hanya saya?)

The _layoutsdirektori mengandungi templat untuk pelbagai jenis halaman di laman web anda. The _sassfolder adalah untuk fail Sass digunakan untuk membina lembaran gaya laman web anda.

Anda boleh membuat perancah tema Hugo baru dengan menjalankan hugo new theme . Fail ini mempunyai:

. ├── 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 

Anda dapat melihat beberapa persamaan. Fail templat halaman Hugo dimasukkan layouts/. Perhatikan bahawa _defaultjenis halaman mempunyai fail untuk a list.htmldan a single.html.

Tidak seperti Jekyll, Hugo menggunakan nama fail khusus ini untuk membezakan antara halaman senarai (seperti halaman dengan pautan ke semua catatan blog anda di atasnya) dan halaman tunggal (seperti salah satu catatan blog anda). The layouts/partials/direktori mengandungi mentega bit boleh digunakan semula, dan fail lembaran gaya mempunyai tempat yang dipilih di dalam static/css/.

Struktur direktori ini tidak dilekatkan, kerana kedua-dua penjana laman web ini membenarkan penyesuaian. Sebagai contoh, Jekyll membolehkan anda menentukan koleksi, dan Hugo menggunakan kumpulan halaman. Ciri-ciri ini membolehkan anda mengatur kandungan anda dengan pelbagai cara, tetapi buat masa ini, mari kita lihat di mana meletakkan beberapa halaman mudah.

Tempat meletakkan isi

Untuk membuat menu laman web yang kelihatan seperti ini:

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

Anda memerlukan dua bahagian ("Pengenalan" dan "Penggunaan Lanjutan") yang mengandungi bahagian masing-masing.

Jekyll tidak ketat dengan lokasi kandungannya. Ini mengharapkan halaman di akar laman web anda, dan akan membina apa sahaja yang ada di sana. Inilah cara anda menyusun halaman ini di akar laman Jekyll anda:

. ├── 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 

Anda boleh mengubah lokasi sumber laman web dalam konfigurasi Jekyll anda.

Di Hugo, semua kandungan yang dirender diharapkan dalam content/folder. Ini menghalang Hugo daripada berusaha membuat halaman yang tidak anda inginkan, seperti 404.html, sebagai kandungan laman web. Inilah cara anda mengatur content/direktori anda di Hugo:

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

Untuk Hugo, _index.mddan index.mdbermaksud perkara yang berbeza. Sangat berguna untuk mengetahui jenis Bundle Halaman yang anda inginkan untuk setiap bahagian: Daun, yang tidak memiliki anak, atau Cabang.

Sekarang setelah anda mempunyai idea tentang tempat meletakkan barang, mari kita lihat cara membina templat halaman.

Bagaimana templat berfungsi

Templat halaman Jekyll dibina dengan bahasa templat Liquid. Ia menggunakan penyokong gigi untuk output kandungan ubah ke halaman, seperti tajuk laman: {{ page.title }}.

Templat Hugo juga menggunakan pendakap, tetapi mereka dibina dengan Templat Go. Sintaks ini adalah sama, tetapi berbeza: {{ .Title }}.

Kedua-dua Templat Liquid dan Go dapat menangani logik. Cecair menggunakan sintaks tag untuk menunjukkan operasi logik:

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

Dan Templat Go meletakkan fungsi dan argumennya dalam sintaks pendakapnya:

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

Bahasa templat membolehkan anda membina satu halaman HTML kerangka, kemudian beritahu penjana laman web untuk meletakkan kandungan berubah di kawasan yang anda tentukan. Mari bandingkan dua defaulttemplat halaman yang mungkin untuk Jekyll dan 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.

Personally, I use Hugo. I like the organizational capabilities of its Page Bundles, and it’s backed by a dedicated and conscientious team that really seems to strive to facilitate convenience for their users. This is evident in Hugo’s many functions, and handy tricks like Image Processing and Shortcodes. They seem to release new fixes and versions about as often as I make a new cup of coffee - which, depending on your use case, may be fantastic, or annoying.

If you still can’t decide, don’t worry. The OpenGitDocs documentation theme I created is available for both Hugo and Jekyll. Start with one, switch later if you want. That’s the benefit of having options.