|
// app/resources/views/breadcrumbs.html.twig |
|
// this file will be included with the 'breadcrumbs' variable |
|
<nav aria-label="You are here:" role="navigation"> |
|
<ul class="breadcrumbs"> |
|
<li> |
|
<a href="/"> |
|
<i class="fa fa-home"></i> |
|
</a> |
|
</li> |
|
{% for crumb in breadcrumbs %} |
|
<li class="{{ crumb.class }}"> |
|
{% if 'disabled' not in crumb.class and not loop.last%} |
|
<a href="{{ crumb.path }}"> |
|
{% endif %} |
|
{% if loop.last %} |
|
<span class="show-for-sr">Current: </span> |
|
{% endif %} |
|
{{ crumb.label }} |
|
{% if 'disabled' not in crumb.class and not loop.last %} |
|
</a> |
|
{% endif %} |
|
</li> |
|
{% endfor %} |
|
</ul> |
|
</nav> |
|
|
|
// CONTROLLER EXAMPLES |
|
|
|
/* simple controller action with two-deep breadcrumb */ |
|
public function productAction() |
|
{ |
|
// get a product object |
|
$em = $this->getDoctrine()->getManager(); |
|
$products = $em->getRepository('CommonContentBundle:Product')->findAll(); |
|
|
|
// build the breadcrumbs array |
|
$breadcrumbs = array( |
|
array( |
|
'label' => $product->getCategory()->getTitle(), |
|
'path' => $this->generateUrl('product_category',array('slug'=>$product->getCategory()->getSlug())), |
|
'class' => null |
|
), |
|
array( |
|
'label' => $product->getTitle(), |
|
'path' => $this->generateUrl('product_detail',array('slug'=>$product-getSlug())), |
|
'class' => null |
|
) |
|
); |
|
|
|
// an additional piece here can be used to create a conditional parent crumb for self referencing associations (parent/child) |
|
|
|
if ($product->getParent()) { |
|
$parentArray = array( |
|
'label' => $product->getParent()->getTitle(), |
|
'path' => $this->generateUrl('product_detail', array('slug'=>$product->getParent()->getSlug())), |
|
'class' => 'parent' |
|
) |
|
array_splice($breadcrumbs, 1, 0, $parentArray); |
|
} |
|
return $this->render('::product.html.twig', array( |
|
'breadcrumbs' => $breadcrumbs, |
|
'product' => $product |
|
)); |
|
} |
|
|
|
// product.html.twig |
|
{% if breadcrumbs is defined %} |
|
{% block breadcrumbs %} |
|
{% include "::breadcrumbs.html.twig" with breadcrumbs %} |
|
{% endblock %} |
|
{% endif %} |
|
|