Jump to:

3447 Posts in 1032 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » [Solved] HasManyFileManager: get last column, put css class to every nth item

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

Page: 1
Go to End
Author Topic: 1513 Views
  • derralf
    Avatar
    Community Member
    24 Posts

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

    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>

    1513 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.