mirror of
https://github.com/danog/liquid.git
synced 2024-12-02 12:17:48 +01:00
Add public DefineTag
This commit is contained in:
parent
70aa70d044
commit
e21d2a736d
@ -18,10 +18,15 @@ func assignTagDef(source string) (func(io.Writer, Context) error, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindTagDefinition(name string) (TagDefinition, bool) {
|
var tagDefinitions = map[string]TagDefinition{
|
||||||
switch name {
|
"assign": assignTagDef,
|
||||||
case "assign":
|
}
|
||||||
return assignTagDef, true
|
|
||||||
}
|
func DefineTag(name string, td TagDefinition) {
|
||||||
return nil, false
|
tagDefinitions[name] = td
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindTagDefinition(name string) (TagDefinition, bool) {
|
||||||
|
td, ok := tagDefinitions[name]
|
||||||
|
return td, ok
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ It's intended for use in for use in https://github.com/osteele/gojekyll.
|
|||||||
package liquid
|
package liquid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"io"
|
||||||
|
|
||||||
"github.com/osteele/liquid/chunks"
|
"github.com/osteele/liquid/chunks"
|
||||||
)
|
)
|
||||||
@ -15,18 +15,14 @@ import (
|
|||||||
//
|
//
|
||||||
// In the future, it will be configured with additional tags, filters, and the {%include%} search path.
|
// In the future, it will be configured with additional tags, filters, and the {%include%} search path.
|
||||||
type Engine interface {
|
type Engine interface {
|
||||||
|
DefineTag(string, func(form string) (func(io.Writer, chunks.Context) error, error))
|
||||||
|
|
||||||
ParseTemplate(text []byte) (Template, error)
|
ParseTemplate(text []byte) (Template, error)
|
||||||
ParseAndRender(text []byte, scope map[string]interface{}) ([]byte, error)
|
ParseAndRender(text []byte, scope map[string]interface{}) ([]byte, error)
|
||||||
ParseAndRenderString(text string, scope map[string]interface{}) (string, error)
|
ParseAndRenderString(text string, scope map[string]interface{}) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Template renders a template according to scope.
|
type TagDefinition func(expr string) (func(io.Writer, chunks.Context) error, error)
|
||||||
//
|
|
||||||
// Scope is a map of liquid variable names to objects.
|
|
||||||
type Template interface {
|
|
||||||
Render(scope map[string]interface{}) ([]byte, error)
|
|
||||||
RenderString(scope map[string]interface{}) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type engine struct{}
|
type engine struct{}
|
||||||
|
|
||||||
@ -39,6 +35,10 @@ func NewEngine() Engine {
|
|||||||
return engine{}
|
return engine{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e engine) DefineTag(name string, td func(form string) (func(io.Writer, chunks.Context) error, error)) {
|
||||||
|
chunks.DefineTag(name, chunks.TagDefinition(td))
|
||||||
|
}
|
||||||
|
|
||||||
func (e engine) ParseTemplate(text []byte) (Template, error) {
|
func (e engine) ParseTemplate(text []byte) (Template, error) {
|
||||||
tokens := chunks.Scan(string(text), "")
|
tokens := chunks.Scan(string(text), "")
|
||||||
ast, err := chunks.Parse(tokens)
|
ast, err := chunks.Parse(tokens)
|
||||||
@ -66,22 +66,3 @@ func (e engine) ParseAndRenderString(text string, scope map[string]interface{})
|
|||||||
}
|
}
|
||||||
return string(b), nil
|
return string(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render applies the template to the scope.
|
|
||||||
func (t *template) Render(scope map[string]interface{}) ([]byte, error) {
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
err := t.ast.Render(buf, chunks.NewContext(scope))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RenderString is a convenience wrapper for Render, that has string input and output.
|
|
||||||
func (t *template) RenderString(scope map[string]interface{}) (string, error) {
|
|
||||||
b, err := t.Render(scope)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return string(b), nil
|
|
||||||
}
|
|
34
template.go
Normal file
34
template.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package liquid
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
|
||||||
|
"github.com/osteele/liquid/chunks"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Template renders a template according to scope.
|
||||||
|
//
|
||||||
|
// Scope is a map of liquid variable names to objects.
|
||||||
|
type Template interface {
|
||||||
|
Render(scope map[string]interface{}) ([]byte, error)
|
||||||
|
RenderString(scope map[string]interface{}) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Render applies the template to the scope.
|
||||||
|
func (t *template) Render(scope map[string]interface{}) ([]byte, error) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
err := t.ast.Render(buf, chunks.NewContext(scope))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RenderString is a convenience wrapper for Render, that has string input and output.
|
||||||
|
func (t *template) RenderString(scope map[string]interface{}) (string, error) {
|
||||||
|
b, err := t.Render(scope)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(b), nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user