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

runtime: unexpected render #910

Closed
kjk opened this issue Oct 19, 2021 · 2 comments
Closed

runtime: unexpected render #910

kjk opened this issue Oct 19, 2021 · 2 comments
Labels
runtime Related to the execution of VM templates Related to the template

Comments

@kjk
Copy link

kjk commented Oct 19, 2021

I might be doing something wrong but this doesn't work as I expect:

I have a macro:

{% macro licls(s1, s2 string) %}{% if s1 == s2 %}class="current"{% end %}{% end %}

I use it as:

<li {{licls(s, "home")}}>

The idea was that this renders to either nothing or to class="current".

Instead it renders as:

<li class��current�>

It looks like =" and " are garbled

Here's the repro:

func must(err error) {
	if err != nil {
		panic(err)
	}
}
func testOne() {
	navHTML := `{% macro licls(s1, s2 string) %}{% if s1 == s2 %}class='current'{% end %}{% end %}
	<li {{licls("home", "home")}}>
`
	fsys := scriggo.Files{
		"nav.html": []byte(navHTML),
	}
	template, err := scriggo.BuildTemplate(fsys, "nav.html", nil)
	must(err)
	err = template.Run(os.Stdout, nil, nil)
	must(err)
}
@gazerro
Copy link
Member

gazerro commented Oct 20, 2021

This is the expected behavior. Simplifying your example:

{% s := "class='current'" %}
<li {{ s }}>

this code is rendered as follows

<li class��current�>

A string rendered in a tag context can only contain attribute names. It is not safe to allow other characters.

Your example could be rewritten like this

{% licls := func(s1, s2 string) bool { return s1 == s2 } %}

<li {% if licls(s, "home") %}class="current"{% end %}>

@gazerro gazerro added the templates Related to the template label Oct 20, 2021
@gazerro
Copy link
Member

gazerro commented Oct 21, 2021

Close this issue. @kjk feel free to reopen this issue if you have any questions or comments.

@gazerro gazerro closed this as completed Oct 21, 2021
@gazerro gazerro changed the title unexpected render runtime: unexpected render Oct 21, 2021
@gazerro gazerro added the runtime Related to the execution of VM label Oct 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
runtime Related to the execution of VM templates Related to the template
Projects
None yet
Development

No branches or pull requests

2 participants