Last active
August 29, 2015 14:18
-
-
Save defeo/843efb1d741411a133bd to your computer and use it in GitHub Desktop.
Templates, données, état
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
bodyP = require('body-parser'), | |
twig = require('twig'); | |
var app = express(); | |
app.set('views', '.'); | |
app.use(bodyP.urlencoded({ extended : false })) | |
.use('/s', express.static('.')); | |
app.get('/signin', function(req, res) { | |
res.sendFile(__dirname + '/form.html'); | |
}); | |
app.get('/hello', function(req, res) { | |
res.render('hello.twig', { | |
'nom' : req.query.nom, | |
'couleur': req.query.couleur, | |
'rouges' : ['cerise', 'fraise', 'sang'], | |
'jaunes' : ['soleil', 'citron', 'banane'] | |
}); | |
}); | |
app.post('/hello', function(req, res) { | |
res.render('hello.twig', { | |
'nom' : req.body.nom, | |
'couleur': req.body.couleur, | |
'rouges' : ['cerise', 'fraise', 'sang'], | |
'jaunes' : ['soleil', 'citron', 'banane'] | |
}); | |
}); | |
app.get('/bye', function(req, res) { | |
res.render('bye.twig', { 'nom' : req.query.nom }); | |
}); | |
app.post('/bye', function(req, res) { | |
res.render('bye.twig', { 'nom' : req.body.nom }); | |
}); | |
app.get('/:nom/compteur/', function(req, res) { | |
res.render('compteur.twig', { | |
'nom' : req.params.nom, | |
'count' : 0 | |
}); | |
}); | |
app.get(RegExp('^/([^/]+)/compteur/([0-9]+)$'), function(req, res) { | |
console.log(req.params); | |
res.render('compteur.twig', { | |
'nom' : req.params[0], | |
'count' : req.params[1] | |
}); | |
}); | |
app.listen(process.env.PORT); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!Doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Bye</title> | |
</head> | |
<body> | |
<p>Au revoir, {{ nom }}</p> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!Doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Hello</title> | |
</head> | |
<body> | |
<p> | |
Bonjour {{ nom }}, | |
{% if not count %} | |
ceci est votre première visite! | |
{% elseif count < 1000000 %} | |
vous avez visité cette page {{ count }} fois. | |
{% else %} | |
félicitations, vous êtes le millionième visiteur ! | |
{% endif %} | |
</p> | |
<p><a href="./{{ count + 1 }}">Visitez à nouveau.</a></p> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!Doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Formulaire</title> | |
</head> | |
<body> | |
<form method="get" action="/hello"> | |
<input type="text" name="nom" placeholder="choisissez un pseudo"><br> | |
<input type="radio" name="couleur" value="r" > rouge<br> | |
<input type="radio" name="couleur" value="j" > jaune<br> | |
<input type="submit" value="Entrer"> | |
<input type="submit" value="Entrer (post)" formmethod="post"> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!Doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Hello</title> | |
<style> | |
ul { | |
color: {{ couleur == "r" ? "red" : "yellow" }}; | |
} | |
</style> | |
</head> | |
<body> | |
<p>Bonjour {{ nom }} !</p> | |
<ul> | |
{% if couleur == "r" %} | |
{% for o in rouges %} | |
<li>{{ o }}</li> | |
{% endfor %} | |
{% else %} | |
{% for o in jaunes %} | |
<li>{{ o }}</li> | |
{% endfor %} | |
{% endif %} | |
</ul> | |
<p><a href="/bye?nom={{ nom }}">au revoir...</a></p> | |
<form action="/bye" method="post"> | |
<input type="hidden" name="nom" value="{{ nom }}"> | |
<input type="submit" value="au revoir (post) ..."> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
use Silex\Application; | |
use Symfony\Component\HttpFoundation\Request; | |
$app = new Application(); | |
// Configuration | |
$app['debug'] = true; | |
$app->register(new Silex\Provider\TwigServiceProvider(), | |
array('twig.path' => '.',)); | |
$app->get('/signin', function(Application $app, Request $req) { | |
return $app->sendFile('form.html'); | |
}); | |
$app->get('/hello', function(Application $app, Request $req) { | |
return $app['twig']->render('hello.twig', array( | |
'nom' => $req->query->get('nom'), | |
'couleur'=> $req->query->get('couleur'), | |
'rouges' => array('cerise', 'fraise', 'sang'), | |
'jaunes' => array('soleil', 'citron', 'banane') | |
)); | |
}); | |
$app->post('/hello', function(Application $app, Request $req) { | |
return $app['twig']->render('hello.twig', array( | |
'nom' => $req->request->get('nom'), | |
'couleur'=> $req->request->get('couleur'), | |
'rouges' => array('cerise', 'fraise', 'sang'), | |
'jaunes' => array('soleil', 'citron', 'banane') | |
)); | |
}); | |
$app->get('/bye', function(Application $app, Request $req) { | |
return $app['twig']->render('bye.twig', array( | |
'nom' => $req->query->get('nom') | |
)); | |
}); | |
$app->post('/bye', function(Application $app, Request $req) { | |
return $app['twig']->render('bye.twig', array( | |
'nom' => $req->request->get('nom') | |
)); | |
}); | |
$app->get('/{nom}/compteur/', function(Application $app, Request $req, $nom) { | |
return $app['twig']->render('compteur.twig', array( | |
'nom' => $nom, | |
'count' => 0 | |
)); | |
}); | |
$app->get('/{nom}/compteur/{cnt}', function(Application $app, Request $req, $nom, $cnt) { | |
return $app['twig']->render('compteur.twig', array( | |
'nom' => $nom, | |
'count' => $cnt | |
)); | |
})->assert('cnt', '\d+'); | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment