Jump to content

Content Database Slice/Break Loop & Add Class to First Loop


Revast

Recommended Posts

Posted

Hello,

I'm in the process of setting up a news/article database for my front page and would love some help on two issues that I'm having, if it's possible.

I'd like to create a featured news slider for the page, I currently have everything setup so that it only pulls records from the database that are mapped as 'featured' and (foreach) loops the template I've created for each one. I'd like to slice/break the loop so that it only displays a certain amount.

Another thing I'm interested in doing is adding a class to the first looped element (a list item).

Thank you for your time!

Posted
{{$limit = 5;}}
{{foreach $records as $pos => $record}}
{{if $pos < $limit }}
<!--within our manual limit-->
{{if !$pos }}
<!--first record-->
{{endif}}
{{endif}}
{{endforeach}}

It's not technically an array, so this approach is probably the cleanest, as you can't call most array management functions on an iterator.

Posted

Thank you, that worked! However, it brings up a new issue. I have the condition to only display articles mapped as 'featured' in the template that comes after the loop. This causes the loop limit you just provided to count non-featured articles. Is there anyway I can have the limit only count 'featured' articles?

My code:

<ul id='newsFeatured' class='ipsClearfix'>
	{{$limit = 5;}}
	{{foreach $articles as $id => $record}}
		{{if $id < $limit}}
			{template="featured" app="cms" location="database" group="news_index" params="$record, $database"}
		{{endif}}     
	{{endforeach}}
</ul>

And then my featured template is just:

{{if $record->mapped('featured')}}
	<li class='featuredSlide'>
        <!-- stuff -->
	</li>
{{endif}}

 

Posted

I see. Been there, didn't think you would need it.

{{$limit = 5;}}
{{$pos = 0;}}
{{foreach $records as $record}}
{{if $pos < $limit }}
<!--within our manual limit-->
{{if !$pos }}
<!--first record-->
{{endif}}
{{$pos++;}}
{{endif}}
{{endforeach}}

This is just making our own 'pointer' instead of using the iterator's.

That said, this kind of code is mostly handy for when you need to filter in a way the widget/feed filters do not suffice, or in a way a query could not manage... There is indeed a 'Featured' Filter in the widget configuration, since ~4.0.9, that should be used instead in this scenario.

Posted

Hey Marcher,

I tried creating a new database feed block that used the 'Featured' only filter and placed it alongside the database on my page, but I didn't want it showing up on all the databases pages. The block also never worked correctly, even with the filter it still displayed all the records for some reason.

I just created custom 'Articles' templates (index/entry) and 'Display' templates for the database and just edited those with all of this. I can't seem to get what you provided working on these templates though. Limiting the loop to 5 was the only thing I managed to get working, is there something with these templates parameters/variables that we may have to consider?

Posted

In certain templates, the $records variable is instead named $articles. Beyond that, I can't imagine what you are hitting, and would advise debugging the data:

{expression="var_export($variable, true)"}

The block also never worked correctly, even with the filter it still displayed all the records for some reason.

I would suggest submitting a ticket on this though, if a filter is provided it should work.... :sweat:

Posted

I got the block working correctly, everything is on point now! I ended using js to get the class on the first loop.

Thanks for the help and sorry for all the trouble.

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...