」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何將數據作為參數傳遞給GO \ \ s模板系統中的包含模板?

如何將數據作為參數傳遞給GO \ \ s模板系統中的包含模板?

發佈於2025-03-24
瀏覽:895

How can I pass data as arguments to an included template in Go\'s templating system? 
在GO的模板系統中傳遞數據

實現此目的,您可以利用將參數合併為單個切片值的自定義函數。通過註冊此功能,可以在模板調用中使用。以下代碼說明瞭如何完成:

接口{} {return vs}, })。 parse(“ {{{template \“ image_row \” arg.5}}}”) 如果err! = nil { fmt.println(err) 返回 } err = t.execute(template.must(template.parsefiles)(“ index.html”,“ image_row.html”)),nil) 如果err! = nil { fmt.println(err) 返回 } } // index.html {{template“ image_row”。 | 5}}} // image_row.html {{定義“ image_row”}} 在此處{{index。 0}} {{index。 1}}} {{end}} 在Image_row Template中,可以使用內置索引函數訪問參數。例如,{{index。 0}}將訪問第一個參數(index 0)從index.html模板傳遞,在這種情況下為數字5。

此解決方案提供了一種通用的方式來傳遞和訪問多個模板之間的數據,從而啟用自定義功能和有效的代碼重複使用。
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3