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


Golang pongo2.FromString函数代码示例

本文整理汇总了Golang中github.com/flosch/pongo2.FromString函数的典型用法代码示例。如果您正苦于以下问题:Golang FromString函数的具体用法?Golang FromString怎么用?Golang FromString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: parseNode

func parseNode(value interface{}) (MapNode, error) {
	switch v := value.(type) {
	case string:
		t, err := pongo2.FromString(v)
		if err != nil {
			return nil, err
		}
		return &mapNodeTemplate{template: t}, nil
	case map[string]string:
		res := make(Map)
		for key, mvalue := range v {
			t, err := pongo2.FromString(mvalue)
			if err != nil {
				return nil, err
			}
			res[key] = &mapNodeTemplate{template: t}
		}
		return &mapNodeMap{nodes: res}, nil
	case map[string]interface{}:
		res, err := ParseMap(Context(v))
		if err != nil {
			return nil, err
		}
		return &mapNodeMap{nodes: res}, nil
	case Context:
		res, err := ParseMap(v)
		if err != nil {
			return nil, err
		}
		return &mapNodeMap{nodes: res}, nil
	case map[interface{}]interface{}:
		res := make(Map)
		for k, mvalue := range v {
			key, ok := k.(string)
			if !ok {
				return nil, fmt.Errorf("Invalid key %#v in templates map. All keys must be strings.", k)
			}
			n, err := parseNode(mvalue)
			if err != nil {
				return nil, err
			}
			res[key] = n
		}
		return &mapNodeMap{nodes: res}, nil
	case []interface{}:
		res := new(mapNodeSlice)
		for _, svalue := range v {
			n, err := parseNode(svalue)
			if err != nil {
				return nil, err
			}
			res.nodes = append(res.nodes, n)
		}
		return res, nil
	}
	return &mapNodeInterface{value: value}, nil
}
开发者ID:crackcomm,项目名称:renderer,代码行数:57,代码来源:map.go

示例2: renderVarsHelper

func renderVarsHelper(varsMap VarsMap, ctxt pongo2.Context) error {
	for key, value := range varsMap {
		switch v := value.(type) {
		case map[string]interface{}:
			if err := renderVarsHelper(varsMap, ctxt); err != nil {
				return HenchErr(err, map[string]interface{}{
					"value_map": value,
					"solution":  "Refer to wiki for proper pongo2 formatting",
				}, "While templating")
			}
		case string:
			tmpl, err := pongo2.FromString(v)
			if err != nil {
				return HenchErr(err, map[string]interface{}{
					"value":    value,
					"solution": "Refer to wiki for proper pongo2 formatting",
				}, "While templating")
			}
			out, err := tmpl.Execute(ctxt)
			if err != nil {
				return HenchErr(err, map[string]interface{}{
					"value":    value,
					"context":  ctxt,
					"solution": "Refer to wiki for proper pongo2 formatting",
				}, "While executing")
			}
			varsMap[key] = out
		}
	}

	return nil
}
开发者ID:apigee,项目名称:henchman,代码行数:32,代码来源:task.go

示例3: TestImplicitExecCtx

func (s *TestSuite) TestImplicitExecCtx(c *C) {
	tpl, err := pongo2.FromString("{{ ImplicitExec }}")
	if err != nil {
		c.Fatalf("Error in FromString: %v", err)
	}

	val := "a stringy thing"

	res, err := tpl.Execute(pongo2.Context{
		"Value": val,
		"ImplicitExec": func(ctx *pongo2.ExecutionContext) string {
			return ctx.Public["Value"].(string)
		},
	})

	if err != nil {
		c.Fatalf("Error executing template: %v", err)
	}

	c.Check(res, Equals, val)

	// The implicit ctx should not be persisted from call-to-call
	res, err = tpl.Execute(pongo2.Context{
		"ImplicitExec": func() string {
			return val
		},
	})

	if err != nil {
		c.Fatalf("Error executing template: %v", err)
	}

	c.Check(res, Equals, val)
}
开发者ID:vodka-contrib,项目名称:pongor,代码行数:34,代码来源:pongo2_test.go

示例4: PrepareTasks

// change this to be a non-plan function
// come back to context variable stuff after getting include done
// look at diagram
// renders Task list, uses vars and machine for context
func PrepareTasks(tasks []*Task, vars TaskVars, machine Machine) ([]*Task, error) {
	// changes Task array back to yaml form to be rendered
	var newTasks = []*Task{}
	tasksBuf, err := yaml.Marshal(&tasks)
	if err != nil {
		return nil, err
	}

	// convert tasks to a pongo2 template
	tmpl, err := pongo2.FromString(string(tasksBuf))
	if err != nil {
		return nil, err
	}

	// add context and render
	ctxt := pongo2.Context{"vars": vars, "machine": machine}
	out, err := tmpl.Execute(ctxt)
	if err != nil {
		return nil, err
	}

	// change the newly rendered yaml format array back to a struct
	err = yaml.Unmarshal([]byte(out), &newTasks)
	if err != nil {
		return nil, err
	}

	return newTasks, nil
}
开发者ID:jlin21,项目名称:henchman,代码行数:33,代码来源:plan.go

示例5: arcPoll

func (c *IFMapClient) arcPoll(callback PollCallback) error {
	tpl, err := pongo2.FromString(ifmapPollRequest)
	if err != nil {
		return err
	}
	requestString, err := tpl.Execute(pongo2.Context{"session_id": c.sessionID})
	if err != nil {
		return err
	}
	err = c.request(c.arc, "poll", requestString)
	if err != nil {
		return err
	}

	output, err := response(c.arc)
	if err != nil {
		return err
	}
	fmt.Println()
	e := PollResult{}
	err = xml.Unmarshal([]byte(output), &e)
	for _, resultItem := range e.SearchItems {
		callback.Search(resultItem.toMap())
	}
	for _, resultItem := range e.UpdateItems {
		callback.Update(resultItem.toMap())
	}
	for _, resultItem := range e.DeleteItems {
		callback.Delete(resultItem.toMap())
	}

	return nil
}
开发者ID:nati,项目名称:goifmap,代码行数:33,代码来源:client.go

示例6: FromString

// FromString - Creates a new template structure from string.
func FromString(body string) (t Template, err error) {
	template, err := pongo2.FromString(body)
	if err != nil {
		return
	}
	return &pongoTemplate{Template: template}, nil
}
开发者ID:crackcomm,项目名称:renderer,代码行数:8,代码来源:pongo.go

示例7: prepareTemplate

func prepareTemplate(data string, vars *TaskVars, machine *Machine) (string, error) {
	tmpl, err := pongo2.FromString(data)
	if err != nil {
		panic(err)
	}
	ctxt := pongo2.Context{"vars": vars, "machine": machine}
	return tmpl.Execute(ctxt)
}
开发者ID:sharadgana,项目名称:henchman,代码行数:8,代码来源:task.go

示例8: executeTemplate

func executeTemplate(template string, vars PlanVars) (string, error) {
	tmpl, err := pongo2.FromString(template)
	if err != nil {
		return "", err
	}

	return tmpl.Execute(pongo2.Context(vars))
}
开发者ID:snowsnail,项目名称:hotomata,代码行数:8,代码来源:template.go

示例9: TestExecutionErrors

func TestExecutionErrors(t *testing.T) {
	//debug = true

	matches, err := filepath.Glob("./template_tests/*-execution.err")
	if err != nil {
		t.Fatal(err)
	}
	for idx, match := range matches {
		t.Logf("[Errors %3d] Testing '%s'", idx+1, match)

		testData, err := ioutil.ReadFile(match)
		tests := strings.Split(string(testData), "\n")

		checkFilename := fmt.Sprintf("%s.out", match)
		checkData, err := ioutil.ReadFile(checkFilename)
		if err != nil {
			t.Fatalf("Error on ReadFile('%s'): %s", checkFilename, err.Error())
		}
		checks := strings.Split(string(checkData), "\n")

		if len(checks) != len(tests) {
			t.Fatal("Template lines != Checks lines")
		}

		for idx, test := range tests {
			if strings.TrimSpace(test) == "" {
				continue
			}
			if strings.TrimSpace(checks[idx]) == "" {
				t.Fatalf("[%s Line %d] Check is empty (must contain an regular expression).",
					match, idx+1)
			}

			tpl, err := pongo2.FromString(test)
			if err != nil {
				t.Fatalf("Error on FromString('%s'): %s", test, err.Error())
			}

			tpl, err = pongo2.FromBytes([]byte(test))
			if err != nil {
				t.Fatalf("Error on FromBytes('%s'): %s", test, err.Error())
			}

			_, err = tpl.ExecuteBytes(tplContext)
			if err == nil {
				t.Fatalf("[%s Line %d] Expected error for (got none): %s",
					match, idx+1, tests[idx])
			}

			re := regexp.MustCompile(fmt.Sprintf("^%s$", checks[idx]))
			if !re.MatchString(err.Error()) {
				t.Fatalf("[%s Line %d] Error for '%s' (err = '%s') does not match the (regexp-)check: %s",
					match, idx+1, test, err.Error(), checks[idx])
			}
		}
	}
}
开发者ID:vodka-contrib,项目名称:pongor,代码行数:57,代码来源:pongo2_template_test.go

示例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)
}
开发者ID:Luzifer,项目名称:ipfs-markdown,代码行数:10,代码来源:main.go

示例11: GenerateCustomPath

//GenerateCustomPath - returns custom path based on sync_key_template
func (schema *Schema) GenerateCustomPath(data map[string]interface{}) (path string, err error) {
	syncKeyTemplate, ok := schema.SyncKeyTemplate()
	if !ok {
		err = fmt.Errorf("Failed to read sync_key_template from schema %v", schema.URL)
		return
	}
	tpl, err := pongo2.FromString(syncKeyTemplate)
	if err != nil {
		return
	}
	path, err = tpl.Execute(pongo2.Context{}.Update(data))
	return
}
开发者ID:vozhyk-,项目名称:gohan,代码行数:14,代码来源:schema.go

示例12: htmlHandler

func htmlHandler(res http.ResponseWriter, r *http.Request) {
	tplsrc, _ := Asset("display.html")

	template, err := pongo2.FromString(string(tplsrc))
	if err != nil {
		log.Fatal(err)
	}

	template.ExecuteWriter(pongo2.Context{
		"results":                probeMonitors,
		"certificateOK":          certificateOK,
		"certificateExpiresSoon": certificateExpiresSoon,
		"version":                version,
	}, res)
}
开发者ID:Luzifer,项目名称:promcertcheck,代码行数:15,代码来源:http.go

示例13: pixieInit

// Builds pxe config to be sent to pixiecore
func (m Machine) pixieInit(config Config) (PixieConfig, error) {
	pixieConfig := PixieConfig{}
	tpl, err := pongo2.FromString(defaultString(m.Cmdline, config.DefaultCmdline))
	if err != nil {
		return pixieConfig, err
	}
	cmdline, err := tpl.Execute(pongo2.Context{"BaseURL": config.BaseURL, "Hostname": m.Hostname, "Token": m.Token})
	if err != nil {
		return pixieConfig, err
	}
	pixieConfig.Kernel = defaultString(m.ImageURL+m.Kernel, config.DefaultImageURL+config.DefaultKernel)
	pixieConfig.Initrd = []string{defaultString(m.ImageURL+m.Initrd, config.DefaultImageURL+config.DefaultInitrd)}
	pixieConfig.Cmdline = cmdline
	return pixieConfig, nil
}
开发者ID:totallyunknown,项目名称:waitron,代码行数:16,代码来源:machine.go

示例14: renderValue

func (task *Task) renderValue(value string) (string, error) {
	tmpl, err := pongo2.FromString(value)
	if err != nil {
		log.Println("tmpl error")
		return "", err
	}
	// NOTE: add an update context when regMap is passed in
	ctxt := pongo2.Context{"vars": task.Vars} //, "machine": machine}
	out, err := tmpl.Execute(ctxt)
	if err != nil {
		log.Println("execute error")
		return "", err
	}
	return out, nil

}
开发者ID:rmenn,项目名称:henchman,代码行数:16,代码来源:task.go

示例15: doTemplate

func doTemplate(c *cli.Context) {
	template := c.String("template")
	manager := schema.GetManager()
	configFile := c.String("config-file")
	config := util.GetConfig()
	err := config.ReadConfig(configFile)
	if err != nil {
		util.ExitFatal(err)
		return
	}
	templateCode, err := util.GetContent(template)
	if err != nil {
		util.ExitFatal(err)
		return
	}
	pwd, _ := os.Getwd()
	os.Chdir(path.Dir(configFile))
	schemaFiles := config.GetStringList("schemas", nil)
	if schemaFiles == nil {
		util.ExitFatal("No schema specified in configuraion")
	} else {
		err = manager.LoadSchemasFromFiles(schemaFiles...)
		if err != nil {
			util.ExitFatal(err)
			return
		}
	}
	schemas := manager.OrderedSchemas()

	if err != nil {
		util.ExitFatal(err)
		return
	}
	tpl, err := pongo2.FromString(string(templateCode))
	if err != nil {
		util.ExitFatal(err)
		return
	}
	output, err := tpl.Execute(pongo2.Context{"schemas": schemas})
	if err != nil {
		util.ExitFatal(err)
		return
	}
	os.Chdir(pwd)
	fmt.Println(output)
}
开发者ID:vozhyk-,项目名称:gohan,代码行数:46,代码来源:template.go


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