Exclude term from being displayed in array

With the following front matter:

title = "Apples"
locations = ["New York", "Boston", "London", "Berlin"]

Is it possible to exclude the terms "London" and "Berlin" from being displayed when calling {{.Params.locations}} and only showing "New York" and "Boston"?


More info on what I’m trying to accomplish. I have the following in index.html:

{{range .Params.locations}}
    <a href="{{ . | urlize }}" title="{{ . }}">{{ . }}</a>

This displays all locations with their respective links. I need to exclude certain predetermined terms from showing on the page.

I’ve tried doing different things such as using replace, scratch and dict, but no luck.

Probably, but we’d need to see a use case. For instance, are you excluding them for a single page, or on every page?

Anyhow, you should read the docs, specifically the various functions, and the examples there should give you a direction to figure it out. :slight_smile:

Thanks for the reply.

The use case would be to exclude them from every page.

In the meantime, I’ll check out the docs some more.


Haven’t managed to solve this yet. Any chance someone can help out?

If I understand correctly you want to concatenate two taxonomy terms into a custom range. Never tried that… But see scratch at the Docs also there is a list of useful articles about Hugo functions (scratch has its own) in this thread.

If you only need to exclude this 2 you could use strings.Contains

And use the “and” function of Hugo.

From within your range:

{{ if and (not strings.Contains . “London”) (not strings.Contains . “Berlin”)) }}
{{ end }}

But you should really try and play with range where and the “not in” condition for something more manageable.

1 Like

This is the best I could find because range where needs an object property, and ranging on your params, the string you want to exclude is not inside a property. range where is out but with does the trick:

{{ $exclude := slice "London" "Berlin" }}
{{ range .Params.locations }}
    {{ $location := . }}
	{{ with not (in $exclude . ) }}
	    <a href="{{ $location | urlize }}" title="{{ $location }}">{{ $location }}</a>
	{{ end }}
{{ end }}

Thanks! Appreciate the help. It works like a charm.

I managed to figure it out yesterday by using lots of replace statements, thats why I asked here how to refactor the code.

But your solution is much better. Thanks again. :pray:

1 Like

Now that you’ve figured it out, would you let us know why you are doing this? It helps others understand why folks do things.

For instance, if you have two terms you don’t ever want to show, why use a taxonomy for it? Why not use a different parameter, to designate those pieces?

Why did you need that functionality? :slight_smile:

First come to mind: Dealing with a large amount of “automaticly” imported markdown files with plenty of useless metadata/taxonomy left there by an obscure framework. :slight_smile:

But again, may be something different.

Just saw your post now, so sorry for the delayed response.

@regis is right. I’ve got some unused metadata that I don’t want to display but it will hopefully become useful in the future so I don’t want to delete it.

As a disclaimer, most of the things i do with Hugo are perhaps unconventional and not considered best practice. This stuff is not really my jam, I’m learning as I go along. My goal is to solve problems, and how they get solved is not important. Over time, however, my code does get better :slightly_smiling_face:

Hugo is still in early development, we all have to use hacks pending a better understanding of the framework!
As long we follow the evolution of Hugo enough to replace those hacks by future features, we’ll be fine I think :wink: