Hugo 配置參數
Hugo 創建自定義站點參數。
使用 params 鍵來自定義參數:
baseURL: https://example.org/
languageCode: en-US
params:
contact:
email: info@example.org
phone: +1 206-555-1212
subtitle: Reference, Tutorials, and Explanations
title: Project Documentation
baseURL = 'https://example.org/'
languageCode = 'en-US'
title = 'Project Documentation'
[params]
subtitle = 'Reference, Tutorials, and Explanations'
[params.contact]
email = 'info@example.org'
phone = '+1 206-555-1212'
{
"baseURL": "https://example.org/",
"languageCode": "en-US",
"params": {
"contact": {
"email": "info@example.org",
"phone": "+1 206-555-1212"
},
"subtitle": "Reference, Tutorials, and Explanations"
},
"title": "Project Documentation"
}
在模板中使用 Site 對象上的 Params 方法訪問自定義參數:
{{ .Site.Params.subtitle }} → Reference, Tutorials, and Explanations
{{ .Site.Params.contact.email }} → info@example.org鍵名應使用 camelCase 或 snake_case。雖然 TOML、YAML 和 JSON 允許 kebab-case 鍵,但它們不是有效的 標識符,在 鏈接 標識符時無法使用。
例如,您可以這樣做:
{{ .Site.params.camelCase.foo }}
{{ .Site.params.snake_case.foo }}但您不能這樣做:
{{ .Site.params.kebab-case.foo }}多語言站點
對於多語言站點,在每種語言下創建 params 鍵:
baseURL: https://example.org/
defaultContentLanguage: en
languages:
de:
languageCode: de-DE
languageDirection: ltr
languageName: Deutsch
params:
contact:
email: info@de.example.org
phone: +49 30 1234567
subtitle: Referenz, Tutorials und Erklärungen
title: Projekt Dokumentation
weight: 1
en:
languageCode: en-US
languageDirection: ltr
languageName: English
params:
contact:
email: info@example.org
phone: +1 206-555-1212
subtitle: Reference, Tutorials, and Explanations
title: Project Documentation
weight: 2
baseURL = 'https://example.org/'
defaultContentLanguage = 'en'
[languages]
[languages.de]
languageCode = 'de-DE'
languageDirection = 'ltr'
languageName = 'Deutsch'
title = 'Projekt Dokumentation'
weight = 1
[languages.de.params]
subtitle = 'Referenz, Tutorials und Erklärungen'
[languages.de.params.contact]
email = 'info@de.example.org'
phone = '+49 30 1234567'
[languages.en]
languageCode = 'en-US'
languageDirection = 'ltr'
languageName = 'English'
title = 'Project Documentation'
weight = 2
[languages.en.params]
subtitle = 'Reference, Tutorials, and Explanations'
[languages.en.params.contact]
email = 'info@example.org'
phone = '+1 206-555-1212'
{
"baseURL": "https://example.org/",
"defaultContentLanguage": "en",
"languages": {
"de": {
"languageCode": "de-DE",
"languageDirection": "ltr",
"languageName": "Deutsch",
"params": {
"contact": {
"email": "info@de.example.org",
"phone": "+49 30 1234567"
},
"subtitle": "Referenz, Tutorials und Erklärungen"
},
"title": "Projekt Dokumentation",
"weight": 1
},
"en": {
"languageCode": "en-US",
"languageDirection": "ltr",
"languageName": "English",
"params": {
"contact": {
"email": "info@example.org",
"phone": "+1 206-555-1212"
},
"subtitle": "Reference, Tutorials, and Explanations"
},
"title": "Project Documentation",
"weight": 2
}
}
}
命名空間
為防止命名沖突,模塊和主題開發者應對其模塊或主題特定的自定義參數進行命名空間化。
params:
modules:
myModule:
colors:
background: '#efefef'
font: '#222222'
[params]
[params.modules]
[params.modules.myModule]
[params.modules.myModule.colors]
background = '#efefef'
font = '#222222'
{
"params": {
"modules": {
"myModule": {
"colors": {
"background": "#efefef",
"font": "#222222"
}
}
}
}
}
要訪問模塊/主題設置:
{{ $cfg := .Site.Params.module.mymodule }}
{{ $cfg.colors.background }} → #efefef
{{ $cfg.colors.font }} → #222222