branaway / Japid

A Java-based statically-typed fast template engine that can be used in any Java code. It has special adapter for use with the Play! Framework.
113 stars 18 forks source link

How to override 404 and 500 pages using japid ? #62

Open PerfectCarl opened 11 years ago

PerfectCarl commented 11 years ago

Play lets us override the default page hander for 404 and 500 errors.

views/errors/404.html

<!DOCTYPE html>

<html>
    <head>
        <title>Not found</title>
        <meta http-equiv="Content-Type" content="text/html; charset=${_response_encoding}"/>    
    </head>
    <body>
        #{if play.mode.name() == 'DEV'}
            #{404 result /}
        #{/if}
        #{else}
            <h1>Not found</h1>
            <p>
                ${result.message}
            </p>
        #{/else}
    </body>
</html>

views/errors/500.html

<!DOCTYPE html>

<html>
    <head>
        <title>Application error</title>
        <meta http-equiv="Content-Type" content="text/html; charset=${_response_encoding}"/>    
    </head>
    <body>
        #{if play.mode.name() == 'DEV'}
            #{500 exception /}
        #{/if}
        #{else}
            <h1>Oops, an error occured</h1>
            #{if exception instanceof play.exceptions.PlayException}
                <p>
                    This exception has been logged with id <strong>${exception.id}</strong>.
                </p>
            #{/if}
        #{/else}
    </body>
</html>

What is the japid way to do it ?

branaway commented 11 years ago

One way is to change the default 404 page to refer to a japid version, eg: (in app/views/errors/404.html)

<html>
    <head>
        <title>Not found</title>
    </head>
    <body>
        #{if play.mode.name() == 'DEV'}
            #{404 result /}
        #{/if}
        #{else}
        ${cn.bran.play.JapidPlayRenderer.renderWith("japidviews.error404").raw()}
        #{/else}
    </body>
</html>

Then do whatever you want with an Japid based "error404.html" in you japidviews folder.

The above example let your Japid 404 page to handle 404 in prod mode.