iris icon indicating copy to clipboard operation
iris copied to clipboard

can not build a new pongo2 tag or filter

Open huziu235 opened this issue 9 years ago • 6 comments

  1. can not port function use alias (view.Value,view.Parser,view.INodeTag...)
  2. can not implment INodeTag which is returned by TagParser(Execute in INodeTag has pongo2 params with no iris.view alias)

huziu235 avatar Sep 12 '17 02:09 huziu235

@huziu235 They are not type alias, you have to use the iris' pongo structs instead of the pongo's one (pongo2 is vendored so we couldn't do it without these iris' pongo's types).

Give me an example and I can show you how you can make it to work :)

hiveminded avatar Sep 12 '17 08:09 hiveminded

type assignTag struct {
	position *pongo2.Token
	args     []pongo2.IEvaluator
}

func (node *assignTag) Execute(ctx *pongo2.ExecutionContext, writer pongo2.TemplateWriter) (err *pongo2.Error) {

	if len(node.args) == 2 {

		name, err := node.args[0].Evaluate(ctx)
		if err != nil {
			return err
		}

		value, err := node.args[1].Evaluate(ctx)
		if err != nil {
			return err
		}

		if name.IsString() && value.IsString() {
			jstr := strings.TrimSpace(value.String())

			var val interface{} = nil

			start := jstr[0]
			end := jstr[len(jstr)-1]

			if start == '[' && end == ']' {
				val = make([]map[string]interface{}, 0, 3)
			} else if start == '{' && end == '}' {
				val = make(map[string]interface{}, 0)
			} else {
				val = jstr
			}

			err := json.Unmarshal([]byte(value.String()), &val)
			if err != nil {
				return ctx.Error("\nwrong json farmat:\n "+err.Error(), node.args[1].GetPositionToken())
			} else {
				ctx.Public[name.String()] = val
				return nil
			}
		}

	}

	return ctx.Error(fmt.Sprintf("\nwrong args, %d args\n", len(node.args)), node.args[0].GetPositionToken())
}

func assignTagParser(doc *pongo2.Parser, start *pongo2.Token, arguments *pongo2.Parser) (pongo2.INodeTag, *pongo2.Error) {
	tag := &assignTag{
		position: start,
	}

	for arguments.Remaining() > 0 {
		node, err := arguments.ParseExpression()
		if err != nil {
			return nil, err
		}
		tag.args = append(tag.args, node)
	}

	return tag, nil
}

Please help me, i have no clue.

huziu235 avatar Sep 13 '17 06:09 huziu235

@huziu235 It's simple at assignTagParser replace the *pongo2.X with the view.X.

import "github.com/kataras/iris/view"

// [...]

func assignTagParser(doc *view.Parser, start *view.Token, arguments *view.Parser) (view.INodeTag, *view.Error) {
	tag := &assignTag{
		position: start,
	}

	for arguments.Remaining() > 0 {
		node, err := arguments.ParseExpression()
		if err != nil {
			return nil, err
		}
		tag.args = append(tag.args, node)
	}

	return tag, nil
}

hiveminded avatar Sep 13 '17 08:09 hiveminded

it does not work. @hiveminded

type (
	Value pongo2.Value
	Error pongo2.Error
	FilterFunction func(in *Value, param *Value) (out *Value, err *Error)
	Parser pongo2.Parser
	Token pongo2.Token
	INodeTag pongo2.INodeTag
	TagParser func(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error)
)

view.Parser can not access ParseExpression directly. need cast to pongo2.Parser. But I can't do that right? Alias can access directly, type define can't. And the tag need implements view.INodeTag, it is not an empty interface. Do you have an exp. actually works?

huziu235 avatar Sep 14 '17 02:09 huziu235

import (
	"github.com/kataras/iris/view"
	"github.com/flosch/pongo2"
)

templates := iris.Django(cfg.Assets + "/templates", ".jinja2")
templates.AddFilter("test",
	func(in *view.Value, param *view.Value) (out *view.Value, err *view.Error) {
		out = (*view.Value)(pongo2.AsValue("test"))
		return
	},
)

I'm getting error: cannot convert "github.com/flosch/pongo2".AsValue("test") (type *"github.com/flosch/pongo2".Value) to type *view.Value.

Is there other way to make view.Value?

val := &view.Value{reflect.ValueOf("test"), false}

is not possible because: implicit assignment of unexported field 'val' in view.Value literal

mcsakoff avatar Feb 24 '18 20:02 mcsakoff

I suggest to add to view/django.go following wrappers:

func AsValue(i interface{}) *Value {
	return (*Value)(pongo2.AsValue(i))
}

func AsSafeValue(i interface{}) *Value {
	return (*Value)(pongo2.AsSafeValue(i))
}

mcsakoff avatar Feb 24 '18 20:02 mcsakoff