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


Golang Context.AsMap方法代码示例

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


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

示例1: Template

// Template is a template-based text formatter.
//
// This uses the core `text/template` to process a given string template.
//
// Params
// 	- template (string): A template string.
// 	- template.Context (bool): If true, the context will be placed into the
// 		template renderer as 'Cxt', and can be used as `{{.Cxt.Foo}}`. False
// 		by default.
// 	- ... (interface{}): Values passed into the template.
//
// Conventionally, template variables should start with an initial capital.
//
// Returns a formatted string.
func Template(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
	format := cookoo.GetString("template", "", p)
	withCxt := cookoo.GetBool("template.Context", false, p)

	name := fmt.Sprintf("%x", md5.Sum([]byte(format)))

	//c.Logf("debug", "Template %s is '%s'\n", name, format)

	tpl, err := template.New(name).Parse(format)
	if err != nil {
		return "", err
	}

	data := p.AsMap()
	if withCxt {
		//c.Logf("debug", "Adding context.")
		data["Cxt"] = c.AsMap()
	}

	var out bytes.Buffer
	if err := tpl.Execute(&out, data); err != nil {
		return "", err
	}

	return out.String(), nil
}
开发者ID:Crispy1975,项目名称:deis,代码行数:40,代码来源:fmt.go

示例2: RenderHTML

// RenderHTML renders an HTML template.
//
// This uses the `html/template` system built into Go to render data into a writer.
//
// Params:
// 	- template (required): An html/templates.Template object.
// 	- templateName (required): The name of the template to render.
// 	- values: An interface{} with the values to be passed to the template. If
// 	  this is not specified, the contents of the Context are passed as a map[string]interface{}.
// 	  Note that datasources, in this model, are not accessible to the template.
// 	- writer: The writer that data should be sent to. By default, this will create a new
// 	  Buffer and put it into the context. (If no Writer was passed in, the returned writer
// 	  is actually a bytes.Buffer.) To flush the contents directly to the client, you can
// 	  use `.Using('writer').From('http.ResponseWriter')`.
//
// Returns
// 	- An io.Writer. The template's contents have already been written into the writer.
//
// Example:
//
//	reg.Route("GET /html", "Test HTML").
//		Does(cookoo.AddToContext, "_").
//			Using("Title").WithDefault("Hello World").
//			Using("Body").WithDefault("This is the body.").
//		Does(web.RenderHTML, "render").
//			Using("template").From('cxt:templateCache').
//			Using("templateName").WithDefault("index.html").
//		Does(web.Flush, "_").
//			Using("contentType").WithDefault("text/html").
//			Using("content").From("cxt:render")
//
// In the example above, we do three things:
// 	- Add Title and Body to the context. For the template rendered, it will see these as
// 	  {{.Title}} and {{.Body}}.
// 	- Render the template located in a local file called "index.html". It is recommended that
// 	  a template.Template object be created at startup. This way, all of the templates can
// 	  be cached immediately and shared throughout processing.
// 	- Flush the result out to the client. This gives you a chance to add any additional headers.
func RenderHTML(cxt cookoo.Context, params *cookoo.Params) (interface{}, cookoo.Interrupt) {
	ok, missing := params.Requires("template", "templateName")
	if !ok {
		return nil, &cookoo.FatalError{"Missing params: " + strings.Join(missing, ", ")}
	}

	var buf bytes.Buffer
	out := params.Get("writer", &buf).(io.Writer)
	tplName := params.Get("templateName", nil).(string)
	tpl := params.Get("template", nil).(*template.Template)
	vals := params.Get("values", cxt.AsMap())

	err := tpl.ExecuteTemplate(out, tplName, vals)
	if err != nil {
		log.Printf("Recoverable error parsing template: %s", err)
		// XXX: This outputs partially completed templates. Is this what we want?
		io.WriteString(out, "Template error. The error has been logged.")
		return out, &cookoo.RecoverableError{"Template failed to completely render."}
	}
	return out, nil
}
开发者ID:Masterminds,项目名称:cookoo,代码行数:59,代码来源:commands.go


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