HufflepuffBR
HufflepuffBR

Reputation: 463

Jekyll - Parsing and finding some info and pair

I'm working on a Jekyll theme for podcasting. I've already put the RSS renders and so and I want to use the <audio> tag to add a HTML5 player in the page.

In the frontmatter for the podcast posts, I have the following structure:

    - audio
      - type: typeA
        url: urlA
      - type: typeB
        url: urlB

I want to find a specific type and use the associated URL as the file for the player. Is there an easy way to catch the correct one (preferably if I can test before I there's a correct entry) or should I parse all the audio frontmatter entries till I find the one I want?

PS: The page is being hosted at Github Pages - http://github.com/hufflepuffbr/hufflepuffbr.github.io

Upvotes: 0

Views: 49

Answers (1)

Shadowen
Shadowen

Reputation: 836

Your Way

Using the YAML front matter you have defined, it's only possible to loop through the list. It's an unsorted list, so by definition search is order N (ie, we have to go through the whole list).

{% assign desired_type = "typeA" %}
{% capture audio_source %}{% for a in page.audio %}{% if a.type == desired_type %}{{ a.url }}{% endif %}{% endfor %}{% endcapture %}
<audio src="{{ audio_source }}">

A Better Way

However, if you switch your YAML front matter to a map/dictionary/hash format like so:

audio:
    typeA: urlA
    typeB: urlB

You can look the type up directly in the variable:

{% assign desired_type = "typeB" %}
audio src="{{ page.audio[desired_type] }}">

As usual, be careful of urls containing :, since that will break the YAML parser. These can be escaped in ".

Upvotes: 1

Related Questions