Yaws

## The source for the shopping cart

The shoppingcart contains a number of tricks and "preferred" ways to code different typical solutions to servers side state applications. The source itself resides in the www/shoppingcart directory in the source code tree. A link to the source itself to view from the browser.

### The first trick, the `break` statement

The source of all yaws pages (including example index.yaws) look very similar in this application, specifically

    out(A) ->
        case shopcart:top(A) of
            ok ->
                shopcart:index(A);
            X ->
                X
        end.

All code, including the head containing the link to the stylesheet is dynamically generated. The first function in all code snippets in yaws files is always a call to `shopcart:top(A)`

The `top/1` function will check the cookie and if a cookie which is associated to an active session is found, the request is granted, otherwise the login page is displayed by the `shopcart:login(A)` function.

The last item displayed by the login function is the atom `break`. When the yaws server sees the `break`, it will not process any more data from the yaws page being requested. Thus, we can have the login call at the top of a yaws page and still have subsequent html/yaws code on the same page.

### Redirect to the requested page

Since the `login(A)` function is supplied with the Arg of the requested page, the login function will set the path of the requested page in a hidden field in the login form. The code that processes the login form will then issue a redirect to the value of this hidden field if the login in successful.

### Ehtml

The use of the ehtml output syntax makes it more convenient to dynamically generate structured content:

- It is easier to get the structure right,

- It is easier to inject the dynamically generated parts in the structure,

- It is much more beautiful.

See the source for a number of "proofs" for this.

### The use of the yaws_session_server

The yaws_session_server is designed to facilitate server side state applications. The yaws application writer is supplied with the following capabilities and services:

1. Truly random cookie generation. Each cookie can subsequently be used to uniquely identify a session.

2. Maintenance of an opaque application state in an ets table. This state is readily available to the yaws page by means of the cookie.

3. Old idle sessions are garbage collected.