olivierbon / Placid

Placid is a Craft plugin which makes it easy to consume REST services in your twig templates
77 stars 5 forks source link

Limit the number it items #21

Closed paul-frost closed 8 years ago

paul-frost commented 8 years ago

I couldn't work out an easy way to restrict it to only getting or showing only the last 5 items from a feed. I had expected the following to work from looking at other code examples around: {% set events = craft.placid.request('EventsFeed').limit(5) %} I'm new to craft, so the only way I managed it was by wrapping the output in an iF loop in the template: {% if loop.index <= 5 %} display t items {% endif %} Is there a better way? Could/should there be a limit option in the settings panel?

alecritson commented 8 years ago

Hey @paul-frost

Currently Placid just returns whatever array is returned from the API your are querying. So you would need to pass a limit value as the query string (assuming the API supports it)

Something like,

{% set events = craft.placid.request('EventsFeed', {
 query: {
  limit: 5
 }
}) %}

This translates to http://yourapi.com/somefeed?limit=5 for example.

paul-frost commented 8 years ago

Thanks Alec, but the API doesn't support that. So is my IF loop the most efficient way to set a limit?

alecritson commented 8 years ago

Hey @paul-frost

I actually just pushed an update which includes a way on how to resolve this for you, if you update to 1.7.112 you should be able to do:

{% set events = craft.placid.request('EventsFeed').limit(5) %}

{% for event in events.data %}
  {{ event.title }}
{% endfor %}

Note the use of the .data part, this is new in the latest version.

This is providing that the response is an array.

paul-frost commented 8 years ago

Thanks for that Alec, I appreciate it. Works perfectly.