Merge pull request #8 from AntCMS-org/twig

Started to migrate to twig
This commit is contained in:
Belle Aerni 2023-01-09 13:16:58 -08:00 committed by GitHub
commit 89b182b3be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 326 additions and 29 deletions

View file

@ -8,7 +8,9 @@
"symfony/yaml": "^6.0",
"php": ">=7.4",
"league/commonmark": "^2.3",
"elgigi/commonmark-emoji": "^2.0"
"elgigi/commonmark-emoji": "^2.0",
"twig/twig": "^3.5",
"shapecode/twig-string-loader": "^1.1"
},
"authors": [
{
@ -23,4 +25,4 @@
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9.5"
}
}
}

243
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "68041be300c807a0d95b6b8bc8c25eac",
"content-hash": "7f7fedbff2c529f6168337f46cef5ed3",
"packages": [
{
"name": "dflydev/dot-access-data",
@ -504,6 +504,88 @@
},
"time": "2019-01-08T18:20:26+00:00"
},
{
"name": "shapecode/twig-string-loader",
"version": "1.1.0",
"source": {
"type": "git",
"url": "https://github.com/shapecode/twig-string-loader.git",
"reference": "c40d3a9f123e22f2e81f1c79d4d3a96455e21b2d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/shapecode/twig-string-loader/zipball/c40d3a9f123e22f2e81f1c79d4d3a96455e21b2d",
"reference": "c40d3a9f123e22f2e81f1c79d4d3a96455e21b2d",
"shasum": ""
},
"require": {
"php": "^7.4|^8.0",
"twig/twig": "^2.11|^3.0"
},
"require-dev": {
"doctrine/coding-standard": "^9.0",
"maglnet/composer-require-checker": "^2.0|^3.8|^4.0",
"phpstan/phpstan": "^1.4",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-strict-rules": "^1.1",
"phpunit/phpunit": "^9.5",
"roave/security-advisories": "dev-master",
"squizlabs/php_codesniffer": "^3.4"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
}
},
"autoload": {
"psr-4": {
"Shapecode\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nikita Loges",
"email": "dev@loges.one",
"homepage": "https://loges.one"
}
],
"description": "A string loader for twig",
"homepage": "https://github.com/shapecode/twig-string-loader",
"keywords": [
"loader",
"shapecode",
"string",
"template",
"twig"
],
"support": {
"email": "support@shapecode.de",
"issues": "https://github.com/shapecode/twig-string-loader/issues",
"source": "https://github.com/shapecode/twig-string-loader/releases",
"wiki": "https://github.com/shapecode/twig-string-loader/wiki"
},
"funding": [
{
"url": "http://paypal.me/nloges",
"type": "custom"
},
{
"url": "https://github.com/nicklog",
"type": "github"
},
{
"url": "https://liberapay.com/nicklog",
"type": "liberapay"
}
],
"time": "2022-01-15T12:43:29+00:00"
},
{
"name": "symfony/deprecation-contracts",
"version": "v3.0.2",
@ -653,6 +735,89 @@
],
"time": "2022-11-03T14:55:06+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.27.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/8ad114f6b39e2c98a8b0e3bd907732c207c2b534",
"reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534",
"shasum": ""
},
"require": {
"php": ">=7.1"
},
"provide": {
"ext-mbstring": "*"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.27-dev"
},
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
},
"autoload": {
"files": [
"bootstrap.php"
],
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.27.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2022-11-03T14:55:06+00:00"
},
{
"name": "symfony/polyfill-php80",
"version": "v1.27.0",
@ -809,6 +974,82 @@
}
],
"time": "2022-12-14T15:52:41+00:00"
},
{
"name": "twig/twig",
"version": "v3.5.0",
"source": {
"type": "git",
"url": "https://github.com/twigphp/Twig.git",
"reference": "3ffcf4b7d890770466da3b2666f82ac054e7ec72"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twigphp/Twig/zipball/3ffcf4b7d890770466da3b2666f82ac054e7ec72",
"reference": "3ffcf4b7d890770466da3b2666f82ac054e7ec72",
"shasum": ""
},
"require": {
"php": ">=7.2.5",
"symfony/polyfill-ctype": "^1.8",
"symfony/polyfill-mbstring": "^1.3"
},
"require-dev": {
"psr/container": "^1.0",
"symfony/phpunit-bridge": "^4.4.9|^5.0.9|^6.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.5-dev"
}
},
"autoload": {
"psr-4": {
"Twig\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com",
"homepage": "http://fabien.potencier.org",
"role": "Lead Developer"
},
{
"name": "Twig Team",
"role": "Contributors"
},
{
"name": "Armin Ronacher",
"email": "armin.ronacher@active-4.com",
"role": "Project Founder"
}
],
"description": "Twig, the flexible, fast, and secure template language for PHP",
"homepage": "https://twig.symfony.com",
"keywords": [
"templating"
],
"support": {
"issues": "https://github.com/twigphp/Twig/issues",
"source": "https://github.com/twigphp/Twig/tree/v3.5.0"
},
"funding": [
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/twig/twig",
"type": "tidelift"
}
],
"time": "2022-12-27T12:28:18+00:00"
}
],
"packages-dev": [

View file

@ -14,6 +14,7 @@ class AntCMS
$start_time = microtime(true);
$content = $this->getPage($page);
$currentConfig = AntConfig::currentConfig();
$antTwig = new AntTwig;
if (!$content || !is_array($content)) {
$this->renderException("404");
@ -23,12 +24,14 @@ class AntCMS
$pageTemplate = $this->getPageLayout();
$pageTemplate = str_replace('<!--AntCMS-Description-->', $content['description'], $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-Author-->', $content['author'], $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-Keywords-->', $content['keywords'], $pageTemplate);
$pageTemplate = str_replace('<!--AntCMS-Title-->', $content['title'], $pageTemplate);
$params = array(
'AntCMSTitle' => $content['title'],
'AntCMSDescription' => $content['description'],
'AntCMSAuthor' => $content['author'],
'AntCMSKeywords' => $content['keywords'],
);
$pageTemplate = str_replace('<!--AntCMS-Body-->', $markdown, $pageTemplate);
$pageTemplate = $antTwig->renderWithTiwg($pageTemplate, $params);
$end_time = microtime(true);
$elapsed_time = round($end_time - $start_time, 4);

View file

@ -7,6 +7,7 @@ use AntCMS\AntYaml;
use AntCMS\AntConfig;
use AntCMS\AntCache;
use AntCMS\AntTools;
use AntCMS\AntTwig;
class AntPages
{
@ -53,17 +54,15 @@ class AntPages
}
}
$navHTML = '';
$baseURL = $currentConfig['baseURL'];
foreach ($pages as $page) {
if (!$page['showInNav']) {
continue;
}
$url = "//" . str_replace('//', '/', $baseURL . $page['functionalPagePath']);
$navEntry = str_replace('<!--AntCMS-PageLink-->', $url, $navTemplate);
$navEntry = str_replace('<!--AntCMS-PageTitle-->', $page['pageTitle'], $navEntry);
$navHTML .= $navEntry;
foreach ($pages as $key => $page) {
$url = "//" . AntTools::repairURL($baseURL . $page['functionalPagePath']);
$pages[$key]['url'] = $url;
}
$antTwig = new AntTwig;
$navHTML = $antTwig->renderWithTiwg($navTemplate, array('pages' =>$pages));
$cache->setCache($cacheKey, $navHTML);
return $navHTML;
}

View file

@ -29,4 +29,21 @@ class AntTools
return $newPath;
}
/**
* Repairs a URL by replacing backslashes with forward slashes and removing duplicate slashes.
*
* @param string $url The URL to repair. Note: this function will not work correctly if the URL provided has its own protocol (like HTTS://).
* @return string The repaired URL
*/
public static function repairURL($url)
{
$newURL = str_replace('\\\\', '/', $url);
$newURL = str_replace('\\', '/', $newURL);
$newURL = str_replace('//', '/', $newURL);
return $newURL;
}
}

31
src/AntCMS/AntTwig.php Normal file
View file

@ -0,0 +1,31 @@
<?php
namespace AntCMS;
use AntCMS\AntConfig;
class AntTwig
{
public function renderWithTiwg(string $content = '', array $params = array(), string $theme = null)
{
$currentConfig = AntConfig::currentConfig();
$twigCache = $currentConfig['enableCache'] ? AntCachePath : false;
$theme = $theme ?? $currentConfig['activeTheme'];
if (!is_dir(antThemePath . '/' . $theme)) {
$theme = 'Default';
}
$templatePath = AntTools::repairFilePath(antThemePath . '/' . $theme . '/' . 'Templates');
$loaderFilesystem = new \Twig\Loader\FilesystemLoader($templatePath);
$loaderString = new \Shapecode\Twig\Loader\StringLoader();
$loader = new \Twig\Loader\ChainLoader([$loaderString, $loaderFilesystem]);
$twig = new \Twig\Environment($loader, [
'cache' => $twigCache,
'debug' => $currentConfig['debug'],
]);
return $twig->render($content, $params);
}
}

View file

@ -4,9 +4,9 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="<!--AntCMS-Description-->">
<meta name="author" content="<!--AntCMS-Author-->">
<meta name="keywords" content="<!--AntCMS-Keywords-->">
<meta name="description" content="{{ AntCMSDescription }}">
<meta name="author" content="{{ AntCMSAuthor }}">
<meta name="keywords" content="{{ AntCMSKeywords }}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
@ -14,7 +14,7 @@
integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN"
crossorigin="anonymous"></script>
<title><!--AntCMS-Title--></title>
<title>{{ AntCMSTitle }}</title>
</head>
<body>

View file

@ -1,3 +1,5 @@
{% for page in pages %}
<li class="nav-item active">
<a class="nav-link" href="<!--AntCMS-PageLink-->"><!--AntCMS-PageTitle--></a>
<a class="nav-link" href="{{ page.url }}">{{ page.pageTitle }}</a>
</li>
{% endfor %}

View file

@ -4,13 +4,13 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="<!--AntCMS-Description-->">
<meta name="author" content="<!--AntCMS-Author-->">
<meta name="keywords" content="<!--AntCMS-Keywords-->">
<meta name="description" content="{{ AntCMSDescription }}">
<meta name="author" content="{{ AntCMSAuthor }}">
<meta name="keywords" content="{{ AntCMSKeywords }}">
<link href="<!--AntCMS-SiteLink-->Themes/Default/Assets/Dist/tailwind.css" rel="stylesheet">
<title><!--AntCMS-Title--></title>
<title>{{ AntCMSTitle }}</title>
</head>
<body class="bg-gray-50 dark:bg-zinc-800 text-gray-500 dark:text-gray-400">

View file

@ -1,3 +1,5 @@
<li>
<a href="<!--AntCMS-PageLink-->" class="block py-2 pl-3 pr-4 text-gray-700 rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:hover:text-blue-700 md:p-0 dark:text-gray-400 md:dark:hover:text-white dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent"><!--AntCMS-PageTitle--></a>
</li>
{% for page in pages %}
<li class="nav-item active">
<a class="block py-2 pl-3 pr-4 text-gray-700 rounded hover:bg-gray-100 md:hover:bg-transparent md:border-0 md:hover:text-blue-700 md:p-0 dark:text-gray-400 md:dark:hover:text-white dark:hover:bg-gray-700 dark:hover:text-white md:dark:hover:bg-transparent" href="{{ page.url }}">{{ page.pageTitle }}</a>
</li>
{% endfor %}