How to Use Silverstripe Built-in Page Controls

Ever wonder when you use $Title and <% Control Children %> what else you can call in the templates? This page is here to help with a guide on what template controls you can call.

Controlling Menus Datafeeds:
Main menu – <% control Menu(1) %><% end_control %>
Submenu – <% control Menu(2) %><% end_control %>

Create a datafeed of the children of the given page:

Controlling Certain Pages
<% control Level(1) %>, <% control Level(2) %>, $Level(1).Title, $Level(2).Content, etc

Returns the current section of the site that we’re in, at the level specified by the numbers. For example, imagine you’re on the page about us > staff > bob marley:

<% control Level(1) %> would return the about us page
<% control Level(2) %> would return the staff page
<% control Level(3) %> would return the bob marley page

<% control Page(my-page) %>$Title<% end_control %>

“Page” will return a single page from the site tree, looking it up by URL. You can use it in the <% control %> format. Can’t be called using $Page(my-page).Title.
Page controls that can be used anywhere

These are defined in the data-object and so can be used as nested page controls. Lucky us! We can control Children of Children of Children for example.
Conditional Logic

SilverStripe supports a simple set of conditional logic

<% if Foo %>
// if Foo is true or an object do this
<% else_if Bar %>
// if Bar is true or an object do this
<% else %>
// then do this by default
<% end_if %>

See more information on conditional logic on templates.
Site wide settings

Since 2.4.0, SilverStripe provides a generic interface for accessing global properties such as Site name or Site tag line. This interface is implemented by the SiteConfig class.
Controlling Parents and Children
<% control Children %>

This will return the children of the current page as a nested datafeed. Useful for nested navigations such as pop-out menus.
<% control AllChildren %>

This will show all children of a page even if the option ‘show in menus?’ is unchecked in the tab panel behavior.
<% control Parent %> or $Parent.Title, $Parent.Content, etc

This will return the parent page. The $ variable format lets us reference an attribute of the parent page directly.
Site Navigation – Breadcrumbs
<% control Breadcrumbs %>

This will return a breadcrumbs widgets for the given page. You can call this on any data-object, so, for example, you could display the breadcrumbs of every search result if you wanted. It has a few options.
<% control Breadcrumbs(3) %>

Will return a maximum of 3 pages in the breadcrumb list, this can be handy if you’re wanting to put breadcrumbs in a place without spilling
<% control Breadcrumbs(3, true) %>

Will return the same, but without any links. This is handy if you’re wanting to put the breadcrumb list into another link tag.
Links and Classes
$LinkingMode, $LinkOrCurrent and $LinkOrSection

These return different linking modes. $LinkingMode provides the greatest control, outputting 3 different strings:

link: Neither this page nor any of its children are current open.
section: A child of this page is currently open, which means that we’re currently in this section of the site.
current: This page is currently open.

A useful way of using this is in your menus. You can use the following code below to generate an or on your links. Take the following code
<li><a href=”$Link”>$Title</a></li>

When viewed on the Home page it will render like this
<li><a href=”home/”>Home</a></li>

$LinkOrCurrent ignores the section status, returning link instead. $LinkOrSection ignores the current status, returning section instead. Both of these options can simplify your CSS when you only have 2 different cases to consider.
<% if LinkOrCurrent = current %>

This is an alternative way to set up your menus – if you want different HTML for the current menu item, you can do something like this:

<% if LinkOrCurrent = current %>
<strong>$Title</strong>
<% else %>
<a href=”$Link”>$Title</a>
<% end_if %>
<% if LinkOrSection = section %>

Will return true if you are on the current page OR a child page of the page. Useful for menus which you only want to show a second level menu when you are on that page or a child of it
<% if InSection(page-url) %>

Date and Time
$Now.Nice, $Now.Year

$Now returns the current date. You can call any of the methods from the Date class on it.
$Created.Nice, $Created.Ago

$Created returns the time the page was created, $Created.Ago returns how long ago the page was created. You can also call any of methods of the Date class on it.
$LastEdited.Nice, $LastEdited.Ago

$LastEdited returns the time the page was modified, $LastEdited.Ago returns how long ago the page was modified. You can also call any of methods of the Date class on it.
DataObjectSet Options

If you are using a DataObjectSet you have a wide range of methods you can call on it from the templates
<% if Even %>, <% if Odd %>, $EvenOdd

These controls can be used to do zebra-striping. $EvenOdd will return ‘even’ or ‘odd’ as appropriate.
<% if First %>, <% if Last %>, <% if Middle %>, $FirstLast

These controls can be used to set up special behavior for the first and last records of a datafeed. <% if Middle %> is set when neither first not last are set. $FirstLast will be ‘first’, ‘last’, or ”
$Pos, $TotalItems

$TotalItems will return the number of items on this page of the datafeed, and Pos will return a counter starting at 1.
$Top

Below is a list of fields and methods that are typically available for templates (grouped by their source) – use this as a quick reference (not all of them are described above):

All methods available in Page_Controller

$NexPageLink, $Link, $RelativeLink, $ChildrenOf, $Page, $Level, $Menu, $Section2, $LoginForm, $SilverStripeNavigator, $PageComments, $Now, $LinkTo, $AbsoluteLink, $CurrentMember, $PastVisitor, $PastMember, $XML_val, $RAW_val, $SQL_val, $JS_val, $ATT_val, $First, $Last, $FirstLast, $MiddleString, $Middle, $Even, $Odd, $EvenOdd, $Pos, $TotalItems, $BaseHref, $Debug, $CurrentPage, $Top

All fields available in Page_Controller

$ID, $ClassName, $Created, $LastEdited, $URLSegment, $Title, $MenuTitle, $Content, $MetaTitle, $MetaDescription, $MetaKeywords, $ShowInMenus, $ShowInSearch, $HomepageForDomain, $ProvideComments, $Sort, $LegacyURL, $HasBrokenFile, $HasBrokenLink, $Status, $ReportClass, $ParentID, $Version, $EmailTo, $EmailOnSubmit, $SubmitButtonText, $OnCompleteMessage, $Subscribe, $AllNewsletters, $Subject, $ErrorCode, $LinkedPageID, $RedirectionType, $ExternalURL, $LinkToID, $VersionID, $CopyContentFromID, $RecordClassName

All methods available in Page

$Link, $LinkOrCurrent, $LinkOrSection, $LinkingMode, $ElementName, $InSection, $Comments, $Breadcrumbs, $NestedTitle, $MetaTags, $ContentSource, $MultipleParents, $TreeTitle, $CMSTreeClasses, $Now, $LinkTo, $AbsoluteLink, $CurrentMember, $PastVisitor, $PastMember, $XML_val, $RAW_val, $SQL_val, $JS_val, $ATT_val, $First, $Last, $FirstLast, $MiddleString, $Middle, $Even, $Odd, $EvenOdd, $Pos, $TotalItems, $BaseHref, $CurrentPage, $Top

All fields available in Page

$ID, $ClassName, $Created, $LastEdited, $URLSegment, $Title, $MenuTitle, $Content, $MetaTitle, $MetaDescription, $MetaKeywords, $ShowInMenus, $ShowInSearch, $HomepageForDomain, $ProvideComments, $Sort, $LegacyURL, $HasBrokenFile, $HasBrokenLink, $Status, $ReportClass, $ParentID, $Version, $EmailTo, $EmailOnSubmit, $SubmitButtonText, $OnCompleteMessage, $Subscribe, $AllNewsletters, $Subject, $ErrorCode, $LinkedPageID, $RedirectionType, $ExternalURL, $LinkToID, $VersionID, $CopyContentFromID, $RecordClassName

Looking for quality SilverStripe Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , | Posted under SilverStripe CMS | RSS 2.0

Author Spotlight

Teet Bergmann

Teet Bergmann

I'm a freelance web designer from Estonia. Mostly doing sites using Silverstripe, magento, prestashop, wordpress.

Leave a Reply

Your email address will not be published. Required fields are marked *


9 × 2 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>