PHP Developers Network
http://forums.devnetwork.net/

How to iterate over only a specific contenttype ??
http://forums.devnetwork.net/viewtopic.php?f=72&t=143106
Page 1 of 1

Author:  gautamz07 [ Tue Nov 01, 2016 3:29 am ]
Post subject:  How to iterate over only a specific contenttype ??

I have a new difficulty :P , just need some help with this. I have the following lines of code in my aside.twig file:

Syntax: [ Download ] [ Hide ]
    {% for ct in app.config.get('contenttypes') if not ct.viewless|default(false) %}
   
                {% setcontent records = ct.slug ~ "/latest/3" %}
   
                <h5>{{ __('contenttypes.generic.recent', {'%contenttypes%': ct.slug}) }}</h5>
                <ul>
                    {% for record in records %}
                        <li><a href="{{ record.link }}">{{ record.title }}</a></li>
                    {% else %}
                        <li>{{ __('contenttypes.generic.no-recent', {'%contenttype%': ct.slug}) }}</li>
                    {% endfor %}
                </ul>
                <p><a href="{{ paths.root }}{{ct.slug}}">{{ __('contenttypes.generic.overview',{'%contenttypes%': ct.slug}) }} &raquo;</a></p>
   
   
            {% endfor %}


This basically iterates over all the contenttypes in my `contenttypes.yml` file and i get the following in front end:



Now what i want is only recent entries, so i tried modifying the following line of code:

Syntax: [ Download ] [ Hide ]
{% for ct in app.config.get('contenttypes') if not ct.viewless|default(false) %}


To the following:

{% for ct in app.config.get('contenttypes') if not ct.viewless|default(false) and app.config.get('contenttypes') == "entries" %}

But i get an error , so how exactly do i modify the following line of code:

Syntax: [ Download ] [ Hide ]
{% for ct in app.config.get('contenttypes') if not ct.viewless|default(false) %}


So that i get only the records of "entries" contenttype ??

Thank you.
Gautam.

Author:  Celauran [ Tue Nov 01, 2016 6:47 am ]
Post subject:  Re: How to iterate over only a specific contenttype ??

https://docs.bolt.cm/3.1/templating/content-fetching

Author:  gautamz07 [ Tue Nov 01, 2016 12:14 pm ]
Post subject:  Re: How to iterate over only a specific contenttype ??

Thanks celauran :)

Author:  gautamz07 [ Tue Nov 01, 2016 12:41 pm ]
Post subject:  Re: How to iterate over only a specific contenttype ??

I have actually referenced this doc a few times , sorry i did't know why it skipped me :( . For now i have removed the for loop and just added the below lines of code:

Syntax: [ Download ] [ Hide ]
{# {% for ct in app.config.get('contenttypes') %} #}
       {# {% for ct in app.config.get('contenttypes') if not ct.viewless|default(false) where { name : 'Entries' } %} #}

            {# {% if ct.name == 'Entries' %} #}
           {% setcontent records = "Entries/latest/3" %}
                {# {% else %} #}
                   {# {% return; %} #}
           {# {% endif %} #}


            <h5>{{ __('contenttypes.generic.recent', {'%contenttypes%': ct.slug}) }}</h5>
            <ul>
                {% for record in records %}
                    <li><a href="{{ record.link }}">{{ record.title }}</a></li>
                {% else %}
                    <li>{{ __('contenttypes.generic.no-recent', {'%contenttype%': ct.slug}) }}</li>
                {% endfor %}
            </ul>
            <p><a href="{{ paths.root }}{{ct.slug}}">{{ __('contenttypes.generic.overview',{'%contenttypes%': ct.slug}) }} &raquo;</a></p>

        {# {% endfor %} #}
 

Author:  Celauran [ Tue Nov 01, 2016 2:26 pm ]
Post subject:  Re: How to iterate over only a specific contenttype ??

Why are you doing this in the template anyhow? Makes more sense (and cleaner code!) in the controller.

Author:  gautamz07 [ Tue Nov 01, 2016 3:37 pm ]
Post subject:  Re: How to iterate over only a specific contenttype ??

@celauran the way it is in the default theme is there is this _master.twig file, which has the following code:

Syntax: [ Download ] [ Hide ]
 {% if theme.layout.aside == "right" %}
     {{ include('partials/_aside.twig') }}
 {% endif %}


and then in the _aside.twig file there is the folliowing:

Syntax: [ Download ] [ Hide ]
<div class="panel">
    {# The next section attempts to fill the panel in the sidebar with the contents of the
      'about-us'-block, if it exists. First, attempt to fetch the block with slug 'about-us' #}
   {% setcontent block = "block/about-us" %}
    {# check if we have a title. If so, we know we have content to display. Otherwise, we will output a default text, telling the user to create the 'blocks' in the backend. #}
   {% if block.title is defined %}

        <h5>{{ block.title }}</h5>
        {{ block.content }}
        {% if link(block.contentlink) or block.editlink() %}
            <p>
                {{ link(block.contentlink, __('general.phrase.read-more')) }} /
                <a href="{{ block.editlink() }}">{{ __('general.phrase.edit') }}</a>
            </p>
        {% endif %}

    {% else %}

        <h5>{{ __('general.phrase.missing-about-page') }}</h5>

        <p>{{ __('general.phrase.missing-about-page-block') }}</p>

    {% endif %}

</div>
 



I am just basically trying to modify the default theme to suit my requirements , I am not quite familiar 100% with MVC , but do you have a more efficient way to do this ?

P.S. some of the code i post might make your eyes bleed :P sorry about that , i do intend on focusing on efficiency as i keep learning.

Thank you.

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/