Skip to content

Instantly share code, notes, and snippets.

@parsiya
Last active March 21, 2016 02:08
Show Gist options
  • Save parsiya/562f5d2bde3e9060d793 to your computer and use it in GitHub Desktop.
Save parsiya/562f5d2bde3e9060d793 to your computer and use it in GitHub Desktop.

There are definitely better ways to do this. The named parameter version is much better in my opinion, the shortcode is easier to write but this should work for both.

Create a file named wp.html (this will be the name of the shortcode) and put it in hugo\layouts\shortcodes\.

{{ if .IsNamedParams }}
  {{ $.Scratch.Set "wp_tag" (.Get "tag") }}
  {{ with .Get "lang" }}
    {{ $.Scratch.Set "wp_lang" . }}
  {{ else }}
    {{ $.Scratch.Set "wp_lang" "en" }}
  {{ end }}

  {{ with .Get "title" }}
    {{ $.Scratch.Set "wp_title" . }}
  {{ else }}
    {{ $.Scratch.Set "wp_title" (.Get "tag") }}
  {{ end }}

{{ else }}
  {{ $.Scratch.Set "wp_tag" (.Get 0) }}
  <!-- if all three attributes are provided -->
  <!-- { { < wp VIC_Cipher "VIC Cipher" fr >}} -->
  <!-- { { < wp VIC_Cipher "" fr >}} -->
  {{ if len .Params | eq 3 }}
    {{ $.Scratch.Set "wp_lang" (.Get 2) }}
    <!-- if title is empty (2nd example) -->
    {{ if len (.Get 1) | eq 0 }}
      <!-- removing the underscore and converting it to space should be done here -->
      {{ $.Scratch.Set "wp_title" (.Get 0) }}
    {{ else }}
      {{ $.Scratch.Set "wp_title" (.Get 1) }}
    {{ end }}
  {{ end }}

  <!-- if only two attributes are provided -->
  <!-- { { < wp VIC_Cipher fr >}} -->
  <!-- { { < wp VIC_Cipher "VIC Cipher" >}} -->
  {{ if len .Params | eq 2 }}
    <!-- here I assume that if the length of the parameter is 2 then it's language otherwise title -->
    {{ if len (.Get 1) | eq 2 }}
      {{ $.Scratch.Set "wp_lang" (.Get 1) }}
      <!-- removing the underscore and converting it to space should be done here -->
      {{ $.Scratch.Set "wp_title" (.Get 0) }}
    {{ else }}
      {{ $.Scratch.Set "wp_lang" "en" }}
      {{ $.Scratch.Set "wp_title" (.Get 1) }}
    {{ end }}
  {{ end }}

  {{ if len .Params | eq 1 }}
    {{ $.Scratch.Set "wp_lang" "en" }}
    <!-- removing the underscore and converting it to space should be done here -->
    {{ $.Scratch.Set "wp_title" (.Get 0) }}
  {{ end }}
{{ end }}

<a href="https://{{ $.Scratch.Get "wp_lang" }}.wikipedia.org/wiki/{{ $.Scratch.Get "wp_tag" }}">{{ $.Scratch.Get "wp_title" }}</a>

Now you can use the shortcode.

You can either use named parameters:

  • {{< wp tag="VIC_cipher" >}}
  • {{< wp tag="VIC_cipher" lang="fr" >}}
  • {{< wp tag="VIC_cipher" lang="fr" title="" >}}
  • {{< wp tag="VIC_cipher" title="VIC Cipher" >}}
  • {{< wp tag="VIC_cipher" lang="en" title="VIC Cipher" >}}

Rendered to:

<a href="https://en.wikipedia.org/wiki/VIC_cipher">VIC_cipher</a>
<a href="https://fr.wikipedia.org/wiki/VIC_cipher">VIC_cipher</a>
<a href="https://fr.wikipedia.org/wiki/VIC_cipher">VIC_cipher</a>
<a href="https://en.wikipedia.org/wiki/VIC_cipher">VIC Cipher</a>
<a href="https://en.wikipedia.org/wiki/VIC_cipher">VIC Cipher</a>

Or positional:

  • {{< wp VIC_cipher >}}
  • {{< wp VIC_cipher fr >}}
  • {{< wp VIC_cipher "" fr >}}
  • {{< wp VIC_cipher "VIC Cipher" >}}
  • {{< wp VIC_cipher "VIC Cipher" fr >}}
<a href="https://en.wikipedia.org/wiki/VIC_cipher">VIC_cipher</a>
<a href="https://fr.wikipedia.org/wiki/VIC_cipher">VIC_cipher</a>
<a href="https://fr.wikipedia.org/wiki/VIC_cipher">VIC_cipher</a>
<a href="https://en.wikipedia.org/wiki/VIC_cipher">VIC Cipher</a>
<a href="https://fr.wikipedia.org/wiki/VIC_cipher">VIC Cipher</a>

It does not remove the underlines from tag to use in title. I have not found a Hugo template function for that yet (perhaps there is one).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment