Skip to content

Instantly share code, notes, and snippets.

@trev
Last active December 15, 2015 08:49
Show Gist options
  • Save trev/5233816 to your computer and use it in GitHub Desktop.
Save trev/5233816 to your computer and use it in GitHub Desktop.
SilverStripe 3.0 Recipes, Errors, Tips and Tricks

SilverStripe 3.0 Recipes, Errors, Tips and Tricks

RECIPES

Get a specific page and access its children

Possible usage

Get the children from a specific page and add them to the main menu

Code

<% with Page("about-us") %>
  <% loop Children %>
    $MenuTitle.XML
  <% end_loop %>
<% end_with %>

Access information from another page

Possible usage

Displaying news items on the homepage or in a sidebar

Code

public function LatestNews($num = 5) {
  //  Get the new article holder
  $holder = ArticleHolder::get()->First();
  
  // Get the latest 5 news article 
  return ($holder) ? ArticlePage::get()->filter('ParentID', $holder->ID)->sort('Date DESC')->limit($num) : false;
}

Notes

The above mentioned code would usually go in the controller of the page type. It'll return an array that can then be access in template with <% loop LatestNews %>

Recommendations

The controller for a page is only created when page is actually visited, while the data object is available when the page is referenced in other pages, e.g. by page controls. A good rule of thumb is to put all functions specific to the page currently being viewed in the controller; only if a function needs to be used in another page should you put it in the data object.

References

Overriding a constructor - Execute code on instantiation

Possible usage

Whenever you want a chunk of code to run as soon as a controller class is initialized.

Code

public function init() {
  // Code here ...
  // Call the parent method we're overriding and run that too so the chain of execution continues
  parent::init();
}

Notes

Typically you'd overwrite the __constuct() method provided by default with PHP but SilverStripe uses a the custom method init() instead.

ERRORS

CMS File upload error: SyntaxError: Unexpected token E

Details

  • Add an uploadField() field to the CMS
  • Try to upload a file using the newly added field
  • You may get error: SyntaxError: Unexpected token E

Solution

Set write permission (potentially 777 depending on setup) to assets/Uploads

TIPS AND TRICKS

CMS Page type description

Details

Add a page type description to increase user friendliness

Code

static $description = 'This is a descriptive text about this page type';

Prepare array for view in template

Details

Normal PHP arrays cannot be displayed in SS templates. You need to wrap them with ArrayData.

Code

$list = Array(1 => "Cool", 2 => "Cooler");
return ArrayData($list);

jQuery.entwine debug inspector for CMS

Details

Similiar to Google Chrome Developer Tools, the jQuery.entwine inspector allows you to see what entwine methods are bound to a specific element.

Hotkey

CTRL+` // CTRL + Backtick
@LimeBlast
Copy link

Just what I needed to know, thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment