当前位置: 首页>>代码示例>>Golang>>正文


Golang Template.Lookup方法代码示例

本文整理汇总了Golang中html/template.Template.Lookup方法的典型用法代码示例。如果您正苦于以下问题:Golang Template.Lookup方法的具体用法?Golang Template.Lookup怎么用?Golang Template.Lookup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在html/template.Template的用法示例。


在下文中一共展示了Template.Lookup方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: dirList

func dirList(ci inject.CopyInject, logger termlog.Logger, w http.ResponseWriter, name string, f http.File, templates *template.Template) {
	w.Header().Set("Cache-Control", "no-store, must-revalidate")
	files, err := f.Readdir(0)
	if err != nil {
		logger.Shout("Error reading directory for listing: %s", err)
		return
	}
	data := dirData{Name: name, Files: files}
	buff := bytes.NewBuffer(make([]byte, 0, 0))
	err = templates.Lookup("dirlist.html").Execute(buff, data)
	length := buff.Len()
	if err != nil {
		logger.Shout("Error producing directory listing: %s", err)
	}
	inj, err := ci.Sniff(buff)
	if err != nil {
		logger.Shout("Failed to inject in dir listing: %s", err)
		return
	}
	w.Header().Set(
		"Content-Length", fmt.Sprintf("%d", length+inj.Extra()),
	)
	_, err = inj.Copy(w)
	if err != nil {
		logger.Shout("Failed to inject in dir listing: %s", err)
		return
	}
}
开发者ID:npk,项目名称:devd,代码行数:28,代码来源:fileserver.go

示例2: Execute

func (context *Context) Execute(name string, result interface{}) {
	var tmpl *template.Template
	var cacheKey string

	if context.Action == "" {
		context.Action = name
	}

	if context.Resource != nil {
		cacheKey = path.Join(context.resourcePath(), name)
	} else {
		cacheKey = name
	}

	if t, ok := templates[cacheKey]; !ok || true {
		var err error
		tmpl = template.New("layout.tmpl").Funcs(context.FuncMap())
		if tmpl, err = context.FindTemplate(tmpl, "layout.tmpl"); err == nil {
			for _, name := range []string{"header", "footer"} {
				if tmpl.Lookup(name) == nil {
					tmpl, _ = context.FindTemplate(tmpl, name+".tmpl")
				}
			}
		}
	} else {
		tmpl = t
	}

	context.Content = context.Render(name, result)
	if err := tmpl.Execute(context.Writer, context); err != nil {
		fmt.Println(err)
	}
}
开发者ID:NeoChow,项目名称:qor,代码行数:33,代码来源:context.go

示例3: initTemplates

func (r *TemplateRender) initTemplates(t *template.Template, tmps []string, exclude *[]string) error {
	firstLevel := 0 == len(*exclude)
	for tkey, tpl := range r.prepareTemplates(tmps...) {
		if nil == t.Lookup(tkey) {
			if data, err := ioutil.ReadFile(tpl); nil == err {
				tmps := templatesRegex.FindAllStringSubmatch(string(data), -1)

				ntemplates := []string{}
				if nil != tmps && len(tmps) > 0 {
					for _, it := range tmps {
						if sIndexOf(it[1], *exclude) < 0 {
							*exclude = append(*exclude, it[1])
							ntemplates = append(ntemplates, it[1])
						}
					}
				}

				// Prepare new templates
				if len(ntemplates) > 0 {
					if err = r.initTemplates(t, ntemplates, exclude); nil != err {
						return err
					}
				}

				if _, err = t.New(tkey).Parse(string(data)); nil != err {
					return err
				}
			} else if firstLevel {
				return err
			}
		}
	}
	return nil
}
开发者ID:gopk,项目名称:templates,代码行数:34,代码来源:templates.go

示例4: findTemplate

func findTemplate(name string, tpls *template.Template) (*template.Template, error) {
	tpl := tpls.Lookup(name)
	if tpl == nil {
		return nil, fmt.Errorf("unable to find template: %q", name)
	}
	return tpl, nil
}
开发者ID:jmheidly,项目名称:dex,代码行数:7,代码来源:config.go

示例5: notFound

func notFound(ci inject.CopyInject, templates *template.Template, w http.ResponseWriter) error {
	err := ci.ServeTemplate(http.StatusNotFound, w, templates.Lookup("404.html"), nil)
	if err != nil {
		return err
	}
	return nil
}
开发者ID:rawbite,项目名称:devd,代码行数:7,代码来源:fileserver.go

示例6: readtemplates

func readtemplates(dir string, titles map[Language]string) (err error) {
	var templates *template.Template
	templates, err = template.New("base").Funcs(tmplFuncs).ParseGlob(dir + "/*.tmpl")
	if err != nil {
		return
	}
	var d *os.File
	d, err = os.Open(dir)
	if err != nil {
		return
	}
	var v []os.FileInfo
	v, err = d.Readdir(0)
	if err != nil {
		return
	}
	for _, fi := range v {
		if fi.IsDir() {
			var t *template.Template
			t, err = templates.Clone()
			if err != nil {
				return
			}
			subdir := dir + "/" + fi.Name()
			_, err = t.ParseGlob(subdir + "/*.tmpl")
			if err != nil {
				return
			}
			th := t.Lookup("home")
			ti := t.Lookup("info")
			if th == nil {
				return fmt.Errorf(`Template "home" is missing in %s`, subdir)
			}
			if ti == nil {
				return fmt.Errorf(`Template "info" is missing in %s`, subdir)
			}
			title, ok := "", false
			if title, ok = titles[Language(fi.Name())]; !ok {
				if title, ok = titles[defaultlang]; !ok {
					title = "Uploader"
				}
			}
			langtmpl[Language(fi.Name())] = &tmpl{title, th, ti}
		}
	}
	defaulttmpl = langtmpl[defaultlang]
	if defaulttmpl == nil {
		fmt.Errorf("missing " + string(defaultlang) + " template")
	}
	languages = make([]Language, 0, len(langtmpl))
	languages = append(languages, defaultlang)
	for k := range langtmpl {
		if k != defaultlang {
			languages = append(languages, k)
		}
	}
	return
}
开发者ID:mehulsbhatt,项目名称:web-ftp-upload,代码行数:58,代码来源:template.go

示例7: HandleNotFound

// HandleNotFound handles pages not found. In particular, this handler is used
// when we have no matching route for a request. This also means it's not
// useful to inject the livereload paraphernalia here.
func HandleNotFound(templates *template.Template) httpctx.Handler {
	return httpctx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
		err := templates.Lookup("404.html").Execute(w, nil)
		if err != nil {
			logger := termlog.FromContext(ctx)
			logger.Shout("Could not execute template: %s", err)
		}
	})
}
开发者ID:jonaz,项目名称:devd,代码行数:13,代码来源:server.go

示例8: getMethodTemplate

func (f *RestHtmlFormattor) getMethodTemplate(m *template.Template, rest *RestContext) *template.Template {
	t := m.Lookup(rest.Method + f.dotsuffix)
	var err error
	if t == nil {
		t, err = m.New(rest.Method + f.dotsuffix).ParseFiles(filepath.Join(document_root, rest.Url, rest.Method+f.dotsuffix))
		if err != nil {
			fmt.Println("ERROR template.ParseFile: %v", err)
		}
	}
	return t
}
开发者ID:extrame,项目名称:goweb,代码行数:11,代码来源:resthtmlformater.go

示例9: dirList

func dirList(ci inject.CopyInject, logger termlog.Logger, w http.ResponseWriter, name string, f http.File, templates *template.Template) {
	w.Header().Set("Cache-Control", "no-store, must-revalidate")
	files, err := f.Readdir(0)
	if err != nil {
		logger.Shout("Error reading directory for listing: %s", err)
		return
	}
	data := dirData{Name: name, Files: files}
	err = ci.ServeTemplate(http.StatusOK, w, templates.Lookup("dirlist.html"), data)
	if err != nil {
		logger.Shout("Failed to generate dir listing: %s", err)
	}
}
开发者ID:rawbite,项目名称:devd,代码行数:13,代码来源:fileserver.go

示例10: Render

func (p *Page) Render(outdir string, templates *template.Template) error {
	tpl := templates.Lookup(p.Template)
	if tpl == nil {
		return fmt.Errorf("page: template %q not found", p.Template)
	}
	if err := os.MkdirAll(filepath.Join(outdir, filepath.Dir(p.Filename)), 0755); err != nil {
		return err
	}
	f, err := os.Create(filepath.Join(outdir, p.Filename))
	if err != nil {
		return err
	}
	defer f.Close()
	return tpl.Execute(f, p)
}
开发者ID:dchest,项目名称:kukuruz,代码行数:15,代码来源:page.go

示例11: Connector

func (cfg *LocalConnectorConfig) Connector(ns url.URL, lf oidc.LoginFunc, tpls *template.Template) (Connector, error) {
	tpl := tpls.Lookup(LoginPageTemplateName)
	if tpl == nil {
		return nil, fmt.Errorf("unable to find necessary HTML template")
	}

	idpc := &LocalConnector{
		id:        cfg.ID,
		namespace: ns,
		loginFunc: lf,
		loginTpl:  tpl,
	}

	return idpc, nil
}
开发者ID:Tecsisa,项目名称:dex,代码行数:15,代码来源:connector_local.go

示例12: optionalTemplate

func optionalTemplate(t *template.Template) func(string, ...interface{}) (template.HTML, error) {
	return func(name string, data ...interface{}) (template.HTML, error) {
		var tm = t.Lookup(name)
		if tm != nil {
			var b bytes.Buffer
			err := tm.ExecuteTemplate(&b, name, data)
			if err != nil {
				return template.HTML(""), err
			}
			return template.HTML(b.String()), nil
		} else {
			return template.HTML(""), nil
		}
	}
}
开发者ID:jpoehls,项目名称:viewengine,代码行数:15,代码来源:funcs.go

示例13: Execute

func (context *Context) Execute(name string, result interface{}) {
	var tmpl *template.Template
	var cacheKey string

	if name == "show" && !context.Resource.isSetShowAttrs {
		name = "edit"
	}

	if context.Action == "" {
		context.Action = name
	}

	if context.Resource != nil {
		cacheKey = path.Join(context.resourcePath(), name)
	} else {
		cacheKey = name
	}

	if t, ok := templates[cacheKey]; !ok || true {
		if file, err := context.FindTemplate("layout.tmpl"); err == nil {
			if tmpl, err = template.New(filepath.Base(file)).Funcs(context.FuncMap()).ParseFiles(file); err == nil {
				for _, name := range []string{"header", "footer"} {
					if tmpl.Lookup(name) == nil {
						if file, err := context.FindTemplate(name + ".tmpl"); err == nil {
							tmpl.ParseFiles(file)
						}
					} else {
						utils.ExitWithMsg(err)
					}
				}
			} else {
				utils.ExitWithMsg(err)
			}
		}
	} else {
		tmpl = t
	}

	context.Result = result
	context.Content = context.Render(name, result)
	if err := tmpl.Execute(context.Writer, context); err != nil {
		utils.ExitWithMsg(err)
	}
}
开发者ID:musicking,项目名称:qor,代码行数:44,代码来源:context.go

示例14: executeTpl

func executeTpl(ctx echo.Context, tpl *template.Template, data map[string]interface{}) error {
	objLog := logic.GetLogger(ctx)

	// 如果没有定义css和js模板,则定义之
	if jsTpl := tpl.Lookup("js"); jsTpl == nil {
		tpl.Parse(`{{define "js"}}{{end}}`)
	}
	if jsTpl := tpl.Lookup("css"); jsTpl == nil {
		tpl.Parse(`{{define "css"}}{{end}}`)
	}

	// 当前用户信息
	curUser, ok := ctx.Get("user").(*model.Me)
	if ok {
		data["me"] = curUser
	} else {
		data["me"] = map[string]interface{}{}
	}

	// websocket主机
	if global.OnlineEnv() {
		data["wshost"] = config.ConfigFile.MustValue("global", "domain")
	} else {
		data["wshost"] = global.App.Host + ":" + global.App.Port
	}
	global.App.SetUptime()
	data["app"] = global.App

	data["online_users"] = map[string]int{"online": logic.Book.Len(), "maxonline": logic.MaxOnlineNum()}

	buf := new(bytes.Buffer)
	err := tpl.Execute(buf, data)
	if err != nil {
		objLog.Errorln("excute template error:", err)
		return err
	}

	return ctx.HTML(http.StatusOK, buf.String())
}
开发者ID:studygolang,项目名称:studygolang,代码行数:39,代码来源:http.go

示例15: get

func get(driver *Template, name string) *Template {

	var contents string
	var t *template.Template
	var err error

	pth := "resources/template/" + name + ".tmpl"
	partials := filepath.Join(filepath.Dir(pth), "partials", "_*.tmpl")

	contents, err = load(pth)
	if err != nil {
		return &Template{nil, err}
	}

	if driver != nil {
		t, err = driver.Clone()
		if err != nil {
			return &Template{nil, err}
		}

		_, err = t.Parse(contents)
		for _, name := range blocknames {
			if found := t.Lookup(name); found == nil {
				t.Parse("{{ define `" + name + "`}}{{ end }}")
			}
		}

		t.ParseGlob(partials)

	} else {
		t = template.New(name)
		if err != nil {
			return &Template{nil, err}
		}
		t, err = t.Funcs(funcMap).Parse(contents)
	}
	return &Template{t, err}
}
开发者ID:stevemlayton,项目名称:archive-utils,代码行数:38,代码来源:template.go


注:本文中的html/template.Template.Lookup方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。