- Addeed a slug method
This commit is contained in:
parent
b836fc5bd8
commit
2b81e9cef8
|
@ -14,7 +14,7 @@ class UrlExtension implements Extension {
|
|||
|
||||
protected array $routesTarget;
|
||||
|
||||
public array $tokens = [ "url" , "route", "asset", "url.current", "url.parameters" ];
|
||||
public array $tokens = [ "url" , "route", "asset", "url.current", "url.parameters", "slug" ];
|
||||
|
||||
public function __construct(Context $context, string $urlBase = "", string $assetToken = "") {
|
||||
$this->urlBase = trim($urlBase, "/");
|
||||
|
@ -39,6 +39,9 @@ class UrlExtension implements Extension {
|
|||
|
||||
case "url.parameters":
|
||||
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>";
|
||||
|
||||
case "slug":
|
||||
return \Transliterator::createFromRules(':: Any-Latin;:: NFD;:: [:Nonspacing Mark:] Remove;:: NFC;:: [:Punctuation:] Remove;:: Lower();[:Separator:] > \'-\'')->transliterate( $arguments );
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -50,6 +53,7 @@ class UrlExtension implements Extension {
|
|||
$context->pushFunction("current_url", [ $this, 'currentUrl' ]);
|
||||
$context->pushFunction("asset", [ $this, 'buildAssetUrl' ]);
|
||||
$context->pushFunction("route", [ $this, 'buildRouteUrl' ]);
|
||||
$context->pushFunction("slug", [ $this, 'slug' ]);
|
||||
}
|
||||
|
||||
public function getRouteList(bool $full = false) : array
|
||||
|
@ -96,6 +100,11 @@ class UrlExtension implements Extension {
|
|||
return $this->scheme() . $this->domain() . $this->base();
|
||||
}
|
||||
|
||||
public static function slug(string $text, string $separator = '-') : string
|
||||
{
|
||||
return str_replace('-', $separator, \Transliterator::createFromRules(':: Any-Latin;:: NFD;:: [:Nonspacing Mark:] Remove;:: NFC;:: [:Punctuation:] Remove;:: Lower();[:Separator:] > \'-\'')->transliterate(str_replace('-', ' ', $text)));
|
||||
}
|
||||
|
||||
public function registerRoute(string $name, string $route, string $class, string $method, array $routeMethods) : void
|
||||
{
|
||||
$this->routes[$name] = [
|
||||
|
|
Loading…
Reference in New Issue