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


Golang Gas.Header方法代码示例

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


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

示例1: Output

func (o jsonOutputter) Output(code int, g *gas.Gas) {
	h := g.Header()
	if _, foundType := h["Content-Type"]; !foundType {
		h.Set("Content-Type", "application/json; charset=utf-8")
	}
	g.WriteHeader(code)
	json.NewEncoder(g).Encode(o.data)
}
开发者ID:moshee,项目名称:gas,代码行数:8,代码来源:out.go

示例2: Output

func (o *templateOutputter) Output(code int, g *gas.Gas) {
	templateLock.RLock()
	defer templateLock.RUnlock()
	group := Templates[o.path]
	var t *template.Template

	if group == nil {
		log.Printf("templates: failed to access template group \"%s\"", o.path)
		g.WriteHeader(500)
		fmt.Fprintf(g, "Error: template group \"%s\" not found. Did it fail to compile?", o.path)
		return
	}

	partial := g.Request.Header.Get("X-Ajax-Partial") != ""

	// If it's a partial page request, try to serve a partial template
	// (denoted by a '%' prepended to the template name). If it doesn't
	// exist, fall back to the regular one.
	if partial && !strings.HasPrefix(o.name, "%") {
		t = group.Lookup("%" + o.name)
	}

	if t == nil {
		t = group.Lookup(o.name)
	}

	if t == nil {
		log.Printf("No such template: %s/%s", o.path, o.name)
		g.WriteHeader(500)
		fmt.Fprintf(g, "Error: no such template: %s/%s", o.path, o.name)
		return
	}

	h := g.Header()
	if _, foundType := h["Content-Type"]; !foundType {
		h.Set("Content-Type", "text/html; charset=utf-8")
	}
	var w io.Writer
	if strings.Contains(g.Request.Header.Get("Accept-Encoding"), "gzip") {
		h.Set("Content-Encoding", "gzip")
		gz := gzip.NewWriter(g)
		defer gz.Close()

		w = io.Writer(gz)
	} else {
		w = g
	}

	if !partial && o.layouts != nil && len(o.layouts) > 0 {
		layouts := make([]*template.Template, len(o.layouts))

		// conceptually the layouts are arranged like this
		// [l1, l2, l3] t
		//  ^
		// execution starts at the beginning of the queue. l1 has a link via
		// the closure below to l(1+1) = l2, l2 has a link to l3, and l3 has a
		// link to t. Once the execution chain starts, each one will fire off
		// the next one until it reaches the end, at which point the main
		// content template is rendered. The layouts will then be rendered
		// outside-in with the main content last (innermost).

		// we need this func slice to properly close over the loop variables.
		// Otherwise the value of n would be the final value always. The layout
		// execution would then always skip all layouts after the first.
		funcs := make([]func(), len(o.layouts))

		for n, path := range o.layouts {
			if err := (func(i int) error {
				group, ok := Templates[path.path]
				if !ok {
					return fmt.Errorf("no such template path %s for layout %s", path.path, path.name)
				}
				layout := group.Lookup(path.name)
				if layout == nil {
					return fmt.Errorf("no such layout %s in path %s", path.name, path.path)
				}

				layouts[i] = layout

				// closure closes over:
				// - layouts slice so that it can access the next layout,
				// - w so that it can write a template with minimal buffering,
				// - i so that it knows its position,
				// - t to render the final content.

				funcs[i] = func() {
					f := func() (string, error) {
						// If this is the last layout in the queue, then do the
						// data instead. Then it'll stop "recursing" to this
						// closure.
						if i < len(funcs)-1 {
							funcs[i+1]()
							return "", layouts[i+1].Execute(w, o.data)
						}
						return "", t.Execute(w, o.data)
					}
					layout.Funcs(template.FuncMap{"content": f})
				}

				return nil
//.........这里部分代码省略.........
开发者ID:moshee,项目名称:gas,代码行数:101,代码来源:templates.go


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