本文整理匯總了Golang中github.com/flosch/pongo2.Must函數的典型用法代碼示例。如果您正苦於以下問題:Golang Must函數的具體用法?Golang Must怎麽用?Golang Must使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Must函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestMisc
func (s *TestSuite) TestMisc(c *C) {
// Must
// TODO: Add better error message (see issue #18)
c.Check(
func() { pongo2.Must(testSuite2.FromFile("template_tests/inheritance/base2.tpl")) },
PanicMatches,
`\[Error \(where: fromfile\) in .*template_tests/inheritance/doesnotexist.tpl | Line 1 Col 12 near 'doesnotexist.tpl'\] open .*template_tests/inheritance/doesnotexist.tpl: no such file or directory`,
)
// Context
c.Check(parseTemplateFn("", pongo2.Context{"'illegal": nil}), PanicMatches, ".*not a valid identifier.*")
// Registers
c.Check(func() { pongo2.RegisterFilter("escape", nil) }, PanicMatches, ".*is already registered.*")
c.Check(func() { pongo2.RegisterTag("for", nil) }, PanicMatches, ".*is already registered.*")
// ApplyFilter
v, err := pongo2.ApplyFilter("title", pongo2.AsValue("this is a title"), nil)
if err != nil {
c.Fatal(err)
}
c.Check(v.String(), Equals, "This Is A Title")
c.Check(func() {
_, err := pongo2.ApplyFilter("doesnotexist", nil, nil)
if err != nil {
panic(err)
}
}, PanicMatches, `\[Error \(where: applyfilter\)\] Filter with name 'doesnotexist' not found.`)
}
示例2: HomeHandler
func HomeHandler(rw http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, cfg.SessionName)
conf = &oauth2.Config{
ClientID: os.Getenv("GOOGLE_CLIENT_ID"),
ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),
RedirectURL: os.Getenv("GOOGLE_CLIENT_REDIRECT"),
Scopes: []string{
"https://www.googleapis.com/auth/plus.login",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
},
Endpoint: google.Endpoint,
}
if session.Values["googleId"] != nil {
http.Redirect(rw, r, "/dashboard", 301)
}
// Generate google signin url with xsrf token
url := conf.AuthCodeURL(session.Values["xsrf"].(string))
tmpl := pongo2.Must(pongo2.FromFile("./templates/home.html"))
err := tmpl.ExecuteWriter(pongo2.Context{"GoogleAuthUrl": url}, rw)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
}
}
示例3: My
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// }
// }
func (this *ScheduleController) My() {
w := this.ResponseWriter
r := this.Request
r.ParseForm()
user := r.FormValue("user")
page := u.Page{PageSize: 10, ShowPages: 5}
currentPage := r.FormValue("page")
log.Println("當前頁數", currentPage)
page.CurrentPage, _ = strconv.Atoi(currentPage)
page.InitSkipRecords()
log.Println("過濾多少頁", page.SkipRecords)
log.Println("總頁數", page.TotalPages)
order := m.Order{RUser: user}
page = order.GetRuserOrders(page)
page.InitTotalPages()
context := pongo2.Context{"orderlist": page.Data}
var tplExample = pongo2.Must(pongo2.FromFile("views/Schedule.my.tpl"))
err := tplExample.ExecuteWriter(context, w)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
示例4: Instance
// Instance should return a new Pongo2Render struct per request and prepare
// the template by either loading it from disk or using pongo2's cache.
func (p Pongo2Render) Instance(name string, data interface{}) render.Render {
var template *pongo2.Template
filename := path.Join(p.Options.TemplateDir, name)
// always read template files from disk if in debug mode, use cache otherwise.
if gin.Mode() == "debug" {
template = pongo2.Must(pongo2.FromFile(filename))
} else {
template = pongo2.Must(pongo2.FromCache(filename))
}
return Pongo2Render{
Template: template,
Context: data.(pongo2.Context),
Options: p.Options,
}
}
示例5: renderTemplate
func (m Machine) renderTemplate(templateName string) (string, error) {
var tpl = pongo2.Must(pongo2.FromFile(path.Join("templates", templateName)))
result, err := tpl.Execute(pongo2.Context{"machine": m})
if err != nil {
return "", err
}
return result, err
}
示例6: Instance
func (p PongoDebug) Instance(name string, data interface{}) render.Render {
t := pongo2.Must(pongo2.FromFile(path.Join(p.Path, name)))
return Pongo{
Template: t,
Name: name,
Data: data,
}
}
示例7: getFilledTemplate
// getFilledTemplate returns the filled template as a slice of bytes.
// Initially wanted to use here the stdlib's text/template but ran into issues
// with the if instruction.
// The template looks quite ugly because of the blank lines left by the tags.
// https://code.djangoproject.com/ticket/2594 (WONTFIX)
// https://github.com/flosch/pongo2/issues/94
func getFilledTemplate(ctxt pongo2.Context, tplFile string) ([]byte, error) {
t := pongo2.Must(pongo2.FromFile(tplFile))
output, err := t.ExecuteBytes(ctxt)
if err != nil {
log.Fatal(err)
}
return output, nil
}
示例8: Instance
func (p Pongo2Common) Instance(name string, data interface{}) render.Render {
tpl := pongo2.Must(pongo2.FromCache(path.Join(p.BasePath, name)))
return &Pongo2{
Template: tpl,
Name: name,
Data: data,
}
}
示例9: main
func main() {
kingpin.Parse()
t := pongo2.Must(pongo2.FromFile(*templatePath))
err := t.ExecuteWriter(getContext(), os.Stdout)
if err != nil {
panic(err)
}
}
示例10: handleViewer
func handleViewer(res http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
tplData, _ := Asset("viewer.html")
tpl := pongo2.Must(pongo2.FromString(string(tplData)))
tpl.ExecuteWriter(pongo2.Context{
"ipfs_hash": vars["ipfs_hash"],
"branding": cfg.Branding,
}, res)
}
示例11: DashboardHandler
func DashboardHandler(rw http.ResponseWriter, r *http.Request) {
session, _ := store.Get(r, cfg.SessionName)
tmpl := pongo2.Must(pongo2.FromFile("./templates/dash.html"))
if session.Values["googleId"] != "" {
err := tmpl.ExecuteWriter(pongo2.Context{"GoogleId": session.Values["googleId"]}, rw)
if err != nil {
panic(err)
}
}
}
示例12: Pongo2
func Pongo2() echo.MiddlewareFunc {
return func(h echo.HandlerFunc) echo.HandlerFunc {
return func(ctx *echo.Context) error {
err := h(ctx)
if err != nil {
return err
}
templateName := ctx.Get("template")
if templateName == nil {
http.Error(
ctx.Response().Writer(),
"Template in Context not defined.",
500)
}
var contentType, encoding string
var isString bool
ct := ctx.Get("ContentType")
if ct == nil {
contentType = ContentHTML
} else {
contentType, isString = ct.(string)
if !isString {
contentType = ContentHTML
}
}
cs := ctx.Get("charset")
if cs == nil {
encoding = defaultCharset
} else {
encoding, isString = cs.(string)
if !isString {
encoding = defaultCharset
}
}
newContentType := contentType + "; charset=" + encoding
templateNameValue, isString := templateName.(string)
if isString {
templateData := ctx.Get("data")
var template = pongo2.Must(pongo2.FromFile(path.Join("templates", templateNameValue)))
ctx.Response().Header().Set(ContentType, newContentType)
err = template.ExecuteWriter(
getContext(templateData), ctx.Response().Writer())
if err != nil {
http.Error(
ctx.Response().Writer(), err.Error(), 500)
}
}
return nil
}
}
}
示例13: renderTemplate
// Render template among with machine and config struct
func (m Machine) renderTemplate(template string, config Config) (string, error) {
template = path.Join(config.TemplatePath, template)
if _, err := os.Stat(template); err != nil {
return "", errors.New("Template does not exist")
}
var tpl = pongo2.Must(pongo2.FromFile(template))
result, err := tpl.Execute(pongo2.Context{"machine": m, "config": config})
if err != nil {
return "", err
}
return result, err
}
示例14: handleHelpPage
func handleHelpPage(res http.ResponseWriter, r *http.Request) {
content, err := ioutil.ReadFile("frontend/help.md")
if err != nil {
log.WithFields(logrus.Fields{
"error": fmt.Sprintf("%v", err),
}).Error("HelpText Load")
http.Error(res, "An unknown error occured.", http.StatusInternalServerError)
return
}
template := pongo2.Must(pongo2.FromFile("frontend/help.html"))
ctx := getBasicContext(res, r)
ctx["helptext"] = string(content)
template.ExecuteWriter(ctx, res)
}
示例15: Render
// Renders a template
func (t *Templating) Render(context Context, name string) {
var filename = fmt.Sprintf("%s/%s", t.GetViewsDirectory(), name)
if _, err := os.Stat(filename); err != nil {
if os.IsNotExist(err) {
log.Printf("View '%s' does not exists", filename)
os.Exit(1)
}
}
var template = pongo2.Must(pongo2.FromFile(filename))
template.ExecuteWriter(pongo2.Context{
"request": context.GetRequest(),
"response": context.GetResponse(),
}, context.GetResponse())
}