在Hugo中,有一個非常有用的功能,就是可以根據條件來渲染多個部分。這種功能可以使得我們根據特定的情況或條件來展示/隱藏頁面的某些部分。無論是在建立靜態網站還是動態網站中,這種條件渲染的功能都能夠幫助我們更靈活地控制頁面的呈現方式。在本文中,我將和大家一起探討如何在Hugo中實現條件渲染多個部分的方法及使用場景。
問題內容
我想渲染除網站主頁中的靜態檔案之外的每個資料夾中的所有markdown 文件,一種方法是在hugo 中使用union,但隨著資料夾數量的增加,我發現自己在重複union到處都是(帶聯合的程式碼被註釋了,順便說一下,它正在工作),所以我認為使用切片會是一個更好的主意,但是當我嘗試使用切片時,我收到以下錯誤-
渲染頁面失敗:「home」渲染失敗:「(目錄路徑)\layouts\index.html:12:19」:在<.pages> 處執行範本失敗:無法評估字串類型中的欄位頁面
目錄結構
index.html 程式碼
{{ define "main" }} <ul class="homepage-topic-sections-container"> {{$sectionNames := slice "posts" "problems" "tutorials"}} {{range $index, $sectionName := $sectionNames}} {{ range where .Pages "Section" $sectionName }} {{/* {{ range union (union (where .Pages "Section" "posts") (where .Pages "Section" "problems")) (where .Pages "Section" "tutorials") }} */}} <li> <section class="homepage-topic-section"> <h1 id="a-href-Permalink-Title-a"><a href="{{.Permalink}}">{{.Title}} </a></h1> <div> {{ range .Pages }} <h3 id="a-href-Permalink-Title-middot-Date-Format-January-a"><a href="{{.Permalink}}">{{.Title}} · {{.Date.Format "January 2, 2006"}}</a></h3> {{ end }} </div> </section> </li> {{end}} {{end}} </ul> {{ end }}
解決方法
https://www.php.cn/link/1330fef5fe4f742c1918c585c2da13b3一个>:
上下文(又稱「點」)
關於 go 模板,最容易被忽略的概念是 {{ . }}
總是引用目前上下文。
- 在範本的頂層,這將是可供範本使用的資料集。
-
但是,在迭代內部,它將具有循環中當前項目的值;即
{{ . }}
將不再引用整個頁面可用的資料。
在下面的程式碼中,.pages
中的點具有第一個 range
操作中目前項目的值。該值的類型是字串,並且它沒有欄位 pages
。這就是為什麼它失敗了,execute of template failed at <.pages>: can'tvaluate field pages in type string</.pages>
.
{{ define "main" }} <ul class="homepage-topic-sections-container"> {{$sectionnames := slice "posts" "problems" "tutorials"}} {{range $index, $sectionname := $sectionnames}} {{ range where .pages "section" $sectionname }} ^^^^^^
一種可能的修復方法是使用 $.
存取全域上下文:.pages
==> $.pages
。
也許更好的解決方案是列出排除部分。那麼當添加更多資料夾時就不需要修改程式碼了:
{{ define "main" }} <ul class="homepage-topic-sections-container"> {{ range where .Pages "Section" "!=" "static" }} <li>
以上是在 Hugo 中有條件地渲染多個部分的詳細內容。更多資訊請關注PHP中文網其他相關文章!

有效的Go應用錯誤日誌記錄需要平衡細節和性能。 1)使用標準log包簡單但缺乏上下文。 2)logrus提供結構化日誌和自定義字段。 3)zap結合性能和結構化日誌,但需要更多設置。完整的錯誤日誌系統應包括錯誤enrichment、日誌級別、集中式日誌、性能考慮和錯誤處理模式。

EmptyinterfacesinGoareinterfaceswithnomethods,representinganyvalue,andshouldbeusedwhenhandlingunknowndatatypes.1)Theyofferflexibilityforgenericdataprocessing,asseeninthefmtpackage.2)Usethemcautiouslyduetopotentiallossoftypesafetyandperformanceissues,

go'sconcurrencyModelisuniquedUetoItsuseofGoroutinesAndChannels,offeringAlightWeightandefficePappRockhiffcomparredTothread-likeLanguagesLikeLikeJjava,Python,andrust.1)

go'sconcurrencyModeluessgoroutinesandChannelStomanageConconCurrentPrommmengement.1)GoroutinesArightweightThreadThreadSthAtalLeadSthAtalAlaLeasyParalleAftasks,增強Performance.2)ChannelsfacilitatesfacilitatesafeDataTaAexafeDataTaAexchangeBetnegnegoroutinesGoroutinesGoroutinesGoroutinesGoroutines,crucialforsforsynchrroniz

Interfacesand -polymormormormormormingingoenhancecodereusanity和Maintainability.1)defineInterfaceSattherightabStractractionLevel.2)useInterInterFacesFordEffordExpentIndention.3)ProfileCodeTomeAgePerformancemacts。

initiTfunctioningOrunSautomation beforeTheMainFunctionToInitializePackages andSetUptheNvironment.it'susefulforsettingupglobalvariables,資源和performingOne-timesEtepaskSarpaskSacraskSacrastAscacrAssanyPackage.here'shere'shere'shere'shere'shodshowitworks:1)Itcanbebeusedinanananainapthecate,NotjustAckAckAptocakeo

接口組合在Go編程中通過將功能分解為小型、專注的接口來構建複雜抽象。 1)定義Reader、Writer和Closer接口。 2)通過組合這些接口創建如File和NetworkStream的複雜類型。 3)使用ProcessData函數展示如何處理這些組合接口。這種方法增強了代碼的靈活性、可測試性和可重用性,但需注意避免過度碎片化和組合複雜性。

initfunctionsingoareAutomationalCalledBeLedBeForeTheMainFunctionandAreuseFulforSetupButcomeWithChallenges.1)executiondorder:totiernitFunctionSrunIndIndefinitionorder,cancancapationSifsUsiseSiftheyDepplothother.2)測試:sterfunctionsmunctionsmunctionsMayInterfionsMayInterferfereWithTests,b


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

禪工作室 13.0.1
強大的PHP整合開發環境

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能