-
-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start implementing Twig for the front-end (#2963)
* Starting work on moving front end to Twig This marks the start of integrating Twig into the front end. There are two global variables shared with all views - mybb and lang. Signed-off-by: Euan Torano <euantorano@gmail.com> * Starting the long process of template translation Not much to show yet, but this is a first step towards seeing that key variables such as $mybb, $lang and $theme can be accessed. Need to sort out loading of stylesheets still to make things look decent. Signed-off-by: Euan Torano <euantorano@gmail.com> * Changing how language is registered There is a new LangExtension that provides a `trans` function along with the `lang` global. Signed-off-by: Euan Torano <euantorano@gmail.com> * Showing drafts by adding drafts to an array Moved setting of theme global to theme extension. Need to fix the use of |raw in some places - we should generate the HTML inside the views themselves instead… Signed-off-by: Euan Torano <euantorano@gmail.com> * Finished the majority of work on the drafts page Signed-off-by: Euan Torano <euantorano@gmail.com> * Add return type hints Signed-off-by: Euan Torano <euantorano@gmail.com> * Fixing trow usage currently following 1.8 standard Signed-off-by: Euan Torano <euantorano@gmail.com>
- Loading branch information
1 parent
37fb723
commit 97b3aed
Showing
16 changed files
with
513 additions
and
82 deletions.
There are no files selected for viewing
Empty file.
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,56 @@ | ||
<?php | ||
|
||
namespace MyBB\Twig\Extensions; | ||
|
||
/** | ||
* A Twig extension class to provide functionality related to translations. | ||
*/ | ||
class LangExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface | ||
{ | ||
/** | ||
* @var \MyLanguage $lang | ||
*/ | ||
private $lang; | ||
|
||
/** | ||
* Create a new instance of the LangExtension. | ||
* | ||
* @param \MyLanguage $lang | ||
*/ | ||
public function __construct(\MyLanguage $lang) | ||
{ | ||
$this->lang = $lang; | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return [ | ||
new \Twig_SimpleFunction('trans', [$this, 'trans']), | ||
]; | ||
} | ||
|
||
/** | ||
* Returns a list of global variables to add to the existing list. | ||
* | ||
* @return array An array of global variables | ||
*/ | ||
public function getGlobals() | ||
{ | ||
return [ | ||
'lang' => $this->lang, | ||
]; | ||
} | ||
|
||
/** | ||
* Get a translation that takes a number of parameters. | ||
* | ||
* @param string $languageVariable The name of the language variable to translate. | ||
* @param array ...$params A list of parameters to inject into the translation. | ||
* | ||
* @return string The resolved translation string. | ||
*/ | ||
public function trans(string $languageVariable, ...$params) : string | ||
{ | ||
return $this->lang->sprintf($this->lang->$languageVariable, ...$params); | ||
} | ||
} |
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,167 @@ | ||
<?php | ||
|
||
namespace MyBB\Twig\Extensions; | ||
|
||
/** | ||
* A Twig extension class to provide functionality related to themes and assets. | ||
*/ | ||
class ThemeExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface | ||
{ | ||
/** | ||
* @var \MyBB $mybb | ||
*/ | ||
private $mybb; | ||
|
||
/** | ||
* @var \DB_Base $db | ||
*/ | ||
private $db; | ||
|
||
/** | ||
* @var string $altRowState | ||
*/ | ||
private $altRowState; | ||
|
||
/** | ||
* Create a new instance of the ThemeExtension. | ||
* | ||
* @param \MyBB $mybb | ||
* @param \DB_Base $db | ||
*/ | ||
public function __construct(\MyBB $mybb, \DB_Base $db) | ||
{ | ||
$this->mybb = $mybb; | ||
$this->db = $db; | ||
|
||
$this->altRowState = null; | ||
} | ||
|
||
public function getFunctions() | ||
{ | ||
return [ | ||
new \Twig_SimpleFunction('asset_url', [$this, 'getAssetUrl']), | ||
new \Twig_SimpleFunction('alt_trow', [$this, 'altTrow']), | ||
new \Twig_SimpleFunction('get_stylesheets', [$this, 'getStylesheets']), | ||
]; | ||
} | ||
|
||
/** | ||
* Returns a list of global variables to add to the existing list. | ||
* | ||
* @return array An array of global variables | ||
*/ | ||
public function getGlobals() | ||
{ | ||
return [ | ||
'theme' => $GLOBALS['theme'], | ||
]; | ||
} | ||
|
||
/** | ||
* Get the path to an asset using the CDN URL if configured. | ||
* | ||
* @param string $path The path to the file. | ||
* @param bool $useCdn Whether to use the configured CDN options. | ||
* | ||
* @return string The complete URL to the asset. | ||
*/ | ||
public function getAssetUrl(string $path, bool $useCdn = true): string | ||
{ | ||
// TODO: This could be smart and add cache busting query parameters to the path automatically... | ||
return $this->mybb->get_asset_url($path, $useCdn); | ||
} | ||
|
||
/** | ||
* Select an alternating row colour based on the previous call to this function. | ||
* | ||
* @param bool $reset Whether to reset the row state to `trow1`. | ||
* | ||
* @return string `trow1` or `trow2` depending on the previous call. | ||
*/ | ||
public function altTrow(bool $reset = false) : string | ||
{ | ||
if (is_null($this->altRowState) || $this->altRowState === 'trow2' || $reset) { | ||
$this->altRowState = 'trow1'; | ||
} else { | ||
$this->altRowState = 'trow2'; | ||
} | ||
|
||
return $this->altRowState; | ||
} | ||
|
||
/** | ||
* Get a list of all the stylesheets applicable for the current page. | ||
* | ||
* @return \Generator A generator object that yields each stylesheet, as a full URL. | ||
*/ | ||
public function getStylesheets() : \Generator | ||
{ | ||
// TODO: Optimise this function - it looks like it can be improved at a glance | ||
$theme = $GLOBALS['theme']; | ||
|
||
$alreadyLoaded = []; | ||
|
||
if (!is_array($theme['stylesheets'])) { | ||
$theme['stylesheets'] = my_unserialize($theme['stylesheets']); | ||
} | ||
|
||
$stylesheetScripts = array("global", basename($_SERVER['PHP_SELF'])); | ||
if (!empty($theme['color'])) { | ||
$stylesheetScripts[] = $theme['color']; | ||
} | ||
|
||
$stylesheetActions = array("global"); | ||
if (!empty($this->mybb->input['action'])) { | ||
$stylesheetActions[] = $this->mybb->get_input('action'); | ||
} | ||
foreach ($stylesheetScripts as $stylesheetScript) { | ||
// Load stylesheets for global actions and the current action | ||
foreach ($stylesheetActions as $stylesheet_action) { | ||
if (!$stylesheet_action) { | ||
continue; | ||
} | ||
|
||
if (!empty($theme['stylesheets'][$stylesheetScript][$stylesheet_action])) { | ||
// Actually add the stylesheets to the list | ||
foreach ($theme['stylesheets'][$stylesheetScript][$stylesheet_action] as $pageStylesheet) { | ||
if (!empty($alreadyLoaded[$pageStylesheet])) { | ||
continue; | ||
} | ||
|
||
if (strpos($pageStylesheet, 'css.php') !== false) { | ||
$stylesheetUrl = $this->mybb->settings['bburl'] . '/' . $pageStylesheet; | ||
} else { | ||
$stylesheetUrl = $this->mybb->get_asset_url($pageStylesheet); | ||
} | ||
|
||
if ($this->mybb->settings['minifycss']) { | ||
$stylesheetUrl = str_replace('.css', '.min.css', $stylesheetUrl); | ||
} | ||
|
||
if (strpos($pageStylesheet, 'css.php') !== false) { | ||
// We need some modification to get it working with the displayorder | ||
$queryString = parse_url($stylesheetUrl, PHP_URL_QUERY); | ||
$id = (int)my_substr($queryString, 11); | ||
$query = $this->db->simple_select("themestylesheets", "name", "sid={$id}"); | ||
$realName = $this->db->fetch_field($query, "name"); | ||
$themeStylesheets[$realName] = $stylesheetUrl; | ||
} else { | ||
$themeStylesheets[basename($pageStylesheet)] = $stylesheetUrl; | ||
} | ||
|
||
$alreadyLoaded[$pageStylesheet] = 1; | ||
} | ||
} | ||
} | ||
} | ||
unset($actions); | ||
|
||
if (!empty($themeStylesheets) && is_array($theme['disporder'])) { | ||
foreach ($theme['disporder'] as $style_name => $order) { | ||
if (!empty($themeStylesheets[$style_name])) { | ||
yield $themeStylesheets[$style_name]; | ||
} | ||
} | ||
} | ||
} | ||
} |
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
File renamed without changes.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{% extends "layouts/admin.twig" %} | ||
{% extends "layouts/master.twig" %} | ||
|
||
{% block head %} | ||
<title>PHP Info</title> | ||
|
Oops, something went wrong.