From 56c34d71ef270d5ba4da94ba02578f769526a9db Mon Sep 17 00:00:00 2001 From: abgreeve Date: Fri, 6 Aug 2021 11:15:06 +0800 Subject: [PATCH] MDL-69588 theme_boost: Use partial templates in boost. This moves away from using escaped html injected into the template and uses partial templates instead. --- admin/search.php | 3 +- lib/classes/navigation/output/more_menu.php | 74 ++++++++ lib/classes/navigation/output/primary.php | 6 +- lib/outputrenderers.php | 28 --- lib/templates/moremenu.mustache | 5 +- lib/templates/settings_link_page.mustache | 2 +- lib/tests/navigation/output/primary_test.php | 18 +- theme/boost/classes/output/core_renderer.php | 186 ++++++++++++++++++- theme/boost/layout/columns2.php | 12 +- theme/boost/layout/drawers.php | 12 +- theme/boost/templates/columns2.mustache | 4 +- theme/boost/templates/drawers.mustache | 4 +- theme/boost/templates/navbar.mustache | 5 +- 13 files changed, 297 insertions(+), 62 deletions(-) create mode 100644 lib/classes/navigation/output/more_menu.php diff --git a/admin/search.php b/admin/search.php index 319aa8f7fa5..1862f3928db 100644 --- a/admin/search.php +++ b/admin/search.php @@ -91,7 +91,8 @@ if ($hassiteconfig) { if ($showsettingslinks) { $node = $PAGE->settingsnav->find('root', navigation_node::TYPE_SITE_ADMIN); if ($node) { - $secondarynavigation = $OUTPUT->more_menu($PAGE->secondarynav, 'nav-tabs', true); + $moremenu = new \core\navigation\output\more_menu($PAGE->secondarynav, 'nav-tabs', true); + $secondarynavigation = $moremenu->export_for_template($OUTPUT); echo $OUTPUT->render_from_template('core/settings_link_page', ['node' => $node, 'secondarynavigation' => $secondarynavigation]); } diff --git a/lib/classes/navigation/output/more_menu.php b/lib/classes/navigation/output/more_menu.php new file mode 100644 index 00000000000..6d6d356baaa --- /dev/null +++ b/lib/classes/navigation/output/more_menu.php @@ -0,0 +1,74 @@ +. + +namespace core\navigation\output; + +use renderable; +use renderer_base; +use templatable; +use custom_menu; + +/** + * more menu navigation renderable + * + * @package core + * @category navigation + * @copyright 2021 onwards Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class more_menu implements renderable, templatable { + + protected $content; + protected $navbarstyle; + protected $hastabs; + protected $haschildren; + + /** + * Constructor for this class. + * + * @param object $content Navigation objects. + * @param string $navbarstyle class name. + * @param bool $hastabs If tabs are being used. + * @param bool $haschildren The content has children. + */ + public function __construct(object $content, string $navbarstyle, bool $hastabs = false, bool $haschildren = true) { + $this->content = $content; + $this->navbarstyle = $navbarstyle; + $this->hastabs = $hastabs; + $this->haschildren = $haschildren; + } + + /** + * Return data for rendering a template. + * + * @param renderer_base $output The output + * @return array Data for rendering a template + */ + public function export_for_template(renderer_base $output): array { + $data = ['navbarstyle' => $this->navbarstyle, 'tabs' => $this->hastabs]; + if ($this->haschildren) { + if (!isset($this->content->children) || count($this->content->children) == 0) { + $data = []; + } + $data['nodecollection'] = $this->content; + } else { + $data['nodearray'] = (array) $this->content; + } + + return $data; + } + +} diff --git a/lib/classes/navigation/output/primary.php b/lib/classes/navigation/output/primary.php index b5ef3245a97..2bcc751b193 100644 --- a/lib/classes/navigation/output/primary.php +++ b/lib/classes/navigation/output/primary.php @@ -55,9 +55,11 @@ class primary implements renderable, templatable { $output = $this->page->get_renderer('core'); } + $menudata = (object) array_merge($this->get_primary_nav(), $this->get_custom_menu($output)); + $moremenu = new \core\navigation\output\more_menu($menudata, 'navbar-nav', false, false); + return [ - 'primary' => $this->get_primary_nav(), - 'custom' => $this->get_custom_menu($output), + 'moremenu' => $moremenu->export_for_template($output), 'lang' => !isloggedin() || isguestuser() ? $this->get_lang_menu($output) : [], 'user' => $this->get_user_menu($output), ]; diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 3f1e959c06a..8deb104e90c 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -3808,34 +3808,6 @@ EOD; return $content; } - /** - * Renders a navigation bar into a "more menu" navigation bar - * - * @param array $content - * @param string $navbarstyle navbar-nav or nav-tabs - * @param boolean $hastabs - * @return string - */ - public function more_menu($content, $navbarstyle, $hastabs = false) { - $tabs = ($navbarstyle == 'nav-tabs'); - if (is_object($content)) { - if (!isset($content->children) || count($content->children) == 0) { - return false; - } - return $this->render_from_template('core/moremenu', (object) [ - 'nodecollection' => $content, - 'navbarstyle' => $navbarstyle, - 'tabs' => $hastabs - ]); - } else { - return $this->render_from_template('core/moremenu', (object) [ - 'nodearray' => $content, - 'navbarstyle' => $navbarstyle, - 'tabs' => $hastabs - ]); - } - } - /** * Renders theme links for switching between default and other themes. * diff --git a/lib/templates/moremenu.mustache b/lib/templates/moremenu.mustache index f9fd1d04f99..52662983ba9 100644 --- a/lib/templates/moremenu.mustache +++ b/lib/templates/moremenu.mustache @@ -45,7 +45,7 @@ } }} {{#js}} require(['core/moremenu'], function(moremenu) { - var moreMenu = document.querySelector('#moremenu-{{ uniqid }}'); - moremenu(moreMenu); + moremenu(document.querySelector('#moremenu-{{ uniqid }}-{{navbarstyle}}')); }); {{/js}} diff --git a/lib/templates/settings_link_page.mustache b/lib/templates/settings_link_page.mustache index ccce9c27772..606530b228e 100644 --- a/lib/templates/settings_link_page.mustache +++ b/lib/templates/settings_link_page.mustache @@ -37,7 +37,7 @@ } }} {{#secondarynavigation}} - {{{secondarynavigation}}} + {{> core/moremenu}} {{/secondarynavigation}} {{^secondarynavigation}}