-
-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Smarty and Twig template engines (#3452)
* Add Smarty and Twig template engines * Update composer.json * Convert Widgets class to template engine and PHPStan fixes * Fix widget initialisation * Fix panel widget initialisation * chore: remove debug return * feat: convert 403/404 pages to new template system and general tidy * chore: style fixes * feat: update latest pages to new template engine * feat: template base should not be nullable in onPageLoad * feat: set template base directory upon initialisation and allow custom smarty security directories * chore: fix style
- Loading branch information
Showing
136 changed files
with
3,579 additions
and
2,351 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
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,30 @@ | ||
<?php | ||
/** | ||
* Fake Smarty class to help with migration to 2.2.0 template system | ||
* It aims to wrap around TemplateBase to ensure $smarty->assign still works until 2.3.0, when this will be removed. | ||
* | ||
* @author Samerton | ||
* @license MIT | ||
* @version 2.2.0 | ||
* @deprecated | ||
*/ | ||
class FakeSmarty | ||
{ | ||
private TemplateEngine $_engine; | ||
|
||
public function __construct(TemplateEngine $engine) | ||
{ | ||
$this->_engine = $engine; | ||
} | ||
|
||
public function assign($key, $value = null) | ||
{ | ||
if (is_string($key)) { | ||
$this->_engine->addVariable($key, $value); | ||
} | ||
|
||
if (is_array($key)) { | ||
$this->_engine->addVariables($key); | ||
} | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
/** | ||
* Base class which templates should extend to add functionality. | ||
* Uses Smarty template engine. | ||
* | ||
* @package NamelessMC\Templates | ||
* @author Samerton | ||
* @version 2.2.0 | ||
* @license MIT | ||
*/ | ||
abstract class SmartyTemplateBase extends TemplateBase | ||
{ | ||
/** | ||
* @param string $name | ||
* @param string $version | ||
* @param string $nameless_version | ||
* @param string $author | ||
* @param string $dir | ||
*/ | ||
public function __construct(string $name, string $version, string $nameless_version, string $author, string $dir) | ||
{ | ||
$this->_engine = new SmartyTemplateEngine($dir); | ||
|
||
parent::__construct($name, $version, $nameless_version, $author); | ||
} | ||
} |
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,95 @@ | ||
<?php | ||
/** | ||
* Smarty template engine. | ||
* | ||
* @author Samerton | ||
* @license MIT | ||
* @version 2.2.0 | ||
*/ | ||
class SmartyTemplateEngine extends TemplateEngine | ||
{ | ||
protected Smarty_Security $_securityPolicy; | ||
private Smarty $_smarty; | ||
|
||
/** | ||
* @param string $dir Path to template directory | ||
* @throws SmartyException | ||
*/ | ||
public function __construct(string $dir) | ||
{ | ||
$smarty = new Smarty(); | ||
|
||
$securityPolicy = new Smarty_Security($smarty); | ||
$securityPolicy->php_modifiers = [ | ||
'escape', | ||
'count', | ||
'key', | ||
'round', | ||
'ucfirst', | ||
'defined', | ||
'date', | ||
'explode', | ||
'implode', | ||
'strtolower', | ||
'strtoupper', | ||
]; | ||
$securityPolicy->php_functions = [ | ||
'isset', | ||
'empty', | ||
'count', | ||
'sizeof', | ||
'in_array', | ||
'is_array', | ||
'time', | ||
'nl2br', | ||
'is_numeric', | ||
'file_exists', | ||
'array_key_exists', | ||
]; | ||
$securityPolicy->secure_dir = [ROOT_PATH . '/custom/templates', ROOT_PATH . '/custom/panel_templates']; | ||
$smarty->enableSecurity($securityPolicy); | ||
|
||
$smarty->setCompileDir(ROOT_PATH . '/cache/templates_c'); | ||
$smarty->setTemplateDir($dir); | ||
|
||
if (defined('PHPDEBUGBAR')) { | ||
DebugBarHelper::getInstance()->addSmartyCollector($smarty); | ||
} | ||
|
||
$this->_securityPolicy = $securityPolicy; | ||
$this->_smarty = $smarty; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
public function render(string $templateFile): void | ||
{ | ||
echo $this->fetch($templateFile); | ||
} | ||
|
||
public function fetch(string $templateFile): string | ||
{ | ||
$templateFile = str_replace('.tpl', '', $templateFile); | ||
|
||
$this->_smarty->assign($this->getVariables()); | ||
|
||
return $this->_smarty->fetch("$templateFile.tpl"); | ||
} | ||
|
||
public function clearCache(): void | ||
{ | ||
$this->_smarty->clearAllCache(); | ||
} | ||
|
||
/** | ||
* Add an extra directory to the Smarty security policy. | ||
* | ||
* @param string $dir Directory to add to policy | ||
* @return void | ||
*/ | ||
public function addSecurityPolicyDirectory(string $dir): void | ||
{ | ||
$this->_securityPolicy->secure_dir = [...$this->_securityPolicy->secure_dir, $dir]; | ||
$this->_smarty->enableSecurity($this->_securityPolicy); | ||
} | ||
} |
Oops, something went wrong.