<p>If I understand you correctly, you need to be able to create unique "pages", and through some sort of "layout/page builder" create those page's layouts on a page-by-page basis.</p>

<p>Using Singles sections to achieve this is the <strong>wrong</strong> approach, in my opinion – in fact, creating/managing sections should <strong>never</strong> be part of the regular editorial workflow, and honestly I can't really imagine a use case where such an approach would even be needed. If it exists, I'd say Craft may not be the best platform for your project. </p>

<p>In your case, I'd suggest creating a Structure or Channel section called "Pages" to hold your "page" entries. If you want to be able to nest your pages – or need them to be in a specific order – use a Structure, if not a Channel will suffice.</p>

<p>For building the layout, I'd add a single Matrix field to the "Pages" section's entry type, called "Layout" or the like, containing all the possible "fields" or layouts (like "Impact statement") as block types.</p>

<p>The one thing I'm a bit confused about is your issue about having "nested containers" with the above approach. I'm not seeing the problem as you could basically just use your current markup like this, and you would have the same end result as you currently have, only with the correct order:</p>

<pre><code>{% for block in entry.pageLayout %}

    {% switch block.type %}

        {% case 'impactStatement' %}
        &lt;div class="container-fluid add-top"&gt;
            &lt;div class="row"&gt;
                 &lt;div class="text-center impact-highlight"&gt;
                     &lt;p&gt;{{block.heading}}&lt;/p&gt;
                 &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;

        {% case 'largeHeading' %}
        ...

    {% endswitch %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-05-09T14:33:54.183Z
Right I was going to try this, but the issue I had with the containers seemed to be related to the for loop. Say each block type needs its own container div. When I tried this before for each block type it would output that same container div and for some reason was nesting them. I will update example code if this ends up working. Thanks for the suggestion for creating a channel or stucture just not sure what the difference is vs creating a section and editing the section.
Bowenac
CC BY-SA 3.0
2015-05-09T17:02:32.203Z
Your nesting issue sounds like a markup thing - maybe an unclosed tag? You might want to put that issue up as a separate question; specifically highlighting the point where your markup turns weird.  Using sections as entries is a bad idea for many reasons, but the main problem is that you're setting your client/editors up with a needlessly complex and error prone workflow.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-05-11T17:47:39.550Z
Wanted to say thanks again for your help, this worked out great, and used this http://craftcms.stackexchange.com/a/1747/3579 for my nesting issue. I created a section called pages, and a matrix field called layout with all the fields so far so good.
Bowenac
CC BY-SA 3.0
2015-05-11T17:49:13.727Z
That's great, @Bowenac - happy to hear it!
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-05-12T02:07:27.510Z
Ran into a nesting issue with testimonials haha http://craftcms.stackexchange.com/questions/9691/matrix-loop-nesting-issue
Bowenac
CC BY-SA 3.0
2015-05-09T10:39:10.630Z
2015-05-09T10:46:17.127Z
2015-05-09T10:46:17.127Z
Mats Mikkel Rummelhoff
<p>You could possibly loop through the field layout for that section, and then output the fields in the order that they are defined. In this example, I'm using macros, but you could just as easily place the code within the switch statement directly.</p>

<pre><code>{% macro testimonials(matrix) %}
    {% import _self as self %}

    &lt;div class="container-fluid add-top"&gt;
        &lt;div class="row"&gt;
            &lt;div class="text-center testimonials"&gt;

                {% for block in matrix %}
                    {% if block.type == "testimonial" %}

                        &lt;blockquote class="blockquote-unstyled" style="display:none;"&gt;
                            &lt;footer class="text-uppercase"&gt;
                                &lt;span title="Source Title"&gt;{{ block.sourceTitle }}&lt;/span&gt;
                            &lt;/footer&gt;
                            &lt;p class="lead"&gt;“{{ block.lead }}”&lt;/p&gt;
                        &lt;/blockquote&gt;

                    {% endif %}
                {% endfor %}

                &lt;div class="list-inline"&gt;

                    {% for block in matrix %}
                        {% if block.type == "testimonial" %}
                            {% set image = block.image.first() %}
                            &lt;li&gt;&lt;img class="img-responsive img-circle" alt="Profile" src="{{ image.getUrl('testimonials') }}" width="{{ image.getWidth('testimonials') }}" height="{{ image.getHeight('testimonials') }}"/&gt;&lt;/li&gt;
                        {% endif %}
                    {% endfor %}

                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

{% endmacro %}

{% macro impactStatment(matrix) %}
    {% import _self as self %}
    {# format/output matrix data #}
{% endmacro %}

{% macro largeHeadline(matrix) %}
    {% import _self as self %}
    {# format/output matrix data #}
{% endmacro %}


{% for tab in entry.getFieldLayout.getTabs %}

    {% for field in tab.getFields %}

        {% switch field.getField.handle %}

            {% case 'testimonials' %}
                {{ macros.testimonials(entry.testimonials) }}

            {% case 'impactStatement' %}
                {{ macros.impactStatement(entry.impactStatement) }}

            {% case 'largeHeadline' %}
                {{ macros.largeHeadline(entry.largeHeadline) }}

        {% endswitch %}

    {% endfor %}

{% endfor %}
</code></pre>

<p>You would obviously need to process and format the output (i.e. loop through the matrix, add html formatting, etc.)</p>

CC BY-SA 3.0
2015-05-08T17:25:29.237Z
Wouldn't this be the same as adding all input fields as content blocks of a matrix field? Then I would end up having issues wrapping each section in a container or so I think I would, not sure how to stop the loop from nesting containers.
Bowenac
CC BY-SA 3.0
2015-05-08T17:58:54.007Z
I'm not sure you get what I was saying — I'm suggesting that you continue to use your matrix fields, but just output them in the order that the fields are defined — that should take care of the nesting issues. Personally I wouldn't try and make such a catch all template to begin with. As I'm not sure I see the advantage — but if you wanted to do that, this is one way to go about it. I'll update the answer to show an example of a matrix output.
Douglas McDonald
CC BY-SA 3.0
2015-05-08T17:59:04.557Z
The reason I want to do this is so a client can add new "pages" sections without needing to know which template they need to choose... Then they can add what ever input fields they want to that section and not have to worry about the order and output.
Bowenac
CC BY-SA 3.0
2015-05-08T18:27:38.433Z
You might want to check out [superTable plugin](https://github.com/engram-design/SuperTable)... putting a superTable field in a matrix field is essentially the same as placing a matrix in a matrix (which is currently not possible). Then your client could potentially build pages/sections in a structure on the fly without having to resort to creating new sections or editing fieldLayouts in the settings, but with advantage of being able to group items together nicely (i.e. testimonials).
Douglas McDonald
CC BY-SA 3.0
2015-05-08T18:37:24.300Z
FYI... they are working on a version of superTable that supports data added in a single column vs  columns/rows — I think that might suit your use case better. Might want to reach out to the developer and see where they are with that.
Douglas McDonald
CC BY-SA 3.0
2015-05-08T16:40:54.200Z
2015-05-08T18:20:29.523Z
2015-05-08T18:20:29.523Z
Douglas McDonald
<p>Banging my head on this one and have not been able to come up with a solution to this. I'm wanting to create a default template for all single entries that checks for all of the fields I have created if they exist and the position in that section.</p>

<p>This template would have all code for all possible field types. So far this doesn't work the way I was hoping it would as it renders the page based on the order of the code in the template. So if a client adds a new field to a section it doesn't matter what order they put the field in the list of fields, in the end it is determined by where the code to render that field is in the template.</p>

<p>I have developed with wordpress, acf, and visual page builders where you can drag drop elements into different positions which are just shortcodes but the page ends up rendering based on the position of the elements "shortcodes".</p>

<p>Is this at all possible? Here is some examples of some code in said template...</p>

<pre><code>&lt;!-- Testimonials Block --&gt;
{% if entry.testimonials|length %}
&lt;div class="container-fluid add-top"&gt;
    &lt;div class="row"&gt;
        &lt;div class="text-center testimonials"&gt;
        {% for block in entry.testimonials %}
            {% if block.type == "testimonial" %}
                {% set image = block.image.first() %}
                &lt;blockquote class="blockquote-unstyled" style="display:none;"&gt;
                    &lt;footer class="text-uppercase"&gt;
                        &lt;span title="Source Title"&gt;{{block.sourceTitle}}&lt;/span&gt;
                    &lt;/footer&gt;
                    &lt;p class="lead"&gt;“{{block.lead}}”&lt;/p&gt;
                &lt;/blockquote&gt;
            {% endif %}
        {% endfor %} 
        &lt;div class="list-inline"&gt;
        {% for block in entry.testimonials %}
            {% if block.type == "testimonial" %}
                {% set image = block.image.first() %}
                    &lt;li&gt;&lt;img class="img-responsive img-circle" alt="Profile" src="{{ image.getUrl('testimonials') }}" width="{{ image.getWidth('testimonials') }}" height="{{ image.getHeight('testimonials') }}"/&gt;&lt;/li&gt;
            {% endif %}
        {% endfor %} 
        &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
{% endif %}

&lt;!-- Impact Statement --&gt;
{% if entry.impactStatement|length %}
    {% for block in entry.impactStatement %}
        {% if block.type == "impactStatement" %}
            &lt;div class="container-fluid add-top"&gt;
                &lt;div class="row"&gt;
                    &lt;div class="text-center impact-highlight"&gt;
                        &lt;p&gt;{{block.heading}}&lt;/p&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        {% endif %}
    {% endfor %} 
{% endif %}

&lt;!-- Large Headline Blocks --&gt;
{% if entry.largeHeadline|length %}
&lt;div class="container-fluid add-top"&gt;
    &lt;div class="row"&gt;
    {% for block in entry.largeHeadline %}
            {% if block.type == "headline" %}
                {% set image = block.image.first() %}
                    &lt;div class="col-sm-6 col-md-10 col-md-push-1 add-top"&gt;
                        &lt;div class="row"&gt;
                            &lt;div class="col-sm-6 hidden-xs {{block.imagePosition}}"&gt;
                                &lt;!-- Img Logo --&gt;
                                &lt;img src="{{image.url}}"/&gt;
                            &lt;/div&gt;
                            &lt;div class="col-sm-6"&gt;
                                &lt;h3 class="remove-top"&gt;{{block.heading}}&lt;/h3&gt;
                                &lt;small&gt;{{block.paragraph}}&lt;/small&gt;
                            &lt;/div&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
            {% endif %}
    {% endfor %} 
    &lt;/div&gt;
    &lt;hr class="add-top"&gt;
&lt;/div&gt;
{% endif %}
</code></pre>

<p>As you can see this is just three field types but I have a lot. But for this example the template has them in this order.</p>

<p>Testimonials Matrix Field</p>

<p>Impact Statement</p>

<p>Large Headline</p>

<p>If I move impact statement in the section above testimonials, that has no relation on where that impact statement gets rendered in the entry...</p>

<p>I also tried to do ifelse thinking it would loop through the template outputting the blocks in the order they are in the section for that single if they exist. But this just outputs the testimonials block and stops haha.</p>

<pre><code>{% if entry.testimonials|length %}
//Block Code
{% elseif entry.testimonials|length %}
//Block Code
{% elseif entry.testimonials|length %}
//Block Code
{% endif %}
</code></pre>

<p>I thought one way to solve it would just be to have one matrix field with all of the possible fields as a block with each field in that block, but then I think I run into issues wrapping each element in a container as for each block I end up outputting a container so they end up nested.</p>

<p>Any solutions or possible tips would be great. Hope this makes sense.</p>

CC BY-SA 3.0
2015-05-08T16:12:56.570Z
In the craft_fieldlayoutfields table there's a sortOrder column...
tom
<templating><matrix><config><field>
Solution to default template with all possible fields and determine position
# Solution to default template with all possible fields and determine position

(Asked by: username_0 on 2015-05-08)

Banging my head on this one and have not been able to come up with a solution to this. I'm wanting to create a default template for all single entries that checks for all of the fields I have created if they exist and the position in that section.
This template would have all code for all possible field types. So far this doesn't work the way I was hoping it would as it renders the page based on the order of the code in the template. So if a client adds a new field to a section it doesn't matter what order they put the field in the list of fields, in the end it is determined by where the code to render that field is in the template.
I have developed with wordpress, acf, and visual page builders where you can drag drop elements into different positions which are just shortcodes but the page ends up rendering based on the position of the elements "shortcodes".
Is this at all possible? Here is some examples of some code in said template...

{% if entry.testimonials|length %}

    
        
        {% for block in entry.testimonials %}
            {% if block.type == "testimonial" %}
                {% set image = block.image.first() %}
                
                    
                        {{block.sourceTitle}}
                    
                    “{{block.lead}}”
                
            {% endif %}
        {% endfor %} 
        
        {% for block in entry.testimonials %}
            {% if block.type == "testimonial" %}
                {% set image = block.image.first() %}
                    
            {% endif %}
        {% endfor %} 
        
        
    

{% endif %}


{% if entry.impactStatement|length %}
    {% for block in entry.impactStatement %}
        {% if block.type == "impactStatement" %}
            
                
                    
                        {{block.heading}}
                    
                
            
        {% endif %}
    {% endfor %} 
{% endif %}


{% if entry.largeHeadline|length %}

    
    {% for block in entry.largeHeadline %}
            {% if block.type == "headline" %}
                {% set image = block.image.first() %}
                    
                        
                            
                                
                                
                            
                            
                                {{block.heading}}
                                {{block.paragraph}}
                            
                        
                    
            {% endif %}
    {% endfor %} 
    
    

{% endif %}

As you can see this is just three field types but I have a lot. But for this example the template has them in this order.
Testimonials Matrix Field
Impact Statement
Large Headline
If I move impact statement in the section above testimonials, that has no relation on where that impact statement gets rendered in the entry...
I also tried to do ifelse thinking it would loop through the template outputting the blocks in the order they are in the section for that single if they exist. But this just outputs the testimonials block and stops haha.
{% if entry.testimonials|length %}
//Block Code
{% elseif entry.testimonials|length %}
//Block Code
{% elseif entry.testimonials|length %}
//Block Code
{% endif %}

I thought one way to solve it would just be to have one matrix field with all of the possible fields as a block with each field in that block, but then I think I run into issues wrapping each element in a container as for each block I end up outputting a container so they end up nested.
Any solutions or possible tips would be great. Hope this makes sense.

**Tags:** templating, matrix, config, field

> **Comments:**
> **username_1**: In the craft_fieldlayoutfields table there's a sortOrder column...  

---

## 2 answers

### Answer by username_2 on 2015-05-09. Score: 1 | Accepted answer

If I understand you correctly, you need to be able to create unique "pages", and through some sort of "layout/page builder" create those page's layouts on a page-by-page basis.
Using Singles sections to achieve this is the wrong approach, in my opinion – in fact, creating/managing sections should never be part of the regular editorial workflow, and honestly I can't really imagine a use case where such an approach would even be needed. If it exists, I'd say Craft may not be the best platform for your project. 
In your case, I'd suggest creating a Structure or Channel section called "Pages" to hold your "page" entries. If you want to be able to nest your pages – or need them to be in a specific order – use a Structure, if not a Channel will suffice.
For building the layout, I'd add a single Matrix field to the "Pages" section's entry type, called "Layout" or the like, containing all the possible "fields" or layouts (like "Impact statement") as block types.
The one thing I'm a bit confused about is your issue about having "nested containers" with the above approach. I'm not seeing the problem as you could basically just use your current markup like this, and you would have the same end result as you currently have, only with the correct order:
{% for block in entry.pageLayout %}

    {% switch block.type %}

        {% case 'impactStatement' %}
        
            
                 
                     {{block.heading}}
                 
            
        

        {% case 'largeHeading' %}
        ...

    {% endswitch %}

{% endfor %}

> **Comments:**
> **username_0**: Right I was going to try this, but the issue I had with the containers seemed to be related to the for loop. Say each block type needs its own container div. When I tried this before for each block type it would output that same container div and for some reason was nesting them. I will update example code if this ends up working. Thanks for the suggestion for creating a channel or stucture just not sure what the difference is vs creating a section and editing the section.  
> **username_2**: Your nesting issue sounds like a markup thing - maybe an unclosed tag? You might want to put that issue up as a separate question; specifically highlighting the point where your markup turns weird.  Using sections as entries is a bad idea for many reasons, but the main problem is that you're setting your client/editors up with a needlessly complex and error prone workflow.  
> **username_0**: Wanted to say thanks again for your help, this worked out great, and used this http://craftcms.stackexchange.com/a/1747/3579 for my nesting issue. I created a section called pages, and a matrix field called layout with all the fields so far so good.  
> **username_2**: That's great, @username_0 - happy to hear it!  
> **username_0**: Ran into a nesting issue with testimonials haha http://craftcms.stackexchange.com/questions/9691/matrix-loop-nesting-issue  

---

### Answer by username_3 on 2015-05-08. Score: 1

You could possibly loop through the field layout for that section, and then output the fields in the order that they are defined. In this example, I'm using macros, but you could just as easily place the code within the switch statement directly.
{% macro testimonials(matrix) %}
    {% import _self as self %}

    
        
            

                {% for block in matrix %}
                    {% if block.type == "testimonial" %}

                        
                            
                                {{ block.sourceTitle }}
                            
                            “{{ block.lead }}”
                        

                    {% endif %}
                {% endfor %}

                

                    {% for block in matrix %}
                        {% if block.type == "testimonial" %}
                            {% set image = block.image.first() %}
                            
                        {% endif %}
                    {% endfor %}

                
            
        
    

{% endmacro %}

{% macro impactStatment(matrix) %}
    {% import _self as self %}
    {# format/output matrix data #}
{% endmacro %}

{% macro largeHeadline(matrix) %}
    {% import _self as self %}
    {# format/output matrix data #}
{% endmacro %}


{% for tab in entry.getFieldLayout.getTabs %}

    {% for field in tab.getFields %}

        {% switch field.getField.handle %}

            {% case 'testimonials' %}
                {{ macros.testimonials(entry.testimonials) }}

            {% case 'impactStatement' %}
                {{ macros.impactStatement(entry.impactStatement) }}

            {% case 'largeHeadline' %}
                {{ macros.largeHeadline(entry.largeHeadline) }}

        {% endswitch %}

    {% endfor %}

{% endfor %}

You would obviously need to process and format the output (i.e. loop through the matrix, add html formatting, etc.)

> **Comments:**
> **username_0**: Wouldn't this be the same as adding all input fields as content blocks of a matrix field? Then I would end up having issues wrapping each section in a container or so I think I would, not sure how to stop the loop from nesting containers.  
> **username_3**: I'm not sure you get what I was saying — I'm suggesting that you continue to use your matrix fields, but just output them in the order that the fields are defined — that should take care of the nesting issues. Personally I wouldn't try and make such a catch all template to begin with. As I'm not sure I see the advantage — but if you wanted to do that, this is one way to go about it. I'll update the answer to show an example of a matrix output.  
> **username_0**: The reason I want to do this is so a client can add new "pages" sections without needing to know which template they need to choose... Then they can add what ever input fields they want to that section and not have to worry about the order and output.  
> **username_3**: You might want to check out [superTable plugin](https://github.com/engram-design/SuperTable)... putting a superTable field in a matrix field is essentially the same as placing a matrix in a matrix (which is currently not possible). Then your client could potentially build pages/sections in a structure on the fly without having to resort to creating new sections or editing fieldLayouts in the settings, but with advantage of being able to group items together nicely (i.e. testimonials).  
> **username_3**: FYI... they are working on a version of superTable that supports data added in a single column vs  columns/rows — I think that might suit your use case better. Might want to reach out to the developer and see where they are with that.  

---

