How to iterate over only a specific contenttype ??

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

How to iterate over only a specific contenttype ??

Post by gautamz07 »

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

Code: Select all

    {% 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:

http://imgur.com/a/6eDfN

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

Code: Select all

{% 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:

Code: Select all

{% 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.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: How to iterate over only a specific contenttype ??

Post by gautamz07 »

Thanks celauran :)
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: How to iterate over only a specific contenttype ??

Post by gautamz07 »

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:

Code: Select all

{# {% 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 %} #}
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to iterate over only a specific contenttype ??

Post by Celauran »

Why are you doing this in the template anyhow? Makes more sense (and cleaner code!) in the controller.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: How to iterate over only a specific contenttype ??

Post by gautamz07 »

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

Code: Select all

 {% if theme.layout.aside == "right" %}
     {{ include('partials/_aside.twig') }}
 {% endif %}
and then in the _aside.twig file there is the folliowing:

Code: Select all

<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.
Post Reply