1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-16 10:04:21 +02:00

feat(site-plugin): update site plugin for this ticeks #373 #357

- add new twig function {{ site_url() }}
- add new shortcode [site_url]
This commit is contained in:
Awilum
2020-02-21 14:46:19 +03:00
parent 4bcd0560bd
commit 7740bcc352
7 changed files with 95 additions and 1 deletions

View File

@@ -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
*

View File

@@ -34,6 +34,11 @@ $site_loader = require_once $site_autoload;
*/
include_once 'routes/web.php';
/**
* Include shortcodes
*/
include_once 'shortcodes/SiteUrlShortcodeExtension.php';
/**
* Include dependencies
*/

View File

@@ -29,7 +29,8 @@
},
"autoload": {
"classmap": [
"app"
"app",
"twig"
]
}
}

View File

@@ -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));

View File

@@ -20,3 +20,6 @@ robots: index, follow
author:
email: ''
name: ''
# Custom site site url
site_url: ''

View 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();
});

View 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();
}
}