點燈坊

失くすものさえない今が強くなるチャンスよ

Using pipe for Function Pipeline

Sam Xiao's Avatar 2022-01-12

We can use the | operator to compose multiple functions for Function Pipeline.

Version

Hugo 0.91

Page

pipe000

  • my blog is capitalized to My blog

  • ** in Markdown is transformed to <strong>

Page Variable

content/_index.md

---
title: my **blog**
---
  • title : use Markdown to define 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 : return the humanized version of an argument with the first letter capitalized
  • markdownify : transform Markdown to HTML
  • | : Function Pipeline from .Title to humanize to markdownify

Conclusion

  • When using multiple functions on the template, pipes are more readable for the codebase

Reference

Hugo, Pipes