Snippets

sironekotoro Mojolicious::Liteの1枚アプリでCSSをちゃんと分離して書いてみる

Created by sironekotoro
#!/usr/bin/env perl
use Mojolicious::Lite;

my $color = 'black';

get '/' => sub {
    my $c = shift;
    $c->stash( color => $color );
    $c->render( template => 'index' );
};

get '/color' => sub {
    my $c = shift;
    $color = $c->param('color');
    $c->redirect_to('/');
};

app->start;
__DATA__

@@ index.html.ep
% layout 'default';
% title 'Welcome';
<h1>Welcome to the Mojolicious real-time web framework!</h1>

<form action="/color">
  <input type="radio" name="color" value="black">
  <input type="radio" name="color" value="red">
  <input type="radio" name="color" value="blue">
  <input type="radio" name="color" value="green">
  <button type="submit">change</button>
</form>


@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head>
  <title><%= title %></title>
  <%= include 'test.css' =%>
  </head>
  <body><%= content %></body>
</html>

@@ test.css.html.ep
%= stylesheet begin
h1{
    color:<%=$color%>;
}
%= end

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.