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

Using cyclic refs in <script> kills hugo #1123

Closed
rauhs opened this issue May 10, 2015 · 10 comments
Closed

Using cyclic refs in <script> kills hugo #1123

rauhs opened this issue May 10, 2015 · 10 comments

Comments

@rauhs
Copy link
Contributor

rauhs commented May 10, 2015

For instance using {{.Site.Sections}} in a <script> will kill hugo with a stackoverflow. Presumably, golang wants to convert the variable to JSON due to the context and this fails.

What is the best way to pass down Hugo variables to javascript?

@bep
Copy link
Member

bep commented May 10, 2015

Not sure what you expected to be rendered from the map {{.Site.Sections}}.

Yes, it looks like Golang's template engine tries to convert data inside <script> to JSON. Any bugs/issues in this area should be reported at https://github.com/golang/go/issues

You can tell it to not touch some value by using the safeHtmlfunc, but in your case it's probably smart to start by pulling the values you need from the container:

<script>
    {{ range $section, $pages := .Site.Sections }}
    {{ $section }}
    {{ end }}
</script>

@rauhs
Copy link
Contributor Author

rauhs commented May 10, 2015

Well I certainly wouldn't expect hugo to die :)

But the above code works well enough for my use case so feel free to close this.

Thanks

@bep
Copy link
Member

bep commented May 10, 2015

If the Go template handler panics, then it's a bug that should be fixed -- by the Go team. Panic is a good way to signal these issues -- very visible.

It doesn't panic in Go 1.4.2 on Linux, btw.

@bep bep closed this as completed May 10, 2015
@bep
Copy link
Member

bep commented May 10, 2015

@rauhs I created this issue over at Go:

golang/go#10769

I'll reopen this issue to keep track of that one.

@bep bep reopened this May 10, 2015
@rauhs
Copy link
Contributor Author

rauhs commented May 10, 2015

Thanks for doing the work. I'd certainly make an effort myself usually but I'm swamped with work and not too familiar with go.
Much appreciated.

@bep bep changed the title Using variables in <script> kills hugo Using cyclic refs in <script> kills hugo May 10, 2015
@adg
Copy link
Contributor

adg commented May 10, 2015

On the Go issue @bep says:

The Go program in question is Hugo, a static site generator. It uses Go templates. In the template context is the Site. Site has a list of Pages; each Page has a reference to the Site it belongs to.
...from Hugo's point of view, to fix this, we must remove the circular reference from Page (and maybe some other), but that would make the software useless.

Why do the Pages have an exported reference to the Site they belong to? Is that actually necessary? You say it would "make the software useless" but I don't follow why this is true.

@bep
Copy link
Member

bep commented May 10, 2015

Let me rephrase: This object graph was constructed by @spf13 long before I entered the picture. It may be so (I haven't thought too hard about it) that the software could live well without it -- but removing it would be a major and ground-breaking API change.

I actually like bi-directional object graphs -- and I must admit it, I was surprised to learn that <script>...</script> did automatic JSON-encoding of non-string objects. A JSONEncode template func might have been more transparent. But that would maybe be too big of an API-change ...

@adg
Copy link
Contributor

adg commented May 11, 2015

I like bi-directional object graphs too, but I don't usually use them for display. Generally like to create copies of my data in template-specific data structures when using html/template.

@spf13
Copy link
Contributor

spf13 commented May 12, 2015

The site is actually a pared down copy of the actual site object used only for display however it does contain references to all of the pages. Hugo needs this as each page needs to be able to access other pages on the site for a variety of features (related pages, menus, etc). We could do this with all copy vs references but it would get exponentially more expensive the larger the site is. I think it's possible that we could create a mostly duplicate but pared down page struct that doesn't include the reference back to the site and embed these within the site, this would solve the circular reference but would create a lot of overhead and complexity. Yes we do this with the site, but we only have 1 site instance vs N page instances.

@spf13 spf13 added the Bug label May 12, 2015
@bep bep closed this as completed in be7c3bb May 23, 2015
tychoish pushed a commit to tychoish/hugo that referenced this issue Aug 13, 2017
Note that this commit makes no promise about great JSON output from the encoder, but the cyclic refs should be broken.

Fixes gohugoio#1123
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants