1

I've got a frontend form which queries the openstates.org API to return a given set of data objects.

Those objects also exist in Craft as entries, and the API data includes a field that serves as a unique ID for the Craft entries - how can I pass those unique IDs back to the Craft template to retrieve the corresponding entries?

  • 2
    You could add them as query string to the URL. When rendering the page, you can tan check the query strign to see which ones you'd have to display... – mrsmn Jul 26 '17 at 5:57
  • Thanks, Michael - that worked perfectly. I wound up combining it with the sample code from this answer. – mierla Jul 26 '17 at 15:23
  • Perhaps add your working combination as answer? That way people in the future can directly see the answer. – mrsmn Aug 1 '17 at 13:24
2

Per Michael's suggestion above, with some help from this answer, I passed the results from the API call back to a craft template as query params, so it looked like this (simplified):

{% if craft.request.isAjax %}
    {% set myReps = craft.request.getParam('personId') %}
    {% set myIds = myReps | split(',') %}
    {% for myId in myIds %}
        {% set person = craft.entries.section('representatives').repId(myId).first() %}
    {% endfor %}
{% endif %}

and then output my info from there.

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.