當前位置: 首頁>>代碼示例>>Golang>>正文


Golang pongo2.Template類代碼示例

本文整理匯總了Golang中github.com/flosch/pongo2.Template的典型用法代碼示例。如果您正苦於以下問題:Golang Template類的具體用法?Golang Template怎麽用?Golang Template使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Template類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: renderTemplate

func renderTemplate(filepath string, data map[string]interface{}) []byte {

	var out string
	var err error
	var template pongo2.Template

	// Read the template from the disk every time
	if ServerConfig.Debug {
		newTemplate, err := pongo2.FromFile(filepath)
		if err != nil {
			panic(err)
		}
		template = *newTemplate

	} else {
		// Read the template and cache it
		cached, ok := templateCache[filepath]
		if ok == false {
			newTemplate, err := pongo2.FromFile(filepath)
			if err != nil {
				panic(err)
			}
			templateCache[filepath] = *newTemplate
			cached = *newTemplate
		}
		template = cached
	}

	out, err = template.Execute(data)
	if err != nil {
		panic(err)
	}
	return []byte(out)
}
開發者ID:walkr,項目名稱:gower,代碼行數:34,代碼來源:gower.go

示例2: renderLowLevel

func (d *Data) renderLowLevel(dst string, src string, prefix string, r io.Reader) error {
	var err error

	// Determine the filename and whether we're dealing with a template
	var tpl *pongo2.Template = nil
	filename := src
	if strings.HasSuffix(filename, ".tpl") {
		var buf bytes.Buffer
		if _, err := io.Copy(&buf, r); err != nil {
			return err
		}

		base := filepath.Dir(filename)
		if prefix != "" {
			base = filepath.Join(prefix, base)
		}

		// Create the template set so we can control loading
		tplSet := pongo2.NewSet("otto", &tplLoader{
			Data: d,
			Base: base,
		})

		// Parse the template
		dst = strings.TrimSuffix(dst, ".tpl")
		tpl, err = tplSet.FromString(buf.String())
		if err != nil {
			return err
		}
	}

	// Make the directory containing the final path.
	dir := filepath.Dir(dst)
	if err := os.MkdirAll(dir, 0755); err != nil {
		return err
	}

	// Create the file itself
	f, err := os.Create(dst)
	if err != nil {
		return err
	}
	defer f.Close()

	// If it isn't a template, do a direct byte copy
	if tpl == nil {
		_, err = io.Copy(f, r)
		return err
	}

	return tpl.ExecuteWriter(d.Context, f)
}
開發者ID:mbrodala,項目名稱:otto,代碼行數:52,代碼來源:data.go

示例3: renderTemplate

func renderTemplate(tpl *pongo2.Template, context pongo2.Context, r *http.Request, writer io.Writer) error {
	if Config.siteName == "" {
		parts := strings.Split(r.Host, ":")
		context["sitename"] = parts[0]
	} else {
		context["sitename"] = Config.siteName
	}

	context["sitepath"] = Config.sitePath
	context["using_auth"] = Config.authFile != ""

	return tpl.ExecuteWriter(context, writer)
}
開發者ID:andreimarcu,項目名稱:linx-server,代碼行數:13,代碼來源:templates.go

示例4: Render

func (p *pongoRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
	file := data[0].(string)
	ctx := data[1].(pongo2.Context)
	var t *pongo2.Template

	if tmpl, ok := p.cache[file]; ok {
		t = tmpl
	} else {
		tmpl, err := pongo2.FromFile(file)
		if err != nil {
			return err
		}
		p.cache[file] = tmpl
		t = tmpl
	}
	writeHeader(w, code, "text/html")
	return t.ExecuteWriter(ctx, w)
}
開發者ID:InsightDataScience,項目名稱:pgweb,代碼行數:18,代碼來源:example_pongo2.go

示例5: Render

func (p *Render) Render(w http.ResponseWriter, code int, data ...interface{}) error {
	file := "templates/" + data[0].(string)
	ctx := data[1].(pongo2.Context)
	var t *pongo2.Template

	if tmpl, ok := p.cache[file]; ok {
		t = tmpl
	} else {
		if options.Debug {
			tmpl, err := pongo2.FromFile(file)
			if err != nil {
				return err
			}
			t = tmpl
		} else {
			buff, err := server.Asset(file)
			if err == nil {
				tmpl, err := pongo2.FromString(string(buff))
				if err != nil {
					return err
				}
				t = tmpl
			} else {
				tmpl, err := pongo2.FromFile(file)
				if err != nil {
					return err
				}
				t = tmpl
			}

		}
		p.cache[file] = t
	}
	writeHeader(w, code, "text/html")
	return t.ExecuteWriter(ctx, w)
}
開發者ID:hi-trust,項目名稱:friendfeed,代碼行數:36,代碼來源:render.go

示例6: fileDisplayHandler

func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
	fileName := c.URLParams["name"]
	filePath := path.Join(Config.filesDir, fileName)

	err := checkFile(fileName)
	if err == NotFoundErr {
		notFoundHandler(c, w, r)
		return
	}

	metadata, err := metadataRead(fileName)
	if err != nil {
		oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
		return
	}
	var expiryHuman string
	if metadata.Expiry != neverExpire {
		expiryHuman = humanize.RelTime(time.Now(), metadata.Expiry, "", "")
	}
	sizeHuman := humanize.Bytes(uint64(metadata.Size))
	extra := make(map[string]string)

	file, _ := os.Open(filePath)
	defer file.Close()

	header := make([]byte, 512)
	file.Read(header)

	extension := strings.TrimPrefix(filepath.Ext(fileName), ".")

	if strings.EqualFold("application/json", r.Header.Get("Accept")) {
		js, _ := json.Marshal(map[string]string{
			"filename":  fileName,
			"expiry":    strconv.FormatInt(metadata.Expiry.Unix(), 10),
			"size":      strconv.FormatInt(metadata.Size, 10),
			"mimetype":  metadata.Mimetype,
			"sha256sum": metadata.Sha256sum,
		})
		w.Write(js)
		return
	}

	var tpl *pongo2.Template

	if strings.HasPrefix(metadata.Mimetype, "image/") {
		tpl = Templates["display/image.html"]

	} else if strings.HasPrefix(metadata.Mimetype, "video/") {
		tpl = Templates["display/video.html"]

	} else if strings.HasPrefix(metadata.Mimetype, "audio/") {
		tpl = Templates["display/audio.html"]

	} else if metadata.Mimetype == "application/pdf" {
		tpl = Templates["display/pdf.html"]

	} else if supportedBinExtension(extension) {
		if metadata.Size < maxDisplayFileSizeBytes {
			bytes, err := ioutil.ReadFile(filePath)
			if err == nil {
				extra["extension"] = extension
				extra["lang_hl"], extra["lang_ace"] = extensionToHlAndAceLangs(extension)
				extra["contents"] = string(bytes)
				tpl = Templates["display/bin.html"]
			}
		}
	} else if extension == "md" {
		if metadata.Size < maxDisplayFileSizeBytes {
			bytes, err := ioutil.ReadFile(filePath)
			if err == nil {
				unsafe := blackfriday.MarkdownCommon(bytes)
				html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)

				extra["contents"] = string(html)
				tpl = Templates["display/md.html"]
			}
		}
	}

	// Catch other files
	if tpl == nil {
		tpl = Templates["display/file.html"]
	}

	err = tpl.ExecuteWriter(pongo2.Context{
		"mime":     metadata.Mimetype,
		"filename": fileName,
		"size":     sizeHuman,
		"expiry":   expiryHuman,
		"extra":    extra,
		"files":    metadata.ArchiveFiles,
	}, w)

	if err != nil {
		oopsHandler(c, w, r, RespHTML, "")
	}
}
開發者ID:Uncurlhalo,項目名稱:linx-server,代碼行數:97,代碼來源:display.go

示例7: fileDisplayHandler

func fileDisplayHandler(c web.C, w http.ResponseWriter, r *http.Request) {
	fileName := c.URLParams["name"]
	filePath := path.Join(Config.filesDir, fileName)
	fileInfo, err := os.Stat(filePath)

	if !fileExistsAndNotExpired(fileName) {
		notFoundHandler(c, w, r)
		return
	}

	expiry, _ := metadataGetExpiry(fileName)
	var expiryHuman string
	if !expiry.IsZero() {
		expiryHuman = humanize.RelTime(time.Now(), expiry, "", "")
	}
	sizeHuman := humanize.Bytes(uint64(fileInfo.Size()))
	extra := make(map[string]string)

	file, _ := os.Open(filePath)
	header := make([]byte, 512)
	file.Read(header)
	file.Close()

	mimetype := mimemagic.Match("", header)
	extension := strings.TrimPrefix(filepath.Ext(fileName), ".")

	if strings.EqualFold("application/json", r.Header.Get("Accept")) {
		js, _ := json.Marshal(map[string]string{
			"filename": fileName,
			"mimetype": mimetype,
			"expiry":   strconv.FormatInt(expiry.Unix(), 10),
			"size":     strconv.FormatInt(fileInfo.Size(), 10),
		})
		w.Write(js)
		return
	}

	var tpl *pongo2.Template

	if strings.HasPrefix(mimetype, "image/") {
		tpl = Templates["display/image.html"]
	} else if strings.HasPrefix(mimetype, "video/") {
		tpl = Templates["display/video.html"]
	} else if strings.HasPrefix(mimetype, "audio/") {
		tpl = Templates["display/audio.html"]
	} else if mimetype == "application/pdf" {
		tpl = Templates["display/pdf.html"]
	} else if supportedBinExtension(extension) {
		if fileInfo.Size() < maxDisplayFileSizeBytes {
			bytes, err := ioutil.ReadFile(filePath)
			if err != nil {
				tpl = Templates["display/file.html"]
			} else {
				extra["extension"] = extension
				extra["lang_hl"], extra["lang_ace"] = extensionToHlAndAceLangs(extension)
				extra["contents"] = string(bytes)
				tpl = Templates["display/bin.html"]
			}
		} else {
			tpl = Templates["display/file.html"]
		}
	} else {
		tpl = Templates["display/file.html"]
	}

	err = tpl.ExecuteWriter(pongo2.Context{
		"mime":     mimetype,
		"filename": fileName,
		"size":     sizeHuman,
		"expiry":   expiryHuman,
		"extra":    extra,
	}, w)

	if err != nil {
		oopsHandler(c, w, r, RespHTML, "")
	}
}
開發者ID:matthazinski,項目名稱:linx-server,代碼行數:77,代碼來源:display.go

示例8: RenderTemplate

func RenderTemplate(t *pongo2.Template, w io.Writer, data interface{}) error {
	return t.ExecuteWriter(pongo2.Context{"v": data}.Update(baseContext), w)
}
開發者ID:samertm,項目名稱:githubstreaks,代碼行數:3,代碼來源:main.go


注:本文中的github.com/flosch/pongo2.Template類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。