发布日期: 2025-12-31
版本号: v0.154.0

Hugo v0.154.0 是今年发布的第14个主要版本,推出了名为“partial decorators”或“带点变化的partials”的新功能。这是一个非常强大的构建块,其核心是 inner 关键字,它允许在一个 partial 模板中零次或多次调用,并且它的存在会反转执行方向,使得被调用方成为调用方。装饰器还支持深度嵌套。此外,本次更新在 reflect 工具包中增加了一些新的实用函数,使得在 Hugo 中识别核心类型(例如,识别一个可处理的图片资源)变得更加简单。该版本还包含若干错误修复、功能改进以及依赖项的更新。

更新内容 (中文)

Hugo v0.154.0 是今年的第14个正式版本(不包含补丁发布),并引入了部分装饰器,即“具有特殊功能的partials”。这是一个非常强大的结构,我(@bep)一直希望在Hugo中实现它,但始终未能找到合适的实现方式。直到现在。

一个简单且不太实用的示例:

{{ with partial "b.html" "World" }}Hello {{ . }}{{ end }}
{{ define "_partials/b.html" }}<b>{{ inner . }}</b>{{ end }}

上述代码渲染结果为:

<b>Hello World</b>
  • 新增的 inner 关键字可以在partial模板中使用零次或多次,通常配合不同的数据(例如在range中使用页面),它的存在标志着执行逻辑的反转——被调用者成为调用者。
  • 装饰器支持深度嵌套,参见此PR中的示例。

此版本还在 reflect 包中新增了一些实用函数,用于识别Hugo的核心类型。例如,识别可处理图像以往并不简单,现在可以这样做:

{{ $obj := . }}
{{ if reflect.IsResource $obj }}
   {{ if reflect.IsImageResource $obj }}
        // 它具有宽度/高度且可处理。
   {{ else }}
       // 仅链接即可。
   {{ end }}
{{ end }}

缺陷修复

  • tpl/collections:修复apply函数与内置函数(如len)的兼容问题 5c7fad23 @bep #13418
  • 撤回"resources/page:修复栏目页面的slugorcontentbasename" bf1d20d7 @bep #14104 #14325

改进

  • helpers:限制详细的watch输出以提升可读性 d3b5d47a @majiayu000 #14277
  • tpl/reflect:优化IsImageResource的实现逻辑 86cd1838 @bep
  • internal/warpc:将WebP内存限制提升至384 MiB 871da337 @jmooring #14309
  • 更新 tpl/reflect/reflect.go 1deec99b @bep
  • 新增 reflect.Is{Page,Site,Resource,ImageResource} 函数 b7bb557c @bep #14307
  • 允许partials作为装饰器使用 7c19c196 @bep #13193

依赖更新

  • build(deps): 将 github.com/tetratelabs/wazero 从 1.10.1 升级至 1.11.0 2637aa15 @dependabot[bot]

更新内容 (原始)

Hugo v0.154.0 is the 14th release this year (not counting patch releases) and introduces partial decorators, or “partials with a twist.” This is a very powerful construct that I, @bep, have always wanted to have in Hugo, but I could never wrap my head around an implementation. Until now.

A small and not very useful example:

{{ with partial "b.html" "World" }}Hello {{ . }}{{ end }}
{{ define "_partials/b.html" }}<b>{{ inner . }}</b>{{ end }}

The above renders to:

<b>Hello World</b>
  • The new inner keyword can be used zero or more times in a partial template, typically with different data (e.g. pages in a range), and its presence signals a reversal of the execution – the callee becomes the caller.
  • Decorators can be deeply nested, see this PR for an example.

This release also brings some new utility funcs in the reflect package to identify the core types in Hugo. For example, to identify an processable image hasn’t been trivial, now it is:

{{ $obj := . }}
{{ if reflect.IsResource $obj }}
   {{ if reflect.IsImageResource $obj }}
        // It has width/height and we can process it.
   {{ else }}
       // Just link to it.
   {{ end }}
{{ end }}

Bug fixes

  • tpl/collections: Fix apply to work with built-in funcs like len 5c7fad23 @bep #13418
  • Revert “resources/page: Fix slugorcontentbasename for section pages” bf1d20d7 @bep #14104 #14325

Improvements

  • helpers: Limit verbose watch output for better readability d3b5d47a @majiayu000 #14277
  • tpl/reflect: Make the IsImageResource implementation less technical 86cd1838 @bep
  • internal/warpc: Increase WebP memory limit to 384 MiB 871da337 @jmooring #14309
  • Update tpl/reflect/reflect.go 1deec99b @bep
  • Add reflect.Is{Page,Site,Resource,ImageResource} b7bb557c @bep #14307
  • Allow partials to work as decorators 7c19c196 @bep #13193

Dependency Updates

  • build(deps): bump github.com/tetratelabs/wazero from 1.10.1 to 1.11.0 2637aa15 @dependabot[bot]

下载链接