時(shí)間:2023-06-12 05:30:02 | 來源:網(wǎng)站運(yùn)營
時(shí)間:2023-06-12 05:30:02 來源:網(wǎng)站運(yùn)營
基礎(chǔ)配置篇:為博客網(wǎng)站項(xiàng)目選擇合適的前端模板引擎:pugEngine := iris.Django("./template", ".html") if config.ServerConfig.Env == "development" { //測試環(huán)境下動(dòng)態(tài)加載 pugEngine.Reload(true) } bootstrap.Application.RegisterView(pugEngine)
這個(gè)代碼塊的作用是選擇使用類Django的模板引擎語法,模板存放的目錄是template,模板的后綴的通用的.html 文件后綴,如果我們?cè)赾onfig.json 中配置了env 為 development 的話,則動(dòng)態(tài)加載模板。動(dòng)態(tài)加載模板的一個(gè)好處是,我們?cè)谡{(diào)試和修改模板的時(shí)候,不需要重啟golang項(xiàng)目,就可以通過刷新瀏覽器來看到修改后的效果。如果我們不啟用動(dòng)態(tài)加載,則每次修改模板,都需要重啟golang項(xiàng)目才能看到改動(dòng)效果。但是動(dòng)態(tài)加載會(huì)影響網(wǎng)站加載性能,因?yàn)槊看卧L問,他都需要重新解析模板。所以我們就有了env參數(shù)配置,在本地調(diào)試的時(shí)候,使用development模式,動(dòng)態(tài)加載模板,方便調(diào)試,在服務(wù)器上的生產(chǎn)環(huán)境,我們并不需要經(jīng)常改動(dòng)代碼,所以在服務(wù)器上,我們就使用production模式,他在啟動(dòng)的時(shí)候就將模板加載到內(nèi)存中,每次訪問就可以加快訪問速度了。func (bootstrap *Bootstrap) Serve() { bootstrap.Application.Logger().SetLevel(bootstrap.LoggerLevel) bootstrap.LoadRoutes() pugEngine := iris.Django("./template", ".html") if config.ServerConfig.Env == "development" { //測試環(huán)境下動(dòng)態(tài)加載 pugEngine.Reload(true) } bootstrap.Application.RegisterView(pugEngine) bootstrap.Application.Run( iris.Addr(fmt.Sprintf("127.0.0.1:%d", bootstrap.Port)), iris.WithoutServerError(iris.ErrServerClosed), iris.WithoutBodyConsumptionOnUnmarshal, )}
現(xiàn)在我們已經(jīng)將模板引擎注冊(cè)到iris中了,接下來,我就就可以使用html標(biāo)記語言來編寫模板了。接著我們?cè)谇懊嬉呀?jīng)創(chuàng)建的template文件夾中,建立一個(gè)index.html 文件,并寫上一些內(nèi)容:<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Hello World</title></head><body>Hello World!</body></html>
同樣的,我們先嘗試輸出Hello World試試看。func IndexPage(ctx iris.Context) { ctx.View("index.html")}
再次點(diǎn)擊右上角的綠色實(shí)心三角形運(yùn)行起來,來瀏覽器看看是否正常: 關(guān)鍵詞:引擎,模板,選擇,項(xiàng)目,配置,基礎(chǔ)
客戶&案例
營銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。