SilverStripe Templates

Enjoy this cheat sheet at its fullest within Dash, the macOS documentation browser.

Template Elements

Variables

$Property
$Property(param)
$Property.SubProperty

Escaping

$Foopx // returns "" (as it looks for a Foopx value)
{$Foo}px  // returns "3px" (CORRECT)
$$Foo // returns ""
${$Foo} // returns "$3"
$Foo // returns "3"
\$Foo // returns "$Foo"

Includes

sample="<% include SideBar %>"

The include tag can be particularly helpful for nested functionality. In this example, the include only happens if a variable is true

sample="<% if $CurrentMember %>
          <% include MembersOnlyInclude %>
        <% end_if %>"

Images

Images in Templates

$Image.SetWidth(80) // returns a image 80px wide, ratio kept the same
$Image.SetHeight(80) // returns a image 80px tall, ratio kept the same
$Image.SetSize(80,80) // returns a 80x80px padded image
$Image.SetRatioSize(80,80) // Returns an image scaled proportional, with its greatest diameter scaled to 80px
$Image.CroppedImage(80,80) // Returns an 80x80 image cropped from the center.
$Image.PaddedImage(80, 80) // Returns an 80x80 image. Unused space is padded white. No crop. No stretching
$Image.Width // returns width of image
$Image.Height // returns height of image
$Image.Orientation // returns Orientation
$Image.Filename // returns filename
$Image.URL // returns filename

Images in the Page PHP

// manipulation functions
$image->resize(width,height); // Basic resize, just skews the image
$image->resizeRatio(width,height) // Resizes an image with max width and height
$image->paddedResize(width,height) // Adds padding after resizing to width or height.
$image->croppedImage(width,height) // Crops the image from the centre, to given values.
$image->resizeByHeight(height) // Maximum height the image resizes to, keeps proportion
$image->resizeByWidth(width) // Maximum width the image resizes to, keeps proportion 
$image->greyscale(r,g,b) // alters image channels ===

// values
$image->getHeight() // Returns the height of the image.
$image->getWidth() // Returns the width of the image
$image->getOrienation() // Returns a class constant: ORIENTATION_SQUARE or ORIENTATION_PORTRAIT or ORIENTATION_LANDSCAPE

Loops

Children Loop

sample = "<% loop $Children %>...<% end_loop %>"

Will loop over all children of the current page context. Helpful to create page-specific subnavigations. Most likely, you'll want to use <% loop $Menu %> for your main menus, since its independent of the page context.

Children Loop of page-by-url

sample="<% loop $ChildrenOf(<my-page-url>) %>...<% end_loop %>"

Will loop over all children of the current page context. Helpful to create page-specific subnavigations. Most likely, you'll want to use <% loop $Menu %> for your main menus, since its independent of the page context.

sample="<% loop $allChildren %>...<% end_loop %>"

Children Loop all children even if ShowInMenus is false

sample="<% loop $allChildren %>...<% end_loop %>"

Will loop over all children of the current page context. Helpful to create page-specific subnavigations. Most likely, you'll want to use <% loop $Menu %> for your main menus, since its independent of the page context.

Access Pages, Levels, Specific Pages

Access level

sample = "<% with $Level(1) %>
              $Title
          <% end_with %>"

Will return a page in the current path, at the level specified by the numbers. It is based on the current page context, looking back through its parent pages.

Access specific page

sample = "<% with $Page(my-page) %>...<% end_with %>"

"Page" will return a single page from the site tree, looking it up by URL. You can use it in the <% loop %> format. Can't be called using $Page(my-page).Title.