I have the following output:
[“Apple”][“Banana”][“Orange”]
I need to format the output so it looks like this:
[“Apple”, “Banana”, “Orange”]
Suggestions?
I have the following output:
[“Apple”][“Banana”][“Orange”]
I need to format the output so it looks like this:
[“Apple”, “Banana”, “Orange”]
Suggestions?
On the off chance this is not some new kind of trolling, you’ll need to supply more information about what you’re trying to do with Hugo, for anyone to help.
No trolling. A genuine question. I thought the info provided would suffice.
I came across this post with the following code snippet: Exclude term from being displayed in array
{{ $exclude := slice "London" "Berlin" }}
{{ range .Params.locations }}
{{ $location := . }}
{{ with not (in $exclude . ) }}
<li>
<a href="{{ $location | urlize }}" title="{{ $location }}">{{ $location }}</a>
</li>
{{ end }}
{{ end }}
I have adapted it to my project, like so:
{{range where .Site.RegularPages "Type" "not in" (slice "catalog) }}
...
...
{{ $exclude := slice "London" "Berlin" }}
{{ range .Params.locations }}
{{ $location := . }}
{{ with not (in $exclude . ) }}
{{ $location }}
{{ end }}
{{ end }}
{{end }}
.Params.locations
is an array, like so ["New York", "London"]
I’ve managed to sort of to get this to work by using scratch. This is what I have right now:
{{range where .Site.RegularPages "Type" "not in" (slice "catalog) }}
...
...
{{ $exclude := slice "London" "Berlin" }}
{{ range .Params.locations }}
{{ $location := . }}
{{ with not (in $exclude . ) }}
{{$.Scratch.Add "test" (slice $keyword) }}
{{end}}
{{end}}
(($.Scratch.Get "test")) | jsonify }},
{{end }}
This gives the output I’m looking for: [“Apple”, “Banana”, “Orange”]
However, I don’t know how to reset Scratch so it keeps on adding to the output in the loop.
Example:
item 1: [“Apple”, “Banana”, “Orange”] <— Correct
item 2: [“Apple”, “Banana”, “Orange”, “Kiwi”] <-- Incorrect. This should only be Kiwi.
Suggestions?
Using the Add method will never reset Scratch indeed. You need to use Set for this:
{{ $.Scratch.Set "test" "" }}
If I add {{ $.Scratch.Set "test" "" }}
, like so:
{{range where .Site.RegularPages "Type" "not in" (slice "catalog) }}
...
...
{{ $exclude := slice "London" "Berlin" }}
{{ $.Scratch.Set "test" "" }
{{ range .Params.locations }}
{{ $location := . }}
{{ with not (in $exclude . ) }}
{{$.Scratch.Add "test" (slice $keyword) }}
{{end}}
{{end}}
(($.Scratch.Get "test")) | jsonify }},
{{end }}
I get the error message:
executing "index.json" at <$.Scratch.Add>: error calling Add: Can't apply the operator to the values.
If I replace Add with Set, like so:
{{range where .Site.RegularPages "Type" "not in" (slice "catalog) }}
...
...
{{ $exclude := slice "London" "Berlin" }}
{{ range .Params.locations }}
{{ $location := . }}
{{ with not (in $exclude . ) }}
{{$.Scratch.Set "test" (slice $keyword) }}
{{end}}
{{end}}
(($.Scratch.Get "test")) | jsonify }},
{{end }}
I only get the last item in the array. Help please.
Well here the problem is you try to add a slice (list) to a string, because you’re using the example I gave you to reset the key to an empty string.
So one way to go forward from here would be to check if your scratch key is an empty string. Like this:
{{ if eq "" ($.Scratch.Get "test") }}
{{ $.Scratch.Set "test" (slice $keyword) }}
{{ else }}
{{ $.Scratch.Add "test" (slice $keyword) }}
{{ end }}
That’s a hack I used throughout my templates. However, maybe a $.Scratch.Reset
function would be nice…
I pretty new to this and I can’t get your construct to work with mine. Can you show me how you would integrate your if else statement with my code that includes the range function and $exclude variable?
Also @bep might be able to shed some light on a $.Scratch.Reset
function and if there is a better way of doing this.
No I can’t because you didn’t give the full code, only a short part of it. And you didn’t even say which error was brought by my suggestion. How could I guess?
However, I’m wondering if $keyword is the value you’re looking for, considering you’re checking whether .
is in $exclude or not. So what’s this $keyword? Are you sure it’s not supposed to be .
?
I have given the full code:
{{range where .Site.RegularPages "Type" "not in" (slice "catalog) }}
...
...
{{ $exclude := slice "London" "Berlin" }}
{{ range .Params.locations }}
{{ $location := . }}
{{ with not (in $exclude . ) }}
{{$.Scratch.Add "test" (slice $location) }}
{{end}}
{{end}}
(($.Scratch.Get "test")) | jsonify }},
{{end }}
I’m putting together a json file. The … is just to show that there are other fields like: "title": "{{.Title}}"
but this has no impact on what I’m trying to do here.
In the code above, I am excluding certain terms from being displayed that are listed in $location. So instead of justing doing "locations": "{{.Params.locations}}"
, I’m doing the above to exclude certain terms. I accidentally used the wrong variable name in the previous example. Sorry.
I did not get any error message with your code, i just could not get it to work and show the correct result.
I just noticed you have a big syntax error on your jsonify
line. You should use brackets and not parenthesis. Are you sure you are not getting any error messages?
You can try this:
{{ $exclude := slice "London" "Berlin" }}
{{ range $location := .Params.locations }}
{{ if not (in $exclude $location) }}
{{ if eq "" ($.Scratch.Get "test") }}{{ $.Scratch.Set "test" (slice $location) }}
{{ else }}{{ $.Scratch.Add "test" (slice $location) }}{{ end }}
{{ end }}
{{ end }}
{{ $.Scratch.Get "test" | jsonify }}
Please say what the actual output is if you want some help. Running around screaming “it’s not working!” is not helping anyone
Understood.
I just tried the code you provided – thank you – but unfortunately it does not work. Scratch still keeps adding locations for each result in the range just like I showed in a previous post, like so
item 1: [“Apple”, “Banana”, “Orange”] <— Correct
item 2: [“Apple”, “Banana”, “Orange”, “Kiwi”] <-- Incorrect. This should only be Kiwi.
item 3: [“Apple”, “Banana”, “Orange”, “Kiwi”, “Pineapple”] <-- Incorrect. This should only be Pineapple.
I’m sorry but I really don’t understand. You show me a code talking about locations with London and Berlin excluded, then you give results about fruits where Kiwi and Pineapple are not welcome. This is rather confusing
So anyway I just tried the snippet I gave you and it seems to me it works. Here’s the full example I used:
{{ $truc := slice "London" "truc" "bidule" }}
{{ $exclude := slice "London" "Berlin" }}
{{ range $location := $truc }}
{{ if not (in $exclude $location) }}
{{ if eq "" ($.Scratch.Get "test") }}{{ $.Scratch.Set "test" (slice $location) }}
{{ else }}{{ $.Scratch.Add "test" (slice $location) }}{{ end }}
{{ end }}
{{ end }}
{{ $.Scratch.Get "test" | jsonify }}
It does print out ["truc","bidule"]
like I was expecting. Can you confirm it works as expected on your side, too? Maybe this will help you find where the problem comes from, who knows?
Just use sed
or perl
to replace London with Kiwi, and so on . Honestly I stopped taking this thread seriously as the OP was trying to convert locations to fruits.
Apologies. I used fruits because it was easy and I had listed them in a previous post. I’m genuinely asking for help. If you don’t want to help, that’s fine. No need to ridicule.
I’ve used your code snippet, and it does work by itself. However, your code snipped does not include {{range where .Site.RegularPages "Type" "not in" (slice "catalog) }}
which I’ve included in my code shown here. When the range is applied, Scratch keeps adding for each post.
item 1: [“New York”, “Boston”, “Miami”] <— Correct
item 2: [“New York”, “Boston”, “Miami", “Chicago”] <-- Incorrect. This should only be Chicago.
etc…
You need to reset your Scratch to an empty slice before ranging on your tags:
{{ $.Scratch.Set "test" slice }}
Something is coming to allow for a less hacky .Scratch
key deleting/reseting:
I see a typo here (that probably has nothing to do with your problem). catalog
should have a second quotation mark at the end :
{{range where .Site.RegularPages "Type" "not in" (slice "catalog") }}
Can you show the full template you used to produce this precise result, and the two pages that produced these two items? That would help me reproduce and therefore understand the problem