-
-
Notifications
You must be signed in to change notification settings - Fork 313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Smarty and Twig template engines #3452
Draft
samerton
wants to merge
5
commits into
develop
Choose a base branch
from
feature/template-engine
base: develop
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9201608
Add Smarty and Twig template engines
samerton 9b4a38f
Update composer.json
samerton 2f5c34a
Convert Widgets class to template engine and PHPStan fixes
samerton de13af2
Fix widget initialisation
samerton 16e5da2
Fix panel widget initialisation
samerton File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
/** | ||
* Fake Smarty class to help with migration to 2.2.0 template system | ||
* It aims to be wrapper 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,25 @@ | ||
<?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 bool $panelTemplate | ||
*/ | ||
public function __construct(string $name, string $version, string $nameless_version, string $author, bool $panelTemplate = false) { | ||
parent::__construct($name, $version, $nameless_version, $author); | ||
|
||
$this->_engine = new SmartyTemplateEngine($name, $panelTemplate); | ||
} | ||
} |
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,81 @@ | ||
<?php | ||
/** | ||
* Smarty template engine | ||
* | ||
* @author Samerton | ||
* @license MIT | ||
* @version 2.2.0 | ||
*/ | ||
|
||
class SmartyTemplateEngine extends TemplateEngine { | ||
private Smarty $_smarty; | ||
|
||
/** | ||
* @param string $template Template name to load | ||
* @param bool $panelTemplate Whether this is a panel template or not | ||
*/ | ||
public function __construct(string $template, bool $panelTemplate = false) { | ||
$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'); | ||
|
||
if ($panelTemplate) { | ||
$smarty->setTemplateDir(ROOT_PATH . '/custom/panel_templates/' . $template); | ||
} else { | ||
$smarty->setTemplateDir(ROOT_PATH . '/custom/templates/' . $template); | ||
} | ||
|
||
if (defined('PHPDEBUGBAR')) { | ||
DebugBarHelper::getInstance()->addSmartyCollector($smarty); | ||
} | ||
|
||
$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(); | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Temporary as I use the same database as another WIP branch