3

I need to access just the ID of a specific section.

I do that like this and it works:

{% set enroll = craft.sections.getSectionByHandle('workshopEnrollments') %}

But when I do:

{% for e in enroll %} {{ e.sectionId }} {% endfor %}

or:

{% for e in enroll %} {{ e.id }} {% endfor %}

I get the error:

Impossible to access an attribute ("sectionId") on a string variable ("32")

However, when I view the variable inside my form what I actually see is exactly this

32Workshop EnrollmentsworkshopEnrollmentschannel1workshop-enrollments/_entry1

So my question is, how can I just display the id. Both ".id" and ".sectionId" produce the same error mentioned above.

"32" is the id I want so it is the correct return, but this seems to show everything about it in a string and I need just the id. I am using getSectionByHandle because I have no other data to use in this particular view.

Thanks for any help.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Rockwell Rice
  • 509
  • 1
  • 5
  • 16

2 Answers2

5

getSectionByHandle doesn't return an array, but a single SectionModel - so you need to remove the for loop:

{% set enroll = craft.sections.getSectionByHandle('workshopEnrollments') %}

{{ enroll.id }}
Mats Mikkel Rummelhoff
  • 22,361
  • 3
  • 38
  • 69
4

Updated for Craft 4:

{% set enroll = craft.app.sections.getSectionByHandle('workshopEnrollments') %}
Mosswalker
  • 310
  • 3
  • 14