mirror of
https://github.com/flextype/flextype.git
synced 2025-08-16 10:04:21 +02:00
- add new twig function {{ site_url() }} - add new shortcode [site_url]
This commit is contained in:
@@ -11,6 +11,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Slim\Http\Environment;
|
||||
use Slim\Http\Uri;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use function ltrim;
|
||||
@@ -96,6 +98,22 @@ class SiteController extends Controller
|
||||
return $this->view->render($response, $path, ['entry' => $this->entry, 'query' => $query, 'uri' => $uri]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Site URL
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
public function getSiteUrl()
|
||||
{
|
||||
if ($this->registry->has('plugins.site.site_url') && $this->registry->get('plugins.site.site_url') != '') {
|
||||
return $this->registry->get('plugins.site.site_url');
|
||||
} else {
|
||||
return Uri::createFromEnvironment(new Environment($_SERVER))->getBaseUrl();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Error404 page
|
||||
*
|
||||
|
@@ -34,6 +34,11 @@ $site_loader = require_once $site_autoload;
|
||||
*/
|
||||
include_once 'routes/web.php';
|
||||
|
||||
/**
|
||||
* Include shortcodes
|
||||
*/
|
||||
include_once 'shortcodes/SiteUrlShortcodeExtension.php';
|
||||
|
||||
/**
|
||||
* Include dependencies
|
||||
*/
|
||||
|
@@ -29,7 +29,8 @@
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"app"
|
||||
"app",
|
||||
"twig"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@@ -17,3 +17,6 @@ namespace Flextype;
|
||||
$flextype['SiteController'] = static function ($container) {
|
||||
return new SiteController($container);
|
||||
};
|
||||
|
||||
// Add Site Url Twig Extension
|
||||
$flextype->view->addExtension(new SiteUrlTwigExtension($flextype));
|
||||
|
@@ -20,3 +20,6 @@ robots: index, follow
|
||||
author:
|
||||
email: ''
|
||||
name: ''
|
||||
|
||||
# Custom site site url
|
||||
site_url: ''
|
||||
|
15
site/plugins/site/shortcodes/SiteUrlShortcodeExtension.php
Normal file
15
site/plugins/site/shortcodes/SiteUrlShortcodeExtension.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (http://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
// Shortcode: [site_url]
|
||||
$flextype['shortcodes']->addHandler('site_url', static function () use ($flextype) {
|
||||
return $flextype->SiteController->getSiteUrl();
|
||||
});
|
49
site/plugins/site/twig/SiteUrlTwigExtension.php
Normal file
49
site/plugins/site/twig/SiteUrlTwigExtension.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype (http://flextype.org)
|
||||
* Founded by Sergey Romanenko and maintained by Flextype Community.
|
||||
*/
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Twig_Extension;
|
||||
use Twig_SimpleFunction;
|
||||
|
||||
class SiteUrlTwigExtension extends Twig_Extension
|
||||
{
|
||||
/**
|
||||
* Flextype Dependency Container
|
||||
*/
|
||||
private $flextype;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct($flextype)
|
||||
{
|
||||
$this->flextype = $flextype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for twig.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFunctions() : array
|
||||
{
|
||||
return [
|
||||
new Twig_SimpleFunction('site_url', [$this, 'site_url'], ['is_safe' => ['html']])
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Site URL
|
||||
*/
|
||||
public function site_url() : string
|
||||
{
|
||||
return $this->flextype->SiteController->getSiteUrl();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user