scratch/multi/blog/Yesod-tutorial-for-newbies.md
2011-12-28 17:15:55 +01:00

4.5 KiB

isHidden: false menupriority: 1 kind: article created_at: 2011-12-28T15:14:40+02:00 en: title: Yesod tutorial for newbies fr: title: Tutoriel Yesod pour les nuls author_name: Yann Esposito author_uri: yannesposito.com tags: - yesod - haskell - programming - web

<%= blogimage("main.png","Title image") %>

begindiv(intro)

en: <%= tldr %>

fr: <%= tlal %>

enddiv

Here it is. You searched for the best technology to handle your new web application. After searching a lot, you finally found the fastest, most reliable language to make it is Haskell.

It is incredibly fast, can handle extremely well parallel tasks, etc...

You start reading a bit about Haskell and you are ready to do some test. First you see you have three different choices of web framework. As you already know Haskell will be a new language to learn, you start slowly by adopting a framework.

The actual choices are:

  1. Happstack
  2. Snap
  3. Yesod

It is hard to get a real idea about which you should choose. But if you read a bit, it seems the one which went the farther is yesod. As it has a lot of module for you. The documentation seems good.

Snap and Happstack both seems simpler. The feeling is closer to the language but that means more work to do on your own. Of course, these are just feeling and you have to try it for yourself. Now you are ready to start a Yesod.

Install

First you need to install Haskell. The recommended way to do this is to use the Haskell Platform.

Secondly you need to install yesod.

> cabal update > cabal install yesod cabal-dev

That is all. It should take some time to do this as cabal will download all package and then compile them.

Initialization

Open a terminal and:

> yesod init

I entered my name, the name of the project was yosog and the name of the Fondation was Yosog, then I chosen sqlite.

Perfect. Now you can start the development cycle:

> cd yosog > cabal-dev install && yesod --dev devel

This will compile the entire project. In the end you should now be able to see your local website by clicking this link:

http://localhost:3000

Congratulation! You were able to see your yesod powered website. For the rest of the tutorial, use another terminal and let this one open in a corner to see what occurs.

Configure git

It is not mandatory for a tutorial, but it is a good practice to have a CVS.

To use git copy this .gitignore file into the yosog folder.

cabal-dev dist .static-cache static/tmp *.sqlite3

Then initialize your git repository:

> git init . > git add . > git commit -a -m "Initial yesod commit"

Now we are ready to modify our web application.

A last point

What did we done:

  1. We have a directory containing a bunch of files
  2. We have a local web server on port 3000

If we modify a file inside this directory, yesod should try to recompile as fast as possible the site. This way, you should see the modification you done.

Instead of explaining the role of every file, let's get straight to the point.

Inside the yosog the important files/directories for this tutorial are:

  1. config/routes
  2. Handler/
  3. templates/
  4. static/
  5. config/models

Obviously:

  • config/routes is where you'll configure the map URL → Code.
  • Handler/ contains the files that will contain the code called when a URL is accessed.
  • templates/ contains HTML, JS and CSS templates.
  • static/ contains static files.
  • config/models is where you'll configure the persistent objects (database tables).

With these informations we should be able to do a lot. Also note until here we don't even typed any line of Haskell.

Let's really start

What could we start to change?

> curl localhost:3000
TODO
  • Display something, show it is protected.
  • Make the same as before, but with an input.
  • Create a minimal blog system.
  • Change template to html5 boilerplate.
  • Use Authentification.