Skip to content

Instantly share code, notes, and snippets.

@trev
Last active December 15, 2015 08:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

document.write() - Dealing with the SS template engine

Details

When using an HTML boilerplate such as H5BP or Zurb Foundation, you might encounter an error with a line simliar to:

<script>document.write('<script src=$ThemeDir/javascript/vendor/' + ('__proto__' in {} ? 'zepto' : 'jquery') + '.js><\/script>');</script>

Unfortunately, the above line wont work since it'll pass through the SS template engine first. You therefore need to escape the </script> tag within the document.write() a bit differently.

  <script>document.write('<script src=$ThemeDir/javascript/vendor/' + ('__proto__' in {} ? 'zepto' : 'jquery') + '.js>\\x3C/script>');</script>

The vendor folder permissions

Details

Many HTML boilerplates and frontend frameworks use the vendor naming convention for a place to put 3rd party resources such as jQuery & Modernizr. Unforunately, SilverStripe has an .htaccess directive which forbids access to any folder names vendor since it's used by Composer and contains sensitive configuration information about your SS setup.

In order to fix this problem, edit your .htaccess at the root of your SS installation like so:

<IfModule mod_alias.c>
  RedirectMatch 403 /silverstripe-cache(/|$)
  # This is the standard SS config, but it will block all vendor folders when we don't want
  # it to block vendor folders under themes/saeq/ 
	# RedirectMatch 403 /vendor(/|$)
	RedirectMatch 403 ^/vendor(/|$)
	RedirectMatch 403 /composer\.(json|lock)
</IfModule>

By adding the caret character (^) we're telling .htaccess to only restrict access to the vendor folder at the root of the SS folder

@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