Jump to:

22997 Posts in 11844 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » RESOLVED: Custom Error Pages

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 3239 Views
  • ckd
    Avatar
    Community Member
    18 Posts

    RESOLVED: Custom Error Pages Link to this post

    I am wanting to create some custom styled error pages. I have had a look through the forums and help topics but just need to clarify the process. For example, if we take the standard 404 error page that is the default which uses Page.ss, how can I get this to use my own template so I can style it separately? I did try creating an ErrorPage.php, ErrorPage.ss but that resulted in a SS warning that there were two ErrorPage.php files ie default in the core and the one I created. I then deleted the ErrorPage.php file that I had created and just left the ErrorPage.ss hoping that SS would treat this in a similar fashion to the templates used when creating a custom login form, however, that did not result in any change.

    With the SS CMS admin area for the 'page not found' there is a drop down list for the various error codes, hence I assume that their obviously is probably an easy way to create all your custom error pages if you are wanting to.

    Is anyone able to please expand on the method used? Any help would be greatly appreciated.

    Thank you

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: RESOLVED: Custom Error Pages Link to this post

    With the default install there is a page created of type ErrorPage called 'Page Not Found' that is set to handle all 404 errors. You can edit the content of that as needed. It is structured as a $Layout so it will retain all your styles and just use the content entered to display. This is designed as a 'catch all' for errors and only 1 may exist (as you found out).

    If you want to handle other Error types, create a new ErrorPage and set the Error Code to the one you want to setup.

    To style the page separately you should be able to create the appropriate ErrorPage.ss to override the default in the theme, just be sure it is in /mysite/templates/ or /themes/your_theme/templates/ErrorPage.ss. Otherwise ErrorPage will always default to the Page.ss style.

  • ckd
    Avatar
    Community Member
    18 Posts

    Re: RESOLVED: Custom Error Pages Link to this post

    Thank you for the information. So, for example, if I wish to override the default 404 error page so that I can style my own and not have it use Page.ss, do I just create a file called ErrorPage.ss in the layouts folder or can this not be done?

    For the other error pages, when you say create a new ErrorPage and set the Error Code, are you referring to something like: ErrorPage403.ss? How do I set the error code? Do I create this file as a .php file as well? Does SS automatically detect the type of error according to how it is named and then act with the appropriate error action?

  • dalesaurus
    Avatar
    Community Member
    283 Posts

    Re: RESOLVED: Custom Error Pages Link to this post

    Well scratch above, ErrorPage handling is totally outside the regular path of SS template/DataObject rendering. There is a little hint here: http://doc.silverstripe.org/doku.php?id=error-handling#friendly_website_errors

    The Debug class will create a flat HTML page in the assets directory like so:

    /assets/error-404.html (regular)
    /assets/error-404-en.html (localization turned on, last 2 letters for your language)

    Dump whatever you want in there as full HTML and you're gold. I'm guessing the reasoning behind this is to reduce rendering load on throwaway error traffic.

  • ckd
    Avatar
    Community Member
    18 Posts

    Re: RESOLVED: Custom Error Pages Link to this post

    Thanks Dale, almost there. I now see that when creating an error page within the CMS, a page is created in the assets folder, using the Page.ss as the template, for each error page that is created.

    The only issue I am still having is trying to figure out how I can give these error pages their own individual styling that is dependent from the rest of the site. I did try changing the code in the error-500.html file that was created, to test to see if the format of the published corresponding error page altered at all, but it did not. Tried creating the ErrorPage.ss file again and put it in templates folder then changed the content but did not alter the appearance of the published error page either.

    I know it will be something simple as you have described but I can't quite get the idea of how it is meant to work. To me, logically, I would just create an ErrorPage.ss with different styling from the rest of the site and throw this in my templates folder. Hence all error pages would be styled according to that file.

    What you stated here seems to make sense but cannot get it to work:

    "To style the page separately you should be able to create the appropriate ErrorPage.ss to override the default in the theme, just be sure it is in /mysite/templates/ or /themes/your_theme/templates/ErrorPage.ss. Otherwise ErrorPage will always default to the Page.ss style."

    In addition to reducing the rendering load on error traffic, I tend to always style my error pages separately from the rest of the site.

  • ckd
    Avatar
    Community Member
    18 Posts

    Re: RESOLVED: Custom Error Pages Link to this post

    Ok, got it sorted. It was as you suggested in your first post:

    "To style the page separately you should be able to create the appropriate ErrorPage.ss to override the default in the theme, just be sure it is in /mysite/templates/ or /themes/your_theme/templates/ErrorPage.ss. Otherwise ErrorPage will always default to the Page.ss style."

    I had neglected to flush the db hence the changes were not showing.

    Thank you for your help!

  • Lime Blast
    Avatar
    Community Member
    22 Posts

    Re: RESOLVED: Custom Error Pages Link to this post

    I found this post as a result of searching for information on setting up custom layouts for error pages.

    As suggested by dalesaurus, creating an ErrrorPage.ss file within `/themes/<theme>/templates/layouts`was the correct thing to do - with the addition of then editing/saving the two error pages within the CMS.

    It would appear that the process of editing/saving the files generates a static copy of error-404.html and error-500.html within the `/assets` folder, which is what the .htdocs file points at for when there is an error.

    (At least that is what worked for me)

    3239 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.