Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exec tag added to enable treating evaluated string as a part of the t… #1

Merged
merged 5 commits into from
Sep 21, 2022

Conversation

sprksh
Copy link
Collaborator

@sprksh sprksh commented Sep 20, 2022

…emplate, added tests

Description of the change

Idea is to provide users a functionality so that by using {% exec %} templatetag, they can evaluate a string based template (say passed as an argument) as a part of the original template.

For example user can define a macro in string and use that in the same execution flow.

Our use case was that user can define macros in yaml and then call it from the yaml itself and we should be able to treat the macro as one of the predefined macros. Earlier we were doing two step evaluation but that limited the capability of the system.

We could not define this templateTag from outside as it has to use few variables private to the package.

func test() {
	tpl, err := pongo2.FromFile("some_file.template")
	if err != nil {
		log.Fatal(err)
	}

	c1 := `{% macro double(arg) %}{{2*arg}}{% endmacro %}`
	c2 := "{{ double(10) }}"
	res, err := tpl.Execute(pongo2.Context{"c1": c1, "c2": c2})
	if err != nil {
		log.Fatal(err)
	}

}

some_file.template

{% exec %}{{c1}}{% endexec %}
{% exec %}{{c2}}{% endexec %}

so effectively this file will be evaluated as

{% macro double(arg) %}{{2*arg}}{% endmacro %}
{{ double(10) }}

and the result will be


20

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Related issues

Fix #1

Checklists

Development

  • Lint rules pass locally
  • The code changed/added as part of this pull request has been covered with tests
  • All tests related to the changed code pass in development

Code review

  • This pull request has a descriptive title and information useful to a reviewer. There may be a screenshot or screencast attached
  • "Ready for review" label attached to the PR and reviewers mentioned in a comment
  • Changes have been reviewed by at least one other engineer
  • Issue from task tracker has a link to this pull request

@gane5hvarma
Copy link

There is a typo in description. instead of c2 its c1

{% exec %}{{c1}}{% endexec %}
{% exec %}{{c2}}{% endexec %}

tags_exec.go Outdated
}
templateSet := ctx.template.set
s := temp.String()
currentTemplate, err2 := templateSet.FromString(s)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not need to have differrent name for err. you can use as err instead of err2

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue is that err and err2 are of different types.. err is basically pongo2.Error. thats why it is causing type error

tags_exec.go Outdated
}
templateSet := ctx.template.set
s := temp.String()
currentTemplate, err2 := templateSet.FromString(s)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
currentTemplate, err2 := templateSet.FromString(s)
tempBytes := temp.Bytes()
currentTemplate, err := templateSet.FromBytes(tempBytes)

Change is because FromString converts string to bytes. Using FromBytes will not do conversion from temp to tempString(temp.String()) and tempString to bytes again(inside FromString)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@sprksh sprksh merged commit 38596d1 into master Sep 21, 2022
ShisuiMadara pushed a commit that referenced this pull request Apr 11, 2023
#1)

* 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 <sp@Suryas-MacBook-Pro.local>
shubhammehra4 pushed a commit that referenced this pull request Nov 3, 2023
#1)

* 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 <sp@Suryas-MacBook-Pro.local>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants