Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] [Outdated] Added a first implementation of a BreadcrumbRenderer #45

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions src/Knp/Menu/Renderer/BreadcrumbRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace Knp\Menu\Renderer;

use \Knp\Menu\ItemInterface;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First \ is not required.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed. I missed it because I created the file by copying the ListRenderer


/**
* Renders MenuItem tree as a breadcrumb
*/
class BreadcrumbRenderer extends Renderer implements RendererInterface
{
private $defaultOptions;

/**
* @param array $defaultOptions
* @param string $charset
*/
public function __construct(array $defaultOptions = array(), $charset = null)
{
$this->defaultOptions = array_merge(array(
'additional_path' => null,
'compressed' => false,
'root_attributes' => array(),
), $defaultOptions);

parent::__construct($charset);
}

/**
* Renders a menu with the specified renderer.
*
* @param \Knp\Menu\ItemInterface $item
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for full namespace as this is added in use

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way is used everywhere in KnpMenu because PhpStorm 3 does not support use statement fully in phpdoc (it looks at them when generating the phpdoc but not when reading it), and I wrote the library using PhpStorm 3. I think the version 4 fixes it but I haven't switched to it yet.
and anyway, changing this is out of the scope of the PR as it affects the whole library

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(now works pretty good in phpstorm)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know. The code in master has already been updated to use short class names a long time ago

* @param array $options
* @return string
*/
public function render(ItemInterface $item, array $options = array())
{
$options = array_merge($this->defaultOptions, $options);

$breadcrumb = $item->getBreadcrumbsArray($options['additional_path']);

if (empty($breadcrumb)) {
return '';
}

return $this->renderBreadcrumb($breadcrumb, $options);
}

/**
* Renders the breadcrumb
*
* @param array $breadcrumb
* @param array $options
* @return string
*/
protected function renderBreadcrumb(array $breadcrumb, array $options)
{
$html = $this->format('<ul'.$this->renderHtmlAttributes($options['root_attributes']).'>', 'ul', 0, $options);
$html .= $this->renderList($breadcrumb, $options);
$html .= $this->format('</ul>', 'ul', 0, $options);

return $html;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why not allowing <ol> too ? =)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the reason why there is a bunch of protected methods: you can extend the class to customize it.

}

/**
* Renders the breadcrumb list
*
* @param array $breadcrumb
* @param array $options
* @return string
*/
protected function renderList(array $breadcrumb, array $options)
{
$html = '';
foreach ($breadcrumb as $label => $uri) {
$html .= $this->renderItem($label, $uri, $options);
}

return $html;
}

/**
* @param string $label
* @param string $uri
* @param array $options
* @return string
*/
protected function renderItem($label, $uri, array $options)
{
// opening li tag
$html = $this->format('<li>', 'li', 1, $options);

// render the text/link inside the li tag
if (null === $uri) {
$content = $this->renderLabel($label, $options);
} else {
$content = sprintf('<a href="%s">%s</a>', $this->escape($uri), $this->renderLabel($label, $options));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at this i wonder if it would not be easier to use a twig template that the user can overwrite

}
$html .= $this->format($content, 'link', 1, $options);

// closing li tag
$html .= $this->format('</li>', 'li', 1, $options);

return $html;
}

protected function renderLabel($label, array $options)
{
return $this->escape($label);
}

/**
* If $this->renderCompressed is on, this will apply the necessary
* spacing and line-breaking so that the particular thing being rendered
* makes up its part in a fully-rendered and spaced menu.
*
* @param string $html The html to render in an (un)formatted way
* @param string $type The type [ul,link,li] of thing being rendered
* @param integer $level
* @param array $options
* @return string
*/
protected function format($html, $type, $level, array $options)
{
if ($options['compressed']) {
return $html;
}

switch ($type) {
case 'ul':
case 'link':
$spacing = $level * 4;
break;

case 'li':
$spacing = $level * 4 - 2;
break;
}

return str_repeat(' ', $spacing).$html."\n";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just about readability of the html source code, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed. This is used to render some nice HTML (which can be changed by setting the compressed option to true, like other renderers)

}
}