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… #310

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sprksh
Copy link

@sprksh sprksh commented Sep 21, 2022

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 a string and use that in the same execution flow.

Our use case was that the 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

#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>
@sonarcloud
Copy link

sonarcloud bot commented Sep 21, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sprksh
Copy link
Author

sprksh commented Sep 26, 2022

@flosch please let me know your thoughts about the added functionality :)

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.

None yet

1 participant