Phlow
February 29, 2020, 12:10pm
1
Problem:
I have several data-files with the same structure.
data/money_2019.yml
data/money_2020.yml
I want to show the data with the help of a shortcode in my pages.
In my page I want to call the shortcode and give it a parameter to call the according data-file.
{{< money dataset="money_2020" >}}
Now I want the Shortcode to range through that data file. But how do I do it?
{{ range .Site.Data.$VALUE_OF_PARAMETER }}
<!-- output -->
{{ end }}
See:
Hint: try using the dollar sign $
to go higher up from within the shortcode context.
OR
You can also use the global site
function -that does not depend on the context- for example:
{{ range site.Data.$VALUE_OF_PARAMETER }}
1 Like
Phlow
February 29, 2020, 3:38pm
3
First: Thank you for helping me. I know of the concept of $ and .
Maybe I explain with an example what I want to achieve:
I call the shortcode like this:
{{< money dataset="money_2020" >}}
And then I want the shortcode to loop through the according data-file like this:
{{ range .Site.Data.money_2020 }}
<!-- output -->
{{ end }}
When I use another shortcode like this
{{< money dataset="money_2020" >}}
it shall loop like this:
{{ range .Site.Data.money_2020 }}
<!-- output -->
{{ end }}
Now my question: How do I construct this code in shortcode by using the parameter provided in the shortcode?
You need to use .Get
to fetch the dataset
parameter, then store it in a variable and finally construct the correct PATH and store it in the same variable, something like:
{{ $file := .Get "dataset" }}
{{ $file = print .Site.Data $file }}
{{ range $file }}
<!-- output -->
{{ end }}
1 Like
Phlow
February 29, 2020, 5:18pm
5
We are getting closer. Thank you. Now your code grabs all data-files but not the one we want and Hugo complains and canβt execute.
{{ range $file }}
<!-- output -->
{{ end }}
If I write this code
{{ $file := .Get "dataset" }}
{{ $file = print .Site.Data $file }}
{{$file}}
It prints all the maps in data. Any ideas to narrow it down to the desired one?
The code I posted above is not right.
Here is something that should work:
{{ range (index .Site.Data (.Get "dataset")) }}
<--- key --->
{{ end }}
To range over the map interface of the given data file we need to use the index
function
1 Like
Phlow
March 2, 2020, 10:20am
7
I want to say Thank you β Your code works perfect.
And now the finished example.
The /data/testdata.yml looks like:
-
date: 31.01.20
money: -5
-
date: 30.01.20
money: -13.65
-
date: 29.01.20
money: -11.99
In a content-page we call the shortcode with a parameter to look up our testdata.yml -file.
{{< money dataset="testdata" >}}
And the shortcode /layouts/shortcodes/money.html looks like this;
<ul>
{{ range (index .Site.Data (.Get "dataset")) }}
<li>{{ .date }} β <code>{{ .money }} $</code></li>
{{ end }}
</ul>
And the result looks like this:
<ul>
<li>31.01.20 β <code>-5 $</code></li>
<li>30.01.20 β <code>-13.65 $</code></li>
<li>29.01.20 β <code>-11.99 $</code></li>
</ul>
1 Like
system
Closed
March 4, 2020, 10:20am
8
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.