Get the children from a specific page and add them to the main menu
<% with Page("about-us") %>
<% loop Children %>
$MenuTitle.XML
<% end_loop %>
<% end_with %>
Displaying news items on the homepage or in a sidebar
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;
}
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 %>
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.
- http://doc.silverstripe.org/framework/en/topics/datamodel
- http://doc.silverstripe.org/framework/en/tutorials/2-extending-a-basic-site
Whenever you want a chunk of code to run as soon as a controller class is initialized.
public function init() {
// Code here ...
// Call the parent method we're overriding and run that too so the chain of execution continues
parent::init();
}
Typically you'd overwrite the __constuct()
method provided by default with PHP but SilverStripe uses a the custom method init()
instead.
- 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
Set write permission (potentially 777 depending on setup) to assets/Uploads
Add a page type description to increase user friendliness
static $description = 'This is a descriptive text about this page type';
Normal PHP arrays cannot be displayed in SS templates. You need to wrap them with ArrayData.
$list = Array(1 => "Cool", 2 => "Cooler");
return ArrayData($list);
Similiar to Google Chrome Developer Tools, the jQuery.entwine inspector allows you to see what entwine methods are bound to a specific element.
CTRL+` // CTRL + Backtick
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>
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
Just what I needed to know, thank you :)