首页 > 其他分享 >Go - Web application 3

Go - Web application 3

时间:2024-09-03 20:05:19浏览次数:5  
标签:Web struct err app snippet application Snippet html Go

Displaying dynamic data

func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
    id, err := strconv.Atoi(r.PathValue("id"))
    if err != nil || id < 1 {
        http.NotFound(w, r)
        return
    }

    snippet, err := app.snippet.Get(id)
    if err != nil {
        if errors.Is(err, models.ErrNoRecord) {
            http.NotFound(w, r)
        } else {
            app.serverError(w, r, err)
        }
        return
    }

    // Initialize a slice containing the paths to the view.html file, 
    // plus the base layout and navigation partial that we made earlier.
    files := []string{
        "./ui/html/base.html",
        "./ui/html/partials/nav.html",
        "./ui/html/pages/view.html",
    }

    // Parse the template files.
    ts, err := template.ParseFiles(files...)
    if err != nil {
        app.serverError(w, r, err)
        return
    }

    // And then execute them. Notice how we are passing in the snippet 
    // data (a models.Snippet struct) as the final parameter.
    err = ts.ExecuteTemplate(w, "base", snippet)
    if err != nil {
        app.serverError(w, r, err)
    }
}

Any data that you pass as the final parameter to ts.ExecuteTemplate() is represented within your HTML templates by the . character (referred to as dot).

In this specific case, the underlying type of dot will be a models.Snippet struct. When the underlying type of dot is a struct, you can render (or yield) the value of any exported field in your templates by postfixing dot with the field name. So, because our models.Snippet struct has a Title field, we could yield the snippet title by writing {{.Title}} in our templates.

{{define "title"}}Snippet #{{.ID}}{{end}}

{{define "main"}}
    <div class="snippet">
        <div class="metadata">
            <strong>{{.Title}}</strong>
            <span>#{{.ID}}</span>
        </div>
        <pre><code>{{.Content}}</code></pre>
        <div class="metadata">
            <time>Created: {{.Created}}</time>
            <time>Expires: {{.Expires}}</time>
        </div>
    </div>
{{end}}

 

标签:Web,struct,err,app,snippet,application,Snippet,html,Go
From: https://www.cnblogs.com/zhangzhihui/p/18395336

相关文章

  • 第一章 Django基础与虚拟环境
    1.Web框架和Django框架1.1网络通信注意:局域网个人一般写程序,想要让别人访问:阿里云、腾讯云。去云平台租服务器(含公网IP)程序放在云服务器让网络中可以互相通信的双发收发数据。服务端【我的电脑】importsocket#1.监听本机的IP和端口sock=socket.socket(socke......
  • WebShell流量特征检测_蚁剑篇
    80后用菜刀,90后用蚁剑,95后用冰蝎和哥斯拉,以phpshell连接为例,本文主要是对这四款经典的webshell管理工具进行流量分析和检测。什么是一句话木马?1、定义顾名思义就是执行恶意指令的木马,通过技术手段上传到指定服务器并可以正常访问,将我们需要服务器执行的命令上传并执行2、特点......
  • Leangoo领歌Scrum工具:Sprint Backlog迭代管理的最佳实践
    ​在敏捷开发中,迭代管理是确保项目持续推进、不断优化的重要环节。有效的迭代管理能够帮助团队快速响应变化,持续交付高质量产品。Leangoo是一款免费的敏捷项目管理工具,为团队提供了直观、高效的看板管理方式来管理迭代过程。本文将探讨如何使用Leangoo进行迭代管理,帮助团队更好......
  • 调用azure的npm实现outlook_api模拟查看邮件、发送邮件(实现web版接受outlook邮件第一
    文章目录⭐前言⭐注册azure应用......
  • Java中的Web服务开发:RESTful API的最佳实践
    Java中的Web服务开发:RESTfulAPI的最佳实践大家好,我是微赚淘客返利系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!在现代Web应用开发中,RESTfulAPI是构建可伸缩、易于维护的Web服务的关键。Java作为一门流行的服务端语言,提供了多种框架来简化RESTfulAPI的开发。本文将探讨......
  • Go - Web Application 2
    Creatingadatabaseconnectionpool//Thesql.Open()functioninitializesanewsql.DBobject,whichisessentiallya//poolofdatabaseconnections.db,err:=sql.Open("mysql","web:pass@/snippetbox?parseTime=true")iferr!=nil......
  • [Azure Application Insights]Azure应用程序见解概述页面中workspace的link不见了?
    问题描述在AzureApplicationInsights的概述页面中,可以直接点击WorkspaceLink进入到Workspace资源页面。但是,在下面的示例图中,WorkspaceLink不见了?这是什么原因呢? 问题解答这是因为Workspace的资源组发生了改变。ApplicationInsights无法根据WorksapceResourceID......
  • 文献解读- Genome-wide imputation using the practical haplotype graph in the hete
    关键词:农业;基因测序;变异检测;文献简介标题(英文):Genome-wideimputationusingthepracticalhaplotypegraphintheheterozygouscropcassava标题(中文):使用杂合作物木薯中的实用单倍型图进行全基因组插补发表期刊:G3作者单位:康奈尔大学等发表年份:2021文章地址:https://doi.org/10.10......
  • 【Go 实践学习】内存泄漏情景及pprof工具使用(上半篇)
    目录什么是内存泄漏?两类内存泄漏暂时性内存泄漏永久性内存泄漏常见的内存泄漏及解决办法浅拷贝共享底层资源,导致无关内存无法释放子切片导致的内存泄漏子字符串导致的内存泄漏子切片未重置指针索引挂起的goroutines导致的内存泄漏死循环导致的内存泄漏阻塞的通道读......
  • SpringBoot项目常用配置文件MybatisPlusConfig、RedisConfig、RedissonConfig、Swagge
    MybatisPlusConfig:@Configuration@MapperScan("com.yupi.usercenter.mapper")publicclassMybatisPlusConfig{@BeanpublicMybatisPlusInterceptormybatisPlusInterceptor(){MybatisPlusInterceptorinterceptor=newMybatisPlusInterc......