Skip to content

Instantly share code, notes, and snippets.

@headzoo
Last active October 20, 2017 00:02
Show Gist options
  • Save headzoo/cbe987903e240f090ec8c3a72e90ffff to your computer and use it in GitHub Desktop.
Save headzoo/cbe987903e240f090ec8c3a72e90ffff to your computer and use it in GitHub Desktop.
{% extends "AppBundle::base.html.twig" %}
{% block title %}Something{% endblock %}
{% block body %}
<h1>Favorites</h1>
<p>Here are you favorites.</p>
<div id="favorites-container">
{% include "AppBundle:user:favorites_list.html.twig" %}
</div>
<button id="load-btn">Next Page</button>
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script type="text/javascript">
$("#load-btn").on("click", function() {
fetch('/u/headzoo/favorites')
.then(function(resp) {
$('#favorites-container').html(resp);
});
});
</script>
{% endblock %}
<ul>
{% for e in events %}
<li>{{ e }}</li>
{% endfor %}
</ul>
<?php
class UserController extends Controller
{
/**
* @Route("/u/{username}/favorites", name="profile_favorites")
*
* @param string $username
* @return Response
*/
public function favoritesAction(Request $request, $username)
{
$em = $this->getDoctrine()->getManager();
$user = $this->findUserOrThrow($username);
// Get the last 25 videos the user favorited.
$events = $em->getRepository("AppBundle:Favorite")
->findByUser($user, 25);
// For ajax requests, send the list of favorites as a "snippet" of html.
if ($request->isXmlHttpRequest()) {
return $this->render("AppBundle:user:favorites_list.html.twig", [
"user" => $user,
"events" => $events
]);
// Otherwise send the whole page.
} else {
return $this->render("AppBundle:user:favorites.html.twig", [
"user" => $user,
"events" => $events
]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment