0

I am trying to use the Placid plugin to retrieve track-names from an external service, and having problems. I have the following code:

{% for song in craft.entries.section('music') %}
  {% set params = { 'id' : song.itunesId } %}
  {% set lookup = craft.placid.get('itunesMusic', { params : params } ) %}

  {% for track in lookup.results %}
    {{ track.trackName }}
  {% endfor %}
{% endfor %}

The values of song.itunesId and params seem to be set correctly, however lookup.results is returning the same track.trackName on each iteration of the loop.

  • Hey Douglas. I'm using a plugin called Placid, and it is using that method. My loop.index increments, and so does my song.itunesId when I set them up like {{ song.itunesId }}, but inside the loop it's not resetting the value of params to the new song.itunesId. – Spentacular Oct 19 '14 at 13:19
  • @DouglasMcDonald ahh, that way didn't work either. I'm getting output fine from both loops, the problem is {{ track.trackName }} only displays the first song track name (in the case the track name is Bless Your Name, so it outputs Bless Your Name twice), but if I put {{ song.itunesId }} in the first loop, it iterates over both values. Is there any way to make sure both of the set values reset for each iteration of the loop, instead of "sticking"? – Spentacular Oct 19 '14 at 17:59
  • The set params value should iterate fine. Have you tried printing {{ params['id'] }} to test? – Douglas McDonald Oct 19 '14 at 18:27
  • Hmmm, now I'm lost, because that does work. Now I have no clue what to do since that placid.get method isn't taking the new variable each time in the loop. Basically, I'm just trying to get the id I added in an entry and put it on the end of the iTunes Search api, i.e. itunes.apple.com/lookup?id=721273648, where the last number is different. That is weird that it doesn't work. – Spentacular Oct 19 '14 at 18:42
  • That is weird. Should work. I hope you don't mind, I edited the question and deleted my comments which no longer applied. – Douglas McDonald Oct 19 '14 at 19:28
2

Thanks for giving Placid a try, sucks you have had issues using it. I understand you're using Guzzle now, but for future reference I think I know what was going on.

Caching

In this case you would need to turn caching off for the Placid request, this is due to you needing a new request for each iteration, I will be looking to improve the caching in the near future to be more intelligent but in the meantime just setting { cache: false } should fix this:

{% for song in craft.entries.section('music') %}
  {% set params = { 'id' : song.itunesId } %}
   {% set lookup = craft.placid.get('itunesMusic', { params : params, cache: false } ) %}
   {% for track in lookup.results %}
    {{ track.trackName }}
  {% endfor %}
{% endfor %}

I've set up an example so you can see it in action here

I hope that helps if you choose to use Placid again in the future!

  • Hey Alec, thanks for replying! I didn't even realize that! I realize I was hacking Placid a little differently than it's intended use as well. If I were better at PHP (which hopefully after digging into it more I will be), I'd love to help out in whatever way I could! – Spentacular Oct 20 '14 at 15:37
  • No worries, it can sometimes be an easy oversight when developing! :) I think the way you are using Placid is fine though, the caching just needs updating so you don't hammer the API with requests on each page load, but that will come! If you ever do want to contribute, the project is on Github so just send a pull request, always welcome :) – Alec Ritson Oct 21 '14 at 7:41
  • @Alec is there any reason not to use Craft's cache tag to cache the whole loop? Do you see any problems I might run into? Thanks for the plugin btw!! – carlcs Oct 31 '14 at 11:48
  • Hey @carlcs, thanks for checking Placid out! There shouldn't be any issues using crafts cache tag, if you do get any issues i'd be interested to see why. I added caching to placid mainly just as a failsafe, if some people aren't in the habit of using the cache tag or they forget etc...it should at least minimise performance hits where it's being used :) – Alec Ritson Oct 31 '14 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.