本文整理匯總了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
}
示例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
}
示例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)
}
示例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
}
示例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
}
示例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
}
示例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)
}
示例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))
}
示例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])
}
}
}
}
示例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)
}
示例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
}
示例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)
}
示例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
}
示例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
}
示例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)
}