次世代 Hugo

無駄を削ぎ、本質を研ぐ

使用 pipe() 實現 Funtion Pipeline

Sam Xiao's Avatar 2025-06-28

我們可使用 | operator 將多個 Function 組合起來實現 Function Pipeline。

Version

Hugo 0.91

Page

pipe000

  • my blog 的首字母大寫後成為 My Blog
  • Markdown 中的 ** 被轉成 <strong>

Page Variable

content/_index.md

---
title: my **blog**
---
  • title:定義 Page variable

Pipe

layouts/index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="alpine.js" defer></script>
    <link rel="stylesheet" href="output.css" />
  </head>
  <body>
    <h1 class="text-4xl text-red-500">{{ .Title | humanize | markdownify }}</h1>
  </body>
</html>

Line 10

<h1 class="text-4xl text-red-500">{{ .Title | humanize | markdownify }}</h1>
  • humanize:將單字的第一個字大寫
  • markdownify:將 markdown 轉成 HTML
  • |:以 Function Pipeline 串連 humanize()markdownify()

Conclusion

  • 當在 Go template 使用多個 function 時,| 寫法的可讀性更高

Reference

Hugo, Pipes