-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #764 from KnpLabs/lazy-twig-extension
improve Twig extension
- Loading branch information
Showing
5 changed files
with
102 additions
and
74 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace Knp\Bundle\PaginatorBundle\Twig\Extension; | ||
|
||
use Knp\Bundle\PaginatorBundle\Helper\Processor; | ||
use Knp\Bundle\PaginatorBundle\Pagination\SlidingPaginationInterface; | ||
use Twig\Environment; | ||
use Twig\Extension\RuntimeExtensionInterface; | ||
|
||
final class PaginationRuntime implements RuntimeExtensionInterface | ||
{ | ||
private Processor $processor; | ||
|
||
public function __construct(Processor $processor) | ||
{ | ||
$this->processor = $processor; | ||
} | ||
|
||
/** | ||
* Renders the pagination template. | ||
* | ||
* @param \Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination<mixed, mixed> $pagination | ||
* @param array<string, mixed> $queryParams | ||
* @param array<string, mixed> $viewParams | ||
*/ | ||
public function render( | ||
Environment $env, | ||
SlidingPaginationInterface $pagination, | ||
?string $template = null, | ||
?array $queryParams = [], | ||
?array $viewParams = [], | ||
): string { | ||
return $env->render( | ||
$template ?: $pagination->getTemplate(), | ||
$this->processor->render($pagination, $queryParams ?? [], $viewParams ?? []) | ||
); | ||
} | ||
|
||
/** | ||
* Create a sort url for the field named $title and identified by $key which consists of | ||
* alias and field. $options holds all link parameters like "alt, class" and so on. | ||
* | ||
* $key example: "article.title" | ||
* | ||
* @param \Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination<mixed, mixed> $pagination | ||
* @param string|array<string, mixed> $key | ||
* @param array<string, mixed> $options | ||
* @param array<string, mixed> $params | ||
*/ | ||
public function sortable( | ||
Environment $env, | ||
SlidingPaginationInterface $pagination, | ||
string $title, | ||
array|string $key, | ||
array $options = [], | ||
array $params = [], | ||
?string $template = null, | ||
): string { | ||
return $env->render( | ||
$template ?: $pagination->getSortableTemplate(), | ||
$this->processor->sortable($pagination, $title, $key, $options, $params) | ||
); | ||
} | ||
|
||
/** | ||
* Create a filter url for the field named $title and identified | ||
* by $key which consists of alias and field. | ||
* $options holds all link parameters like "alt, class" and so on. | ||
* | ||
* $key example: "article.title" | ||
* | ||
* @param \Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination<mixed, mixed> $pagination | ||
* @param array<string, mixed> $fields | ||
* @param array<string, mixed> $options | ||
* @param array<string, mixed>|null $params | ||
*/ | ||
public function filter( | ||
Environment $env, | ||
SlidingPaginationInterface $pagination, | ||
array $fields, ?array $options = [], | ||
?array $params = [], | ||
?string $template = null, | ||
): string { | ||
return $env->render( | ||
$template ?: $pagination->getFiltrationTemplate(), | ||
$this->processor->filter($pagination, $fields, $options ?? [], $params ?? []) | ||
); | ||
} | ||
} |