Home

Templates

Manipulating every Nth item in a <% control %> loop

Often you will want to manipulate every nth item inside a <% control %> loop. For example say you had a gallery and you wanted every 3rd image to start on a new line, you need to be able to test each item from the template to see if it is divisible by 3 and should therefore have the clear CSS class added to it.

Using iteratorPos % we can very easily achieve this. First we need to add the test function to the object we will be iterating through, so for example if you are iterating through a number of ImageItem DataObjects, we would add this function to our ImageItem class:

The % operator calculates the modulous or Remainder after dividing by the number that follows it (in this case 3). If there is a remainder then we know that the item is not divisible by 3 and so should not be cleared, but if the modulous is equal to 0 then we know it is divisible by 3 and should therefore clear a new line.

Read More >>

Working with banners


Submitted by Nick Jacobs.

Most of the sites I work on are for corporate/business clients and there is not typically a lot of fancy motion graphics going on. I do like to ensure there is some dynamic element going on, just to provide a bit of interest and one of the simplest ways to achieve this is by some variation of the random/rotating banners scenario. Of course this could equally apply to other parts of the page, not just banner graphics.

Over the course of a few projects, trawling the SS forums and hacking together my own interpretations, I have ended up with a few different ways of doing this in SilverStripe.

A random rotating banner

This function gets a set of random banners from a specified folder - in this case BannerImages

Read More >>

Creating Previous and Next Buttons on a page

When you have a number of pages which follow on from one another you may find that asking a user to return to an index or select the next item from a side menu each time they want to progress is cumbersome. In this situation having previous and next buttons at the bottom of a page becomes very useful. In this snippet we'll add these buttons and also a counter to show the current page we are on and the total number of pages.

First add this function to your Page_Controller class:

Let's go through the function. First we pass in a string $Mode. This will be used to decide whether to return the previous or next page.

Read More >>

Create an 'Edit this Page' link

For some sites you may want your client to be able to click a link on a particlar page and be taken strait to that page in the admin area. Here's what a link that does just that would look like:

If you wanted this to only show up when a user is logged in you could use the CurrentMember call to check. So it would look like this:

You could also take this a step further and write a function in your page controller to check whether the current user has permission to access the CMS using Permission::check('CMS_ACCESS_CMSMain') call.

Read More >>

Resizing an image in a custom <img> tag

You can resize images from within the template quite easily. Using an image defined as $Image1 in the class this is how we would create a few different resizes of it.

Read More >>
| 1 2