1

Lets say I want to get a list of category names and ids and pass them into JSON (for vue.js).

This is how I'm currently doing it:

{% set sections = craft.categories.group('teamCategory').select(['title','elements.id','groupId']).all() %}

<!-- My vue.js tag: -->
<team :sections="{{ sections|json_encode() }}"></team>

My problem is that I only want title and id to be passed to vue, but craft/twig is passing load and loads more data that forms part of the category object.

Is there an easy way to just pass the array columns I need?

1

You can use the asArray function to return arrays instead of elements

{% set sections = craft
    .categories
    .group('teamCategory')
    .select(['title','elements.id','groupId'])
    .asArray()
    .all() 
%}
  • 1
    Nice. That's the ticket. I wonder why this approach never stood out for me. Maybe I need to go and read the docs again! – sunscreem Apr 15 '19 at 16:10

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.