<p>You don't need to go through <code>craft.fields</code> to get the fields. They are already there.</p>

<pre><code>{% 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 %}
</code></pre>

<p>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.</p>

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

<p>There are also a few plugins <a href="http://straightupcraft.com/craft-plugins" rel="nofollow">available on the plugins page</a> for form building that you might want to check out.</p>

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

<pre><code>{% set fields = craft.sections.getSectionByHandle('news').getEntryTypes()[0].getFieldLayout().getFields() %}
</code></pre>

CC BY-SA 3.0
2014-12-18T23:30:21.897Z
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
CC BY-SA 3.0
2014-12-18T23:34:22.417Z
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. ;)
Douglas McDonald
CC BY-SA 3.0
2014-12-19T02:01:17.883Z
Hah, actually my technique fails when there are no entries either. Should I just delete this whole question now?
veritascs
CC BY-SA 3.0
2014-12-19T02:16:23.153Z
try this: `{% set fields = craft.sections.getSectionByHandle('rentalsBulletin').getEntryTypes()[0].getFieldLayout().getFields() %}`
Douglas McDonald
CC BY-SA 3.0
2014-12-19T02:23:37.147Z
Nice! That works.
veritascs
CC BY-SA 3.0
2014-12-19T02:26:13.963Z
Might want to check out a nice plugin called 'inspector'. It prints out all properties and methods of an object using {{ object|inspect }}. Great for following the rabbit hole ;)
Douglas McDonald
CC BY-SA 3.0
2014-12-19T02:36:31.523Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/19599/discussion-between-veritascs-and-douglas-mcdonald).
veritascs
CC BY-SA 3.0
2014-12-18T23:18:43.333Z
2014-12-19T02:19:38.740Z
2014-12-19T02:19:38.740Z
Douglas McDonald
<p>I feel like I'm doing this wrong (looping on section fields):</p>

<pre><code>{% 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 %}
</code></pre>

<p>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.</p>

<p>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:</p>

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

<field><fieldtypes>
getting fields of a section (getFields())
# getting fields of a section (getFields())

(Asked by: username_0 on 2014-12-18)

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() %}

**Tags:** field, fieldtypes


---

## 1 answers

### Answer by username_1 on 2014-12-18. Score: 1 | Accepted answer

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() %}

> **Comments:**
> **username_0**: 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.  
> **username_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. ;)  
> **username_0**: Hah, actually my technique fails when there are no entries either. Should I just delete this whole question now?  
> **username_1**: try this: `{% set fields = craft.sections.getSectionByHandle('rentalsBulletin').getEntryTypes()[0].getFieldLayout().getFields() %}`  
> **username_0**: Nice! That works.  
> **username_1**: Might want to check out a nice plugin called 'inspector'. It prints out all properties and methods of an object using {{ object|inspect }}. Great for following the rabbit hole ;)  
> **username_0**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/19599/discussion-between-username_0-and-douglas-mcdonald).  

---

