mojolicious, real-time web framework

26
mojolicious real-time web framework NPW2012, Stockholm Lars Thegler

Upload: taggg

Post on 15-Jan-2015

643 views

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Mojolicious, real-time web framework

mojoliciousreal-time web framework

NPW2012, Stockholm Lars Thegler

Page 2: Mojolicious, real-time web framework

meta

I’m not a mojolicious expert

I will talk about how to start

I will talk about what to expect

I will hint at how to continue

Page 3: Mojolicious, real-time web framework

agenda

Mojolicious::Lite

routes and placeholders

templates and layouts

graduating from ::Lite to Mojolicious proper

Mojo as client

Page 4: Mojolicious, real-time web framework

mojolicious - what? who?

“real-time web application framework”

written by Sebastian Riedel (sri) &c

who also wrote Catalyst

current core: ams, tempire, mramberg

Page 5: Mojolicious, real-time web framework

timeline

first release 0.2 2008-09-24

release 1.0 ‘snowflake’ 2010-12-26

release 2.0 ‘leaf fluttering in the wind’ 2011-10-17

release 3.0 ‘rainbow’ 2012-06-25

release 3.46 2012-10-11

Page 6: Mojolicious, real-time web framework

installation

no dependencies

# cpanm Mojolicious

easy curl recipe

# curl get.mojolicio.us | sh

Page 7: Mojolicious, real-time web framework

execution

builtin (simple, morbo, hypnotoad)

nginx

apache/mod_proxy, CGI

PSGI/plack

Page 8: Mojolicious, real-time web framework

anatomy of aweb framework

incoming request =>

dispatcher =>

action code =>

outgoing response

Page 9: Mojolicious, real-time web framework

dispatcher

small language

matching incoming request => action code

“routes”

Page 10: Mojolicious, real-time web framework

getting started

# GET /

get '/' => sub {

my $self = shift;

$self->render(text => 'Hello');

};

# 200 OK, text/html, Hello

Page 11: Mojolicious, real-time web framework

placeholder

# /hello/Lars

get '/hello/:name' => sub {

my $self = shift;

my $name = $self->param(‘name’);

$self->render(text => “Hello $name”);

};

# 200 OK, text/html, Hello Lars

Page 12: Mojolicious, real-time web framework

url parameters# /hello/Lars?greet=Hej

get '/hello/:name' => sub {

my $self = shift;

my $name = $self->param(‘name’);

my $greet = $self->param(‘greet’);

$self->render(text => “$greet $name”);

} => ‘greeting’;

# 200 OK, text/html, Hej Lars

Page 13: Mojolicious, real-time web framework

placeholders and routes, next level

slurpy placeholders

wildcard placeholders

formats (‘.html’, ‘.txt’)

bridging routes

conditional routes

Page 14: Mojolicious, real-time web framework

stash

nonpresistent data store

keep data for the duration of the request

carries data from action code to template

with a bunch of reserved keys

format, text, template, ...

$self->stash(name => ‘Lars’);

Page 15: Mojolicious, real-time web framework

template

builtin ‘Embedded Perl’

variables in templates from stash

instantiated as normal values

Hello, <%= $name %>

Page 16: Mojolicious, real-time web framework

structures in templates

It’s all Perl, so Perl control structures just work:

% for my $i (0..3) { ... <%= $i %> ...% }

Page 17: Mojolicious, real-time web framework

inline templates

__DATA__

@@ hello.html.epHello, World!

@@ goodbye.html.epGoodbye, and thanks!

Page 18: Mojolicious, real-time web framework

template selection

explicit template name in render() call

$self->render(template => ‘hello’);

name in stash

$self->stash(template => ‘hello’);

defaults to name of router

get ‘/welcome’ => ‘hello’;

Page 19: Mojolicious, real-time web framework

auto render

no need to call render() yourself

if you don’t, mojo will do it for you

get ‘/’ => ‘index’;

__DATA__@@ index.html.ep

Page 20: Mojolicious, real-time web framework

layouts

__DATA__@@ index.html.ep% layout ‘default’Hello, World!

@@ layouts/default.html.ep<html> <head><title>moo</title></head> <body><%= $content %></body></html>

Page 21: Mojolicious, real-time web framework

templates, next level

includes

partial templates

404, 500 pages

blocks

template inheritance

...

Page 22: Mojolicious, real-time web framework

sessions

built in

data stored in cookies, client-side

cookie signed to prevent tampering

you can use something else, if you want

Page 23: Mojolicious, real-time web framework

growing out of ::Lite

inflate inline templates into proper files

move routes into application class

move action code into controller class

testing with Test::Mojo

Page 24: Mojolicious, real-time web framework

but wait, there’s more!

Mojo::UserAgent

nice web client

Mojo::DOM

excellent HTML/XML parser

ojo

for one-liners

Page 25: Mojolicious, real-time web framework

resources

http://mojolicio.us

Mojolicious::Guides::{Routing,Rendering,Growing,...}

http://mojocasts.com by tempire

https://github.com/kraih/mojo

incl wiki