Exclude term from being displayed in array


#1

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"?

EDIT

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

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

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.


#2

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:


#3

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.


#4

Hey,

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


#5

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.


#6

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.


#7

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 . ) }}
	 <li>
	    <a href="{{ $location | urlize }}" title="{{ $location }}">{{ $location }}</a>
	</li>
	{{ end }}
{{ end }}

#8

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:


#9

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:


#10

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.