Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Add keys to collection of each content_type #10

Closed
flxsource opened this issue Apr 22, 2016 · 4 comments
Closed

Add keys to collection of each content_type #10

flxsource opened this issue Apr 22, 2016 · 4 comments
Assignees
Labels

Comments

@flxsource
Copy link

Currently, each content_type is placed in an collection, but without any keys. Would it be possible to get the collection indexed by entry title, so that we can cherry pick a particular entry?

@dlitvakb
Copy link
Contributor

Hey @flxsource,

Sadly, entry title is something one can't rely on as it's dependant on the Content Type structure.

But if you want to cherry-pick an entry you can do it like follows:

{% for entry in site.data.contentful.spaces.my_space.entry %}
  # By sys.id
  {% if entry.sys.id == 'MY_ENTRY_ID' %}
    # Do something...
  {% endif %}

  # By a property
  {% if entry.title == 'some fancy title' %}
    # Do something...
  {% endif %}
{% endfor %}

All entries provide a key for it's ID accessed with object.sys.id and you can easily filter by it, or you can filter by any property you want by directly accessing it.

Hope this helps

@dlitvakb dlitvakb self-assigned this Apr 22, 2016
@flxsource
Copy link
Author

I was hoping to avoid looping all of them, but it will certainly work.

Many thanks

@dlitvakb
Copy link
Contributor

Sadly that's a limitation of the Liquid templating language, something like array.find { |object| object.sys.id == 'foo' } would have solved it.

Glad it helped! 👍

@flxsource
Copy link
Author

I've found another workaround for this issue, as long as the data structure you have in contentful supports it. I've built a proof of concept (that's also sufficient for my use case), and if you think it's worth while I'll do some more work to put it behind a config option and add some error handling.

See #11

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants