SOLVED: Selecting data files with shortcode

Hi,

in a site about books I want to set links to book online shops with a shortcode that pulls the data from a data file.

I managed to select the data file of the book by using yaml metadata at the beginning of content files and inserting the data with a partial in the layout/partial folder.

data/foobar.yaml looks like this:

title: The Book Title
cover: "/images/cover.jpg"
bookpage: http://example.com
amazon: http://amazon-link
kindle: http://kindle-link
itunes: http://itunes-link
google: http://google-link

partial/shoplinks.html looks like this:

{{ with (index .Site.Data (substr .Params.book 0)) }}
      <div class="row">
            <div class="col-md-3">
              <img alt = "{{ .title }}" src = "{{ .cover}}" class="img-responsive" width = "250px">
            </div>
            <div class="col-md-3">
              <a href="{{ .amazon}}">Amazon</a>
            </div>
            <div class="col-md-3">
              <a href="{{ .kindle}}">Kindle</a>
            </div>
            <div class="col-md-3">
              <a href="{{ .itunes}}">iTunes</a>
            </div>
            <div class="col-md-3">
              <a href="{{ .google}}">Google</a>
            </div>
            <div class="col-md-3">
              <a href="{{ .bookpage}}">Mehr Infos zum Buch</a>
            </div>
      </div>
    {{ end }}

In content/post/example.md I have:

---
book: foobar
---

Now I would like to create a shortcode so that I can insert the info anywhere in the content like this:

{{< shoplink "foobar">}}

Can anyone shed some light on how to achieve this? I think I have to put shoplinks,html into the shortcodes folder. But how to adjust the template to get the right parameter?

UPDATE:

Now I tried this in shortcodes/shoplinks:

  {{ with (index $.Page.Site.Data (substr .Params 0)) }}
      <div class="row">
            <div class="col-md-3">
              <img alt = "{{ .title }}" src = "{{ .cover}}" class="img-responsive" width = "250px">
            </div>
            <div class="col-md-3">
              <a href="{{ .amazon}}">Amazon</a>
            </div>
            <div class="col-md-3">
              <a href="{{ .kindle}}">Kindle</a>
            </div>
            <div class="col-md-3">
              <a href="{{ .itunes}}">iTunes</a>
            </div>
            <div class="col-md-3">
              <a href="{{ .google}}">Google</a>
            </div>
            <div class="col-md-3">
              <a href="{{ .bookpage}}">Mehr Infos zum Buch</a>
            </div>
      </div>
    {{ end }}

While I have in a content file this:

{{< shoplinks “foobar” >}}

And I get this error message:

ERROR: 2016/06/21 12:07:32 shortcode.go:533: error processing shortcode shortcodes/shoplinks.html 
 ERR: template: shortcodes/shoplinks.html:1:35: executing "shortcodes/shoplinks.html" at <substr .Params 0>: error calling substr: Unable to Cast []string{"foobar"} to string

SOLUTION:

It works if I do it right:

{{ with (index $.Page.Site.Data (substr .Params.book 0)) }}

Using a named argument in content file:

{{< shoplinks book=“foobar” >}}

1 Like