Skip to content

Instantly share code, notes, and snippets.

@wouterj
Last active August 29, 2015 13:56
Show Gist options
  • Save wouterj/9136992 to your computer and use it in GitHub Desktop.
Save wouterj/9136992 to your computer and use it in GitHub Desktop.
<?php
class PostListController extends Controller
{
private $viewModel;
public function __construct()
{
$this->viewModel = $this->get('view_model.twig');
$this->on('data.post_list_served', array($this, 'listServed'));
}
/**
* @Route("/blog")
*/
public function listAction()
{
$this->trigger('ui.post_list_requested');
return new Response($this->viewModel->render('Blog:post_list.html.twig'));
}
public function listServed(Event $event)
{
$this->viewModel->addSlot('blog_list', $event->getArgument('markup'));
}
private function on($name, $call)
{
$this->get('event_dispatcher')->addListener($name, $call);
}
private function trigger($name, $event = null)
{
$this->get('event_dispatcher')->dispatch($name, $event);
}
}
<?php
class PostModel implements EventSubscriberInterface
{
private $dispatcher;
private $repository;
private $templating;
public function __construct(EventDispatcherInterface $dispatcher, EntityRepository $repository, BlogTemplateModel $templating)
{
$this->dispatcher = $dispatcher;
$this->repository = $repository;
$this->templating = $templating;
}
public function serveBlogList()
{
$posts = $this->repository->findAll();
$this->dispatcher->dispatcher('data.post_list_served', new GenericEvent(null, array(
'markup' => $this->templating->renderBlogList($posts),
)));
}
public static function getSubscribedEvents()
{
return array(
'ui.post_list_requested' => 'serveBlogList',
);
}
}
<?php
class PostTemplateModel
{
private $templating;
public function __construct(\Twig_Environment $templating)
{
$this->templating = $templating;
}
public function renderPostList($posts)
{
return $this->templating->render('Blog:Post:list.html.twig', array(
'posts' => $posts,
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment