Tutorial Step 9 - Other useful tools
In this last tutorial step we will show how to use some other specific Erlang Web's features.
Contents
Switching the web servers
Since Erlang Web provides web server's level abstraction we do not need to bother about the server we will be running on the top of. Nevertheless in some situation we would like to change it and try to run the service on the different type of the software. In order to switch from the Inets (default one, included in the standard OTP release) to Yaws (included in Erlang Web, homepage: http://yaws.hyber.org) we must change only two things:
edit the configuration file config/yaws.conf properly (change the server's callback module to either e_mod_yaws or e_fe_mod_yaws depending on the node type mode)
change the running command from ./bin/start_interactive to ./bin/start_interactive yaws
Unless we used the server specific functions (what is not recommended though), we should be able to run the server immediately.
Switching the database back end
So far, we were using Mnesia (standard OTP DBMS) as our model holder. But it is no big deal to switch to CouchDB backend - it could be done in as easy way as switching the web servers. In order to do this, we must do the following things:
edit the project configuration file config/project.conf and put there the following entries
{dbms, couchdb}.
{couchdb_address, URL} - when is different than http://localhost:5984/
{project_name, Name} - when we want to use different databases than default erlangweb and erlangweb_ids (erlangweb is the default project name)
Of course there is no need to call mnesia:create_table/2 any more. Since CouchDB is a schema-less database we do not need to set it up before use.
Template inheritance
The template inheritance has been described fully here.
Paginating the items list
As the service is getting larger and larger it will be totally unreasonable to list all the available items on the one page. We should better think about partitioning and paginating the content. It could be done by using wpart_paginate mechanism. The browser:list_all/1 saves the properly formatted items under the items key in the request dictionary. Let's see how we can change our view to display only a part of the collection, for example - 5 items per page:
change the dispatcher rule that points at the /item/all URL from "^/item/all$" to "^/item/all" - we will use GET parameter to specify the page number
edit the item/show_all.html template so it looks like:
<html> <head> <title>Erlang Web Shop - All Items</title> </head> <body> <h1>All items that are stored in the shop:</h1> <ul> <wpart:paginate action="page" as="items_page" list="items" per_page="5"> <wpart:list select="map" list="items_page" as="item"> <li> <wpart:choose> <wpart:when test="not {item:available}"> NOT AVAILABLE </wpart:when> </wpart:choose> <a wpart:href="/item/show/{[integer]item:id}"><wpart:lookup key="item:name" /></a> </li> </wpart:list> </wpart:paginate> </ul> <wpart:paginate action="prev_link" text="Previous items" /> || <wpart:paginate action="next_link" text="Next items" /> </body> </html>
And that is really all...
