Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[Solved] HasManyFileManager: get last column, put css class to every nth item


Go to End


2549 Views

Avatar
derralf

Community Member, 28 Posts

25 March 2009 at 10:38pm

Edited: 26/03/2009 2:38am

Hello Community,

as i found nothing about this problem in the forum that worked for me, i want to post my solution.
Maybe this will help some newbies like me

I wanted do build a small Gallery with HasManyFileManager and needed something like this with floating elemts:
IMG margin IMG margin IMG margin IMG
IMG margin IMG margin IMG margin IMG

So i need to check evety nth item to put whatever in my template code.
As postet here by Sam, i can not access some iterator function placed in Page_Controller or Page class from within my <% control ... %>-function

This is how i finally did it:
I can now define the name/group and number of columns in my template (it's more flexible than defining this in my php file - maybe i will have multiple HasManyFileManager on a page or want different numbers or columns on different pages) and i don't lose the sorting of the images that i've done in HasManyFileManager:

GalleryPage.php

class GaleriePage extends Page {
......
	function getCMSFields() {
	......
		$minigallery = new HasManyFileManager(
		   $this,
		   'GalleryImages', // name -> will be used for file grouping
		   'Images' // relation name defined in $has_many
		);
	$fields->addFieldToTab('Root.Content.Galerie', $minigallery);
	......
	}

......

	function AttachedFilesColumns($Group,$Cols=0) {
		$Data = $this -> AttachedFiles($Group);
		$output = new DataObjectSet(); 	
		$i = 0;
		foreach($Data as $Data) {
			$i++;
			if($Cols != 0) {
				if($i % $Cols == 0) {
					$Data->Col = "last";
					$i = 0;
				} else {
					$Data->Col = $i;
				}
			} else {
				$Data->Col = $i;
			}			
			
			$output->push($Data);
		}
		return $output;
	}



}

now in my template instead of "<% control AttachedFiles(GalleryImages) %>" i can use "<% control AttachedFilesColumns(GalleryImages,4) %>" where argument1 is the group name used by HasManyFileManager and argument2 is the number of columns.

Example:

<% control AttachedFilesColumns(GalleryImages,4) %>
   <div class="GalerieThumb-$Col"><a>img<a></div>
<% end_control %>

Output:
<div class="GalerieThumb-1"><a>img<a></div>
<div class="GalerieThumb-2"><a>img<a></div>
<div class="GalerieThumb-3"><a>img<a></div>
<div class="GalerieThumb-last"><a>img<a></div>
<div class="GalerieThumb-1"><a>img<a></div>
<div class="GalerieThumb-2"><a>img<a></div>
<div class="GalerieThumb-3"><a>img<a></div>
<div class="GalerieThumb-last"><a>img<a></div>
....

now the last "column" in "each row" is accessible via css
(or i could use an if-block to check the $Col-output and place tr-tags for a table in the code)

-----------------------

wihtout argument2 for number of columns

<% control AttachedFilesColumns(GalleryImages) %>
   <div class="GalerieThumb-$Col"><a>img<a></div>
<% end_control %>

Output:

<div class="GalerieThumb-1"><a>img<a></div>
<div class="GalerieThumb-2"><a>img<a></div>
<div class="GalerieThumb-3"><a>img<a></div>
<div class="GalerieThumb-4"><a>img<a></div>
<div class="GalerieThumb-5"><a>img<a></div>
<div class="GalerieThumb-6"><a>img<a></div>