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

Consider empty strings in Param() as "not found" #3366

Closed
bep opened this issue Apr 19, 2017 · 18 comments
Closed

Consider empty strings in Param() as "not found" #3366

bep opened this issue Apr 19, 2017 · 18 comments

Comments

@bep
Copy link
Member

bep commented Apr 19, 2017

I'm not totally sure about this, but @budparr has a theme where he in front matter (for archetype reasons) have:

- featured_image: ""

And when doing

{{ .Param "featured_image" }}

You will not get the desired effect of getting the image defined in site config. May be something to think about, but we should maybe fix archetypes to allow comments. Keep this as a reminder for a while ...

@moorereason
Copy link
Contributor

My initial reaction is to leave Param the way it is now.

What's the expected behavior in Bud's case? Why can't we use {{ with .Param "featured_image" }}?

@rdwatters
Copy link
Contributor

What's the expected behavior in Bud's case?

I was under the impression that it's sort of the equivalent of the following from the docs:

{{ .Params.featured_image | default .Site.Params.featured_image }}

@bep
Copy link
Member Author

bep commented Apr 19, 2017

@rdwatters is correct; his construction would work with less confusion in this case, but then again, having empty params scattered around seems like a messy ... mess, too, so.

@budparr
Copy link

budparr commented Apr 19, 2017

It's true that the default only works when the field is gone completely, but it's inevitable that those empty params are going to be there when using an archetype.

@bep
Copy link
Member Author

bep commented Apr 19, 2017

but it's inevitable that those empty params are going to be there when using an archetype.

It's not inevitable. You could just ... delete it, but I get your point.

@fj may have some weight on this, as he reworked these methods some time ago.

@budparr
Copy link

budparr commented Apr 19, 2017

Not to be argumentative, but the way I set up my sites (and the way I think others will be too in many cases) is to set up archetypes to be clues to an editor like forestry.io as to what fields to show in their interface. So, having those "fields" there, empty or no, is important. Every page needs to have a consistent set of fields whether or not any given field is being used.

@rdwatters
Copy link
Contributor

rdwatters commented Apr 19, 2017

So, having those "fields" there, empty or no, is important. Every page needs to have a consistent set of fields whether or not any given field is being used.

Agreed. I find I always put the empty string for the value since the generator, if my memory serves, will otherwise insert null.

@bep
Copy link
Member Author

bep commented Apr 19, 2017

A cleaner solution may be to not put empty strings into the params map when we read the front matter.

@rdwatters
Copy link
Contributor

How crazy does it sound to beef up the default function instead since it's a more intuitive syntax and also used in other templating languages?

For example, allowing just a default declaration (i.e., without a passed value) to point to the respective .Params in config?

@moorereason
Copy link
Contributor

{{ .Params.featured_image | default .Site.Params.featured_image }} should work as expected when defined but empty. @budparr, is this your use case?

@bep, if we don't put the keys in the map, will .Params.featured_image give an index error?

Having empty strings in the Params map is fine with me. Can we detect the difference between key: and key: "" (nil vs ""). If I put key: "" in my front matter, I would expect to find it in the Params map as an empty string.

@budparr
Copy link

budparr commented Apr 19, 2017

@moorereason Yes. I just tested that and it works.

@stale
Copy link

stale bot commented Dec 6, 2017

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@stale stale bot added the Stale label Dec 6, 2017
@moorereason
Copy link
Contributor

@bep, I think we can close this issue. The solution is to use

featured_image:

instead of

featured_image: ""

In that case, the {{ .Params.featured_image | default .Site.Params.featured_image }} construct works as expected.

@stale stale bot removed the Stale label Dec 8, 2017
@bep
Copy link
Member Author

bep commented Dec 8, 2017

In that case, the {{ .Params.featured_image | default .Site.Params.featured_image }} construct works as expected.

Not sure how that applies to this particular issue?

@jmooring
Copy link
Member

I would like to close this issue. The .Param method on .Page was designed to return the value associated with a given key, regardless of whether the value is truthy or falsy.

To ignore falsy values, use this construct instead:

{{ or .Params.foo site.Params.foo }}

I recently updated https://gohugo.io/functions/param/, including the example above.

@bep
Copy link
Member Author

bep commented Jan 11, 2023

I would like to close this issue. The .Param method on .Page was designed to return the value associated with a given key, regardless of whether the value is truthy or falsy.

No, the .Param was designed to return the value in Page front matter for a given key if set, else fall back to the site.Params value.

@bep bep closed this as completed Jan 11, 2023
@jmooring
Copy link
Member

I'm not sure where the disagreement is. Please let me know if the updated documentation is incorrect:
< https://gohugo.io/functions/param/>

@github-actions
Copy link

github-actions bot commented Feb 2, 2023

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 Feb 2, 2023
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

5 participants