Skip to content

Instantly share code, notes, and snippets.

@Shelob9

Shelob9/composer.json

Last active Nov 6, 2016
Embed
What would you like to do?
{
"name": "shelob9/router-experiment",
"require": {
"silex/silex": "~1.3",
"twig/twig": "^2.0@dev"
},
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"shelob9\\router\\": "src"
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>{{ post.title.rendered }}</title>
</head>
<body>
<div id="masthead">
</div>
<div id="main">
{% include 'single.twig' %}
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="masthead">
</div>
<div id="main">
{% for post in posts %}
{% include 'single.twig' %}
{% endfor %}
</div>
</body>
</html>
<?php
namespace shelob9\router;
class _routes {
function __construct() {
$app = new \Silex\Application();
$app->register(new \Silex\Provider\TwigServiceProvider(), array(
'twig.path' => dirname( __FILE__ ).'/views',
'debug' => true,
));
$app->get('/blog/{name}', function ($name) use ($app) {
$posts = get_posts(
array(
'post_name' => $name,
'post_type' => 'post',
'post_status' => ' publish'
)
);
if( empty( $posts ) ) {
$app->abort(404, "Post $name Not Found!!");
}
$data = array( 'post' => (array) $posts[0] );
return $app['twig']->render( 'post.twig', $data );
})->convert( 'name', function ( $name ) {
return sanitize_title( $name );
});
$app->get('/', function () use ($app) {
$posts = get_posts(
array(
'posts_per_page' => 10,
'post_type' => 'post',
'post_status' => ' publish'
)
);
if( empty( $posts ) ) {
$app->abort(404, "No posts found");
}
foreach( $posts as $post ) {
$data[] = (array) $post;
}
$data = array( 'posts' => $data );
return $app['twig']->render( 'posts.twig', $data );
});
$app->get('/page/{page}', function ($page) use ($app) {
if( 0 == absint( $page ) ) {
return;
}
if( empty( $posts ) ) {
$app->abort(404, "No posts found");
}
foreach( $posts as $post ) {
$data[] = (array) $post;
}
$data = array( 'posts' => $data );
return $app['twig']->render( 'posts.twig', $data );
})->convert( 'page', function ( $page ) {
return (int) $page;
});
$app->run();
}
}
<article id="{{ post.ID|e('html_attr') }}">
<h1>{{ post.post_title }}</h1>
<div class="entry-content">
{{ post.post_content|raw }}
</div>
</article>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment