In this section you will find short snippets of code that we have found useful and often hard to uncover.
05
Apr
1
Check to see if a widget area has any widgets
If you want to check that a WidgetArea actually has widgets, and only
display it if it has, you can add this function to your Page_Controller (where widgetBarID is the name of your widgetArea + ID):
Then in your template file, MyPage.ss you can use HasWidgets to check whether the WidgetArea should be rendered:
29
Mar
0
Create an 'Edit this Page' link
For some sites you may want your client to be able to click a link on a particlar page and be taken strait to that page in the admin area. Here's what a link that does just that would look like:
If you wanted this to only show up when a user is logged in you could use the CurrentMember call to check. So it would look like this:
You could also take this a step further and write a function in your page controller to check whether the current user has permission to access the CMS using Permission::check('CMS_ACCESS_CMSMain') call.
Read More >>24
Mar
2
Get Errors E-mailed to you
If your site is live it means you are probebly not getting errors reported in the view, so it's handy to have them emailed to you so you can still debug.
Add this line to your mysite/_config.php file and expect some mail!
mysite/_config.php
23
Mar
3
Dynamic Year Function
Using this function you can create a dynamic year in the footer of your
site. Simply add the function to your Page_Controller class and use
$Year in your footer template.
This will output "2009" for this year and then "2009-2010" next year etc.
Read More >>22
Mar
9
Resizing an image in a custom <img> tag
You can resize images from within the template quite easily. Using an image defined as $Image1 in the class this is how we would create a few different resizes of it.
Read More >>