I feel like I'm doing this wrong (looping on section fields):

{% for fieldLayoutField in craft.entries.section('rentalsBulletin').getEntryTypes().first.getFieldLayout().getFields() %}
    {% set field = craft.fields.getFieldById(fieldLayoutField.fieldId) %}
    {% include 'rentals-bulletin/_components/form-field-types/' ~ field.type %}
{% endfor %}

Building a front-end "create" page. Goal is loop through fields and depending on type include a snippet to build the form. Basically, get section fields and build the form dynamically.

EDIT: if you didn't see the accepted answer comments, the way to do this when you don't have an entry to work with is:

{% for fieldLayoutField in craft.sections.getSectionByHandle('rentalsBulletin').getEntryTypes()[0].getFieldLayout().getFields() %}

1 Answer 1


You don't need to go through craft.fields to get the fields. They are already there.

{% set fields = craft.entries.section('rentalsBulletin').getEntryTypes().first.getFieldLayout().getFields() %}
{% for field in fields %}
    {% set type = field.getField().type %}
    {% include 'rentals-bulletin/_components/form-field-types/' ~ type %}
{% endfor %}

You will probably also want to pass some variables into your included templates to actually build the fields with the proper field names as well.

{% set properties = { 'name': field.getField().handle } %}
{% include 'rentals-bulletin/_components/form-field-types/' ~ type with properties %}

There are also a few plugins available on the plugins page for form building that you might want to check out.

Update Looks like craft.entries.section('rentalsBulletin').getEntryTypes() returns null when there are no entries defined. Instead you can go through a different route that doesn't seem to depend on entries.

{% set fields = craft.sections.getSectionByHandle('news').getEntryTypes()[0].getFieldLayout().getFields() %}
  • Yea that looks much better. Properties piece too! However, I was wanting to avoid grabbing an actual entry in the case there are no entries to grab.
    – veritascs
    Commented Dec 18, 2014 at 23:30
  • 1
    Yes. I see what you mean about no entries. Definitely use the getEntryTypes().first then, if that works. The rest should be the same. I updated the code. ;) Commented Dec 18, 2014 at 23:34
  • Hah, actually my technique fails when there are no entries either. Should I just delete this whole question now?
    – veritascs
    Commented Dec 19, 2014 at 2:01
  • 1
    try this: {% set fields = craft.sections.getSectionByHandle('rentalsBulletin').getEntryTypes()[0].getFieldLayout().getFields() %} Commented Dec 19, 2014 at 2:16
  • Nice! That works.
    – veritascs
    Commented Dec 19, 2014 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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