NAME

Jifty::Manual::Tutorial - Zero to Jifty in a Jiffy

DESCRIPTION

This tutorial should give you everything you need to build your first application with Jifty.

HOW TO

The requirements

Installing Jifty

No bones about it. We believe pretty strongly in the DRY (Don't Repeat Yourself) principle. That's one of the big reasons we love Perl and CPAN. Jifty makes use of lots of amazing code from CPAN. At last count, it directly depended on 60 packages from CPAN. Most of these libraries are cross-platform pure-perl packages and should run great out of the box on any platform you can get Perl onto.

We've gone to lengths to make sure you don't spend your day downloading library after library by bundling everything we can inside the Jifty package. With luck, all you'll need to install is a few tricky libraries that actually need to be compiled for your operating system. (Little things like perl's database interface and the embedded SQLite that Jifty defaults to.)

You can either grab a complete Jifty package from http://jifty.org/download or install from CPAN. If you get the slim version from CPAN, you'll have to install Jifty's dependencies yourself. (Though we help out with that where we can.) If you want to get up and running quickly, grab:

http://download.jifty.org/pub/jifty/jifty-latest.tgz

Either way, the installation process is the same:

# tar xzvf jifty-<version>tgz
# cd jifty-version
# perl Makefile.PL
# make
# make test
# make install

If the tests don't pass, we want to hear about it. Please join us on jifty-devel@lists.jifty.org and report the failure. (See "GETTING HELP" below for info on how to join the list.)

Setting up the Scaffolding

Once you have Jifty happily installed, you're ready to create your first application.

Jifty is intentionally a bit minimalist. All you really need to make an application go is a copy of the jifty commandline tool (inside your new application's bin/ directory.

Of course, it's often helpful to have a bit more structure around to help guide your work. Jifty comes with tools to build that structure for you:

Change directory to some place it will be safe to create a new Jifty application. (Jifty will create a subdirectory for you).

# jifty app --name MyWeblog

Creating new application MyWebLog
Creating directory bin
Creating directory etc
Creating directory doc
Creating directory log
Creating directory web
Creating directory web/templates
Creating directory web/static
Creating directory lib
Creating directory lib/MyWebLog
Creating directory lib/MyWebLog/Model
Creating directory lib/MyWebLog/Action
Creating directory t

Let's take those one by one

bin

Inside bin/, you'll find jifty, the Jifty command dispatcher. Some of the most important commands are schema, which sets up or updates your database schema and server, which starts u a standalone webserver.To find out what commands your jifty comes with, run:

C<jifty help>
etc

Configuration files live in etc/, though if you don't have a config file, Jifty will supply some sane defaults

doc

Jifty won't magically write your documentation for you, but when you write your docs, you should drop them in docs/.

log

Jifty uses Log::Log4perl to configure its logging. By default, it dumps logs named server.log and error.log into the log directory.

web/templates

Jifty uses HTML::Mason as its primary templating system. You should drop your application's templates into web/templates/. Out of the box, Jifty comes with an application skeleton that it installs in share/web/templates/. This default application is a convenient way to get a basic application up and running quickly, but probably wants some customization as you build a more advanced application.

You can find where Perl stuck Jifty's default templates with this oneliner:

perl -MJifty::Util -e 'print Jifty::Util->share_root'
web/static

Some nontrivial percentage of the stuff your web application serves out doesn't need to be (or shouldn't be) passed through your templating engine.

Just drop your static files into web/static/ and Jifty will serve them out if it can't find a template with the right name.

Out of the box, Jifty comes with a CSS style, Javascript libraries and a Pony. Have a look in /usr/local/share/jifty/web/static.

lib/MyWebLog

For a full treatment of the Jifty object model see Jifty::Manual::ObjectModel.

To build a basic Jifty application, you only need to worry about two sorts of classes, Models and Actions.

lib/MyWebLog/Model

The real base of your application lives in lib/AppName/Model. Classes here define your application's data structures and how they relate to each other. Jifty will use your model classes to set up and upgrade your database's schema when it needs to.

lib/MyWebLog/Action

When we said you only need to worry about Models and Actions, we weren't telling the whole truth. Jifty will take care basic database-interaction (CREATE, READ, UPDATE, DELETE) Actions for your Models, but they're there if you want to change anything.)

t

Jifty starts off your application with a basic harness, but can't yet write all your tests for you. (It does, however, build up simple tests for model classes you generate.)

Building your data model

As you might imagine by the fact that this tutorial application is named MyWebLog, we're building a simple weblog application. In future tutorials, we'll add authentication, comments, RSS and Atom feeds.

Posts

Weblogs tend to center around posts, so it's no surprise that the first model we'll be creating is the post.

# cd MyWeblog
# jifty model --name post
Writing file /tmp/MyWeblog/t/00-model-Post.t
Writing file /tmp/MyWeblog/lib/MyWeblog/Model/Post.pm

Great! Now you've got a Post model. Not that it models anything yet.

Open lib/MyWeblog/Model/Post.pm in your favorite text editor.

You should see something like this:

package MyWeblog::Model::Post::Schema;
use Jifty::DBI::Schema;

# You column definitions go here.  See L<Jifty::DBI::Schema> for
# documentation about how to write column definitions.

package MyWeblog::Model::Post;
use base qw/MyWeblog::Record/;

# Your model-specific methods go here.

1;

Now it's time to tell the model class about posts. We're going to start by giving our post a body and a title and a category. (In a future tutorial, we'll become fully folksonomy-compliant and upgrade that category to a tags table.

Position your cursor right after:

# You column definitions go here.  See L<Jifty::DBI::Schema> for
# documentation about how to write column definitions.

Add the following lines:

column title =>
       type is 'text',
       label is 'Title',
       default is 'Untitled post';

column body => 
       type is 'text',
       label is 'Content',
       render_as 'Textarea';

Save your model class.

Setting up the database

Ok. It's time to initialize MyWeblog's database. By default, Jifty sets up your application with the SQLite database engine. If you'd rather use PostgreSQL or MySQL, you'll need to add some content to etc/jifty.yml. (Have a look at Jifty::Config for a bit more information).

# jifty schema --setup INFO - Generating SQL for application MyWeblog... INFO - Using MyWeblog::Model::Post INFO - Using Jifty::Model::Schema INFO - Set up version v0.0.1

Starting the Jifty application server

Ok. You have a working, if simplistic, application. Start up a webserver and have a look around. Be sure to check out the AJAX-enabled administrative UI, online documentation browser and the Pony.

# ./bin/jifty/server INFO - You can connect to your server at http://localhost:8888/

Building a user interface

So, the administrative web does give you everything you need to work with your application's data, but it's not much of a weblog.

Posting

Let's create a page to post a new weblog entry:

# cd web/templates/

Open a new file called post in your text editor. Make it look like this:

<%init>
my $action = Jifty->web->new_action(class =>'CreatePost');
</%init>

<&|/_elements/wrapper, title => "Post to your weblog" &>
<% Jifty->web->form->start() %>
<% Jifty->web->form->next_page( url => '/') %>
<% $action->form_field('title') %>
<% $action->form_field('body') %>
<% Jifty->web->form->submit( label => 'Post' ) %>
<% Jifty->web->form->end() %>
</&>

Viewing

it's really easy to get a basic listing of entries and a little bit more complex to get a pretty AJAXified paged list. We'll show you how to do both and you can decide which one works best for you.

The quick and dirty way

Open a new file called index.html in your text editor. (Your webserver will treat /index.html as the default page for your site). Make it look like this:

<%init>
my $posts = Jifty::Model::PostCollection();
$posts->unlimit();
</%init>

<&|/_elements/wrapper, title => Jifty->config->framework('AppName') &>
<dl>
% while (my $post = $posts->next) {
 <dt><%$post->title%></dt>
 <dd><%$post->body%></dd>
% }
</dl>
</&>

The complex way that gets you lots of cool toys

The complex way involves using one of Jifty's advanced features: Page regions. These regions let your application reload page sections independently, either using AJAX on modern, high end, browsers or regular GET requests with downlevel browsers like lynx, w3m or the browser on your mobile phone.

The downside of this approach is that each separate region needs to live in its own fragment file.

So, we'll start off about the same:

Open a new file called web/templates/index.html in your text editor. Fill it with this:

<&|/_elements/wrapper, title => Jifty->config->framework('ApplicationName') &>

<% Jifty->web->region(name => "myweblog-posts",
                      path => "/fragments/page_of_posts") %>
</&>

If you're on the ball, you've probably already guessed that you need to create a file called web/templates/fragments/page_of_posts and stuff it with this

<%args>
$page => 1
</%args>
<%init>
my $posts = MyWeblog::Model::PostCollection->new();
$posts->unlimit();
$posts->set_page_info( current_page => $page,
                           per_page     => 25
                          );
$m->out("No items found.") if ($posts->pager->total_entries == 0);

</%init>
% if ($posts->pager->last_page > 1) {
   Page <% $page %> of <% $posts->pager->last_page %>
% }
<dl class="list">
% while (my $post = $posts->next) {
 <dt><%$post->title%></dt>
 <dd><%$post->body%></dd>
% }
</dl>

% if ($posts->pager->previous_page) {
  <% Jifty->web->link( label => "Previous Page", onclick => { args => { page => $posts->pager->previous_page } } ) %>
% }
% if ($posts->pager->next_page) {
  <% Jifty->web->link( label => "Next Page", onclick => { args => { page => $posts->pager->next_page } } ) %>
% }

And now, fire up your Jifty webserver again. Go create a post by browsing /post on your webserver.

Of course, having to remember the URL to get to the posting page is a bit annoying. To get a Post button in the menu, you'll need to override the default menus:

Jifty's default menus live in _elements/nav in the default application template (along with the Pony)

For now, you'll need to override _elements/nav. (We're working on ways to make this better.

Inside your applications web/templates directory, create a directory called _elements.

Inside _elements/nav, open up a new file called nav in your text editor and insert this content:

<%init>
my $top = Jifty->web->navigation;
$top->child( Home => url => "/", sort_order => 1 );
$top->child( Post  => url => "/post", label      => "Post", sort_order => 2);
return();
</%init>

For more information about the menu system, see the documentation in Jifty::Menu.

That's it!

And that's just about everything you need to get started building Jifty applications. We're working hard to make Jifty even easier to use and to obsolete the hard bits of this tutorial as quickly as we can.

Please join us on the jifty-devel mailing list to talk about how you're using Jifty or what you find difficult or hard to use about it.

GETTING HELP

Joining the mailing list

jifty-devel@lists.jifty.org is where we discuss how we're building Jifty, what we're having trouble with and so on.

To join the list, send mail to jifty-devel-subscribe@lists.jifty.org.

Browsing the wiki

We've got a wiki! (Actually, the wiki is Jifty's primary website)

Please visit http://jifty.org, browse and contribute.

The wiki is powered by Wifty, a Wiki built on Jifty. Its code is freely available from the jifty subversion repository.

REPORTING BUGS

At this incredibly early stage in its life, please report bugs in Jifty to jifty-devel@lists.jifty.org.

FUTURE TUTORIALS

Access Control and Security

Upgrading your application's data model

Deploying your application in production

Web Services

Continuations in depth