Last active
November 6, 2016 02:21
-
-
Save Shelob9/7179dd12985f9eb14dc1 to your computer and use it in GitHub Desktop.
This file contains 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
{ | |
"name": "shelob9/router-experiment", | |
"require": { | |
"silex/silex": "~1.3", | |
"twig/twig": "^2.0@dev" | |
}, | |
"minimum-stability": "dev", | |
"autoload": { | |
"psr-4": { | |
"shelob9\\router\\": "src" | |
} | |
} | |
} |
This file contains 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> | |
<title>{{ post.title.rendered }}</title> | |
</head> | |
<body> | |
<div id="masthead"> | |
</div> | |
<div id="main"> | |
{% include 'single.twig' %} | |
</div> | |
</body> | |
</html> |
This file contains 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> | |
<title></title> | |
</head> | |
<body> | |
<div id="masthead"> | |
</div> | |
<div id="main"> | |
{% for post in posts %} | |
{% include 'single.twig' %} | |
{% endfor %} | |
</div> | |
</body> | |
</html> |
This file contains 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 | |
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(); | |
} | |
} |
This file contains 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
<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