Snippets - Little bits of code to make you happy
-
SnippetsAutomatically ?flush when in 'dev' mode
15 April 2010 | | | Supports v2.4, v2.3
TweetOften when doing development work on a website - and particularly the templates - it can be a pain having to remember to add ?flush to the end of the URL to make sure all of your changes have come through. I'm sure that all of us have had that "d'oh" moment when we have spent too much time wondering why our changes weren't working, only to find a simple flush fixed it.
Continue reading... -
SnippetsUsing Dates in Foreign Languages
17 November 2009 | | | Supports v2.4, v2.3
TweetWith all the translatable, i18n and other translation related stuff in silverstripe, I think more than one is wondering how to achieve something simple as using dates in a foreign language... Actually, it's ultra easy if you add this 2 lines in your silverstripe powered website.
Continue reading... -
SnippetsCreating a Page Export Function
6 May 2009 | | | Supports v2.4, v2.3
TweetNeed to create an 'export' function for a page? Do it quickly and cleanly with HTTP::sendFileToBrowser. In your page controller, add a new action 'export':
Continue reading... -
SnippetsManipulating every Nth item in a <% control %> loop
16 July 2009 | | | Supports v2.4, v2.3
TweetOften you will want to manipulate every nth item inside a <% control %> loop. For example say you had a gallery and you wanted every 3rd image to start on a new line, you need to be able to test each item from the template to see if it is divisible by 3 and should therefore have the clear CSS class added to it.
Continue reading... -
SnippetsDisable Default JavaScript Behaviours
15 February 2010 | | | Supports v2.4, v2.3
TweetBy default SilverStripe uses bulky and sometimes unwanted JavaScript libraries to handle form validation and AJAX page commenting features. Often you want to disable this behaviour so you can integrate your own JavaScript code. This is easily achieved with the following two lines in you site configuration file:
Continue reading... -
SnippetsSelect a theme using a URL parameter
21 December 2009 | |
TweetWhen I develop SilverStripe sites, I usually get functionality working in the default theme blackcandy before getting it working in the final templates. This way, authors can start adding content and seeing how it looks on the page, and I can get early feedback about the functionality. Also, as a developer I get to see real content in the browser early in the process. So does the designer.
Continue reading... -
SnippetsResizing and Manipulating Images
22 March 2009 | | | Supports v2.4, v2.3
TweetYou can resize images from within the template very easily. But sometimes you need to resize it and just use the URL value, something you can't do by calling the resize on the variable directly (e.g. $Image.CroppedImage(200,200)). This is how we would create a cropped resize of it and still be able to use just the URL, allowing a custom <img> tag.
Continue reading... -
SnippetsClearing the Search field on click with jQuery
5 August 2009 | |
TweetAs with everything, it's attention to detail that makes great sites. One thing that really bugs me is when a site has a search box with the word 'search' inside it but which does not clear it self when clicked, meaning I have to clear it before I can search. Given it can be achieved with a few lines of jQuery, it's a real surprise how many sites fail to implement this, or that still use the 'onclick' inline javascript event.
Continue reading... -
SnippetsUsing a Print Stylesheet
16 December 2009 | |
TweetOften people want to be able to print webpages without all the menus and graphics and with all the content fitting correctly onto a page. To do this you need to create a separate print stylesheet. It would look something like this:
Continue reading... -
SnippetsDisplaying Fields From a Set of Pages on Another Page
25 May 2009 | | | Supports v2.4, v2.3
TweetAlthough ultimately requiring very little code, displaying data from a set of pages on another page can seem confusing for those new to SilverStripe. This example will display all of the Images from all the staff pages on our current page. This works by first returning all the staff pages to our template and cycling through each of them, drawing the Image for each one.
Continue reading... -
SnippetsGetting objects from multiple child pages
14 April 2009 | | | Supports v2.4, v2.3
TweetWhen you want to create a DataObjectSet encompasing objects from all the current pages children you can do something like this
$pageIDs = $this->getDescendantIDList(); $ObjectSet = DataObject::get( "Object", "`ObjectPageID` IN (" . implode(",", $pageIDs) . ")" );Continue reading...