我正在学习如何在 Go 中嵌入 HTML。然后我在运行 server.go 时收到此消息
template executing error: html/template:base.html:30:25: no such template "Sidebar"
这是我的代码 Go-HTML-Template
//server.go
package main
import (
"fmt"
"html/template"
"io"
"log"
"net/http"
"time"
)
const STATIC_URL string = "/assets/"
const STATIC_ROOT string = "assets/"
type Context struct {
Title string
Static string
}
func Home(w http.ResponseWriter, req *http.Request) {
context := Context{Title: "Welcome!"}
render(w, "index", context)
}
func About(w http.ResponseWriter, req *http.Request) {
context := Context{Title: "About"}
render(w, "about", context)
}
func render(w http.ResponseWriter, tmpl string, context Context) {
context.Static = STATIC_URL
tmpl_list := []string{"templates/base.html",
fmt.Sprintf("templates/%s.html", tmpl)}
t, err := template.ParseFiles(tmpl_list...)
if err != nil {
log.Print("template parsing error: ", err)
}
err = t.Execute(w, context)
if err != nil {
log.Print("template executing error: ", err)
}
}
func StaticHandler(w http.ResponseWriter, req *http.Request) {
static_file := req.URL.Path[len(STATIC_URL):]
if len(static_file) != 0 {
f, err := http.Dir(STATIC_ROOT).Open(static_file)
if err == nil {
content := io.ReadSeeker(f)
http.ServeContent(w, req, static_file, time.Now(), content)
return
}
}
http.NotFound(w, req)
}
func main() {
http.HandleFunc("/", Home)
http.HandleFunc("/about/", About)
http.HandleFunc(STATIC_URL, StaticHandler)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
我制作了 sidebar.html 模板并将其包含在 base.html 中,就像我包含 index.html 一样。我遵循本教程 Golang Web Apps为了这次学习。不仅侧边栏,我也不能包括页眉和页脚
<!--base.html-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CELERATES UNIVERSITY</title>
<!-- Favicon -->
<link rel="icon" type="image/png" href="{{ .Static }}img/favicon2.ico">
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="{{ .Static }}css/bootstrap.min.css" type="text/css">
<!-- Animation library for notifications -->
<link href="{{ .Static }}css/animate.min.css" rel="stylesheet"/>
<!-- Light Bootstrap Table core CSS -->
<link href="{{ .Static }}css/light-bootstrap-dashboard.css" rel="stylesheet"/>
<!-- Fonts and icons -->
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Roboto:400,700,300' rel='stylesheet' type='text/css'>
<link href="{{ .Static }}css/pe-icon-7-stroke.css" rel="stylesheet" />
<!-- Datatables -->
<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"/>
</head>
<body>
<div class="wrapper">
<!--SIDEBAR-->
{{ template "Sidebar" . }}
<div class="main-panel">
<!--HEADER-->
{{ template "Header" . }}
<!--CONTENT-->
<div class="content">
<div class="container-fluid" align="center">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
{{ template "content" . }}
</div>
</div>
</div>
</div>
<!--FOOTER-->
{{ template "Footer" . }}
</div>
</div>
<!-- JAVASCRIPTS -->
<!-- Core JS Files -->
<script src="https://code.jquery.com/jquery-3.3.1.js" type="text/javascript"></script>
<script src="{{ .Static }}js/bootstrap.min.js" type="text/javascript"></script>
<!-- Datatables -->
<script type="text/javascript" src="http://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#example").DataTable();
} );
</script>
<!-- Checkbox, Radio & Switch Plugins -->
<script src="{{ .Static }}js/bootstrap-checkbox-radio-switch.js"></script>
<!-- Charts Plugin -->
<script src="{{ .Static }}js/chartist.min.js"></script>
<!-- Notifications Plugin -->
<script src="{{ .Static }}js/bootstrap-notify.js"></script>
<!-- Google Maps Plugin -->
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<!-- Light Bootstrap Table Core javascript and methods for Demo purpose -->
<script src="{{ .Static }}js/light-bootstrap-dashboard.js"></script>
</body>
{{ define "Sidebar" }}
<!--sidebar.html-->
<div class="sidebar" data-color="blue" data-image="{{ .Static }}img/sidebar-4.jpg">
<div class="sidebar-wrapper">
<div class="logo">
<a href="/" class="simple-text">
Celerates University
</a>
</div>
<ul class="nav">
<li>
<a href="/">
<i class="pe-7s-graph"></i>
<p>Dashboard</p>
</a>
</li>
<li>
<a href="/">
<i class="pe-7s-user"></i>
<p>Admin List</p>
</a>
</li>
<li>
<a href="/">
<i class="pe-7s-note2"></i>
<p>Student List</p>
</a>
</li>
</ul>
</div>
</div>
{{ end }}
我写的侧边栏模板正确吗?
任何帮助将不胜感激。谢谢。
最佳答案
不是 HTML WEB 专家但是
您的 slice 创建不正确,没有模板(页脚、侧边栏和页眉)作为元素。 当我尝试打印 tmpl_list 时,我看到以下结果。
[templates/base.html templates/index.html]
如果您按如下方式创建 tmpl_list,它应该可以工作。 (暂时只是一个解决方法) 您需要查看创建 slice tmpl_list 的部分。
tmpl_list := []string{"templates/base.html", "templates/about.html", "templates/footer.html", "templates/header.html", "templates/sidebar.html"}
关于html - Golang HTML Web Apps 中没有这样的模板 "xxx",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54104020/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择