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... -
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... -
SnippetsRemoving an Item From the Main CMS Menu
3 July 2009 | |
TweetIf you want to remove an item from the CMSTopMenu without making it inaccessible you can add this line to your _config.php file:
CMSMenu::remove_menu_item('SecurityAdmin');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... -
SnippetsPreventing malicious SQL injections
12 March 2010 | | | Supports v2.4, v2.3
Tweet
PHP has a very shallow learning curve, it's free and anyone can have a go at making a website by following a few tutorials and implementing their experience with other languages. However, coding for the web can be a risky business, especially with dynamic websites that take some kind of user (or external) input and use that to get data from a database.
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... -
SnippetsSpice up Your CMS Sitetree
27 August 2009 | | | Supports v2.4, v2.3
Tweet
So you have created your own page types, but they all look the same in your CMS site tree. Time to make them more recognizable!
This is quite easy, but it can make your site tree a lot more attractive.
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... -
SnippetsOverriding Silverstripes language files with your own
28 October 2009 | |
TweetIn my most recent project I've had to run a site in another language (Swedish) and run into some issues which I thought I'd share with all of you. Now Silverstripe probably comes with a translation for your language but even if it does, you probably want to change the wording of some of the phrases, so what to do? You could of course hit up the language files for the relevant module or the ones in cms and sapphire but you really don't want to touch these files if you can help it.
Continue reading... -
SnippetsHow to access your admin account when you've forgotten the password
22 August 2009 | |
Tweet I managed to forget my admin password and found quite a few ways to unlock an SS install. I originally thought about poking around the database and changing the hashes myself. After reviewing the Security and password encryption code, I would recommend against doing that. SilverStripe uses salted SHA1 encryption which is no fun to try and create yourself. Also you'd have to update multiple tables to get the password changed. Luckily the SS Core team has included multiple features for accomplishing this. The options below assume you have access to your webserver to use some PHP code. 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... -
SnippetsUsing canCreate(), canEdit(), canDelete() and canPublish() to manage page type permissions
23 June 2009 | | | Supports v2.4, v2.3
TweetOften you will want to control which users can create, edit, delete and publish certain page types. For example you may only want high level users to be able to create ContactPage page types, or prevent low level users from deleting HomePage page types. This can easily be achieved by adding these functions to the page type model (usually just before getCMSFields()). Then within the function you can define conditionals which decide whether to return true or false.
Continue reading... -
SnippetsExtending $LinkingMode to handle controller actions
20 October 2009 | |
TweetAs some of you might have noticed, as soon as you step outside of the standard SiteTree structure you're pretty much on your own. Today I had to make a menu that mixed normal SiteTree pages and controller actions.
Continue reading... -
SnippetsSilverStripe developer bookmarklets
2 April 2012 | | | Supports v2.4, v2.3
Tweet
Here are some handy dynamic bookmarklets for your browser for one click access to things like rebuilding the database, flushing the cache and accessing the CMS!
Continue reading... -
SnippetsCustomise Form Actions to use the <button> tag
12 June 2010 | |
TweetBy Default, SilverStripe uses the <input type="submit"> tag for it's form actions. However, sometimes you may want to use the <button> tag to give you a little more control over styling. This snippet shows you how, as well as giving you code to achieve the same thing in UserForms!
Continue reading...