app.get('/articles/2003/', function(req, res){
res.send('articles in 2003 year');
});
app.get(/^\/articles\/([0-9]{4})\/$/, function(req, res){
res.send('articles in ' + req.params[0] + ' year');
});
app.get('/user/:id', function(){
// ...
})
var controllers = require('./controllers');
app.get('/articles/2003/', controllers.special_case_2003);
app.get(/^\/articles\/([0-9]{4})\/$/, controllers.year_archive);
app.get('/user/:id', controllers.user);
import re
res = re.compile(r'\w')
tbl = {'ч':'ch'..}
print(res.sub(lambda x: tbl[x.group()], 'чечевица')
function renderTpl($tpl, $data) {
return preg_replace_callback(
"@\{(.+?)\}@",
function($matches) use ($data) {
return isset($data[$matches[1]]) ? $data[$matches[1]] : "";
},
$tpl
);
}
$template = "Hello {first_name} {last_name}!!!";
$data = array(
"first_name" => "John",
"last_name" => "Doe"
);
echo renderTpl($template, $data); //Hello John Doe!!!
function render($template, array $data = array()) {
$content = file_get_content(sprintf('%s/templates/%s.tpl', __DIR__, $template);
return preg_replace_callback('/\{\{\s*([a-z_\-][a-z0-9_\-]*)\s*(\|\s*raw\s*)?\}\}/i', function ($matches) use ($data) {
$needToEscape = !isset($matches[2]);
if (!isset($data[$matches[1]]) {
throw new \Exception(sprintf('Variable "%s" not exists', $matches[1]));
}
$value = $data[$matches[1]];
if ($needToEscape) {
// тут можно придумать чего получше, это просто для примера.
$value = htmlentities($value);
}
return $value;
}, $content);
}
<!-- post.tpl -->
<h1>{{ title }}</h1>
{{ content | raw }}
echo render('post', ['title' => 'Some Title', 'content' => 'Some <strong>content with html</strong>!']);