From 38596d191a13afeb4b132b0a497ced536ac31afe Mon Sep 17 00:00:00 2001 From: Surya Prakash Date: Wed, 21 Sep 2022 17:39:07 +0530 Subject: [PATCH] =?UTF-8?q?exec=20tag=20added=20to=20enable=20treating=20e?= =?UTF-8?q?valuated=20string=20as=20a=20part=20of=20the=20t=E2=80=A6=20(#1?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * exec tag added to enable treating evaluated string as a part of the template, added tests * added logging and comments * minor change for context creation * fixed tests, incorporated changes from pr * removed unnecessary type conversion Co-authored-by: Surya Prakash --- docs/tags.md | 1 + tags_exec.go | 44 +++++++++++++++++++++++++++++++++++++ template_tests/exec.helper | 1 + template_tests/exec.tpl | 13 +++++++++++ template_tests/exec.tpl.out | 13 +++++++++++ 5 files changed, 72 insertions(+) create mode 100644 tags_exec.go create mode 100644 template_tests/exec.helper create mode 100644 template_tests/exec.tpl create mode 100644 template_tests/exec.tpl.out diff --git a/docs/tags.md b/docs/tags.md index dae4566..07d357e 100644 --- a/docs/tags.md +++ b/docs/tags.md @@ -10,6 +10,7 @@ Implemented tags so far which needs documentation: * comment * cycle * extends +* exec * filter * firstof * for diff --git a/tags_exec.go b/tags_exec.go new file mode 100644 index 0000000..b88fd89 --- /dev/null +++ b/tags_exec.go @@ -0,0 +1,44 @@ +package pongo2 + +import ( + "bytes" +) + +type tagExecNode struct { + position *Token + bodyWrapper *NodeWrapper +} + +func (node *tagExecNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error { + temp := bytes.NewBuffer(make([]byte, 0, 1024)) // 1 KiB size + + err := node.bodyWrapper.Execute(ctx, temp) + if err != nil { + return err + } + templateSet := ctx.template.set + currentTemplate, err2 := templateSet.FromBytes(temp.Bytes()) + if err2 != nil { + return err2.(*Error) + } + currentTemplate.root.Execute(ctx, writer) + return nil +} + +func tagExecuteParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) { + execNode := &tagExecNode{ + position: start, + } + + wrapper, _, err := doc.WrapUntilTag("endexec") + if err != nil { + return nil, err + } + execNode.bodyWrapper = wrapper + + return execNode, nil +} + +func init() { + RegisterTag("exec", tagExecuteParser) +} diff --git a/template_tests/exec.helper b/template_tests/exec.helper new file mode 100644 index 0000000..a94e968 --- /dev/null +++ b/template_tests/exec.helper @@ -0,0 +1 @@ +{% macro m4(arg) export %}{{arg}}{% endmacro %} \ No newline at end of file diff --git a/template_tests/exec.tpl b/template_tests/exec.tpl new file mode 100644 index 0000000..ee77dd2 --- /dev/null +++ b/template_tests/exec.tpl @@ -0,0 +1,13 @@ +{% with val = "{{ simple.name }}" %}{% exec %}{{val}}{% endexec %}{% endwith %} + +{% exec %}{% macro m1() %}some text{% endmacro %}{% endexec %}{{m1()}} + +{% exec %}{% autoescape off %}{% macro m2(arg) export %}some text with {{arg}}{% endmacro %}{% endautoescape %}{% endexec %}{{m2("arg value")}} + +{% with val = "{% macro m3() export %}m3 text{% endmacro %}{{m3()}}" %}{% exec %}{{val}}{% endexec %}{% endwith %} + +{% with val = '{% import "template_tests/exec.helper" m4 %}' %}{% exec %}{% autoescape off %}{{val}}{% endautoescape %}{% endexec %}{{m4("arg value")}}{% endwith %} + +{% exec %}{% macro m1() export %}some text{% endmacro %}{% endexec %}{{m1()}} + +{% with chars = "abc"|make_list val = "{% for i in chars %}{{i}}{% endfor %}" %}{% exec %}{{val}}{% endexec %}{% endwith %} \ No newline at end of file diff --git a/template_tests/exec.tpl.out b/template_tests/exec.tpl.out new file mode 100644 index 0000000..b5eb292 --- /dev/null +++ b/template_tests/exec.tpl.out @@ -0,0 +1,13 @@ +john doe + +some text + +some text with arg value + +m3 text + +arg value + +some text + +abc \ No newline at end of file