Skip to content

Commit

Permalink
Change our menu building blocks to use PHP templates so that themes
Browse files Browse the repository at this point in the history
can override them and define their own menu formats.  I worry a little
bit that this approach may be too heavy since we're now doing a lot
more template includes than we were before.  Also, I had to change the
Menu API to stop using __toString() because you can't throw exceptions
from __toString() which would make it an unhappy experience for
developers.
  • Loading branch information
bharat committed Oct 27, 2009
1 parent ab1d21e commit 76c0c7f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 53 deletions.
3 changes: 1 addition & 2 deletions modules/gallery/libraries/Admin_View.php
Expand Up @@ -47,8 +47,7 @@ public function __construct($name) {
public function admin_menu() {
$menu = Menu::factory("root");
module::event("admin_menu", $menu, $this);
$menu->compact();
return $menu;
return $menu->compact()->render();
}

/**
Expand Down
61 changes: 16 additions & 45 deletions modules/gallery/libraries/Menu.php
Expand Up @@ -80,19 +80,10 @@ public function css_class($css_class) {
* Menu element that provides a link to a new page.
*/
class Menu_Element_Link extends Menu_Element {
public function __toString() {
if (isset($this->css_id) && !empty($this->css_id)) {
$css_id = " id=\"$this->css_id\"";
} else {
$css_id = "";
}
if (isset($this->css_class) && !empty($this->css_class)) {
$css_class = " $this->css_class";
} else {
$css_class = "";
}
return "<li><a$css_id class=\"g-menu-link $css_class\" href=\"$this->url\" " .
"title=\"$this->label\">$this->label</a></li>";
public function render() {
$view = new View("menu_link.html");
$view->menu = $this;
return $view;
}
}

Expand All @@ -111,39 +102,21 @@ public function ajax_handler($ajax_handler) {
return $this;
}

public function __toString() {
if (isset($this->css_id) && !empty($this->css_id)) {
$css_id = " id=\"$this->css_id\"";
} else {
$css_id = "";
}
if (isset($this->css_class) && !empty($this->css_class)) {
$css_class = " $this->css_class";
} else {
$css_class = "";
}
return "<li><a$css_id class=\"g-ajax-link $css_class\" href=\"$this->url\" " .
"title=\"$this->label\" ajax_handler=\"$this->ajax_handler\">$this->label</a></li>";
public function render() {
$view = new View("menu_ajax_link.html");
$view->menu = $this;
return $view;
}
}

/**
* Menu element that provides a pop-up dialog
*/
class Menu_Element_Dialog extends Menu_Element {
public function __toString() {
if (isset($this->css_id) && !empty($this->css_id)) {
$css_id = " id=\"$this->css_id\"";
} else {
$css_id = "";
}
if (isset($this->css_class) && !empty($this->css_class)) {
$css_class = " $this->css_class";
} else {
$css_class = "";
}
return "<li><a$css_id class=\"g-dialog-link $css_class\" href=\"$this->url\" " .
"title=\"$this->label\">$this->label</a></li>";
public function render() {
$view = new View("menu_dialog.html");
$view->menu = $this;
return $view;
}
}

Expand Down Expand Up @@ -242,11 +215,9 @@ public function get($id) {
return null;
}

public function __toString() {
$html = $this->is_root ? "<ul class=\"$this->css_class\">" :
"<li title=\"$this->label\"><a href=\"#\">$this->label</a><ul>";
$html .= implode("\n", $this->elements);
$html .= $this->is_root ? "</ul>" : "</ul></li>";
return $html;
public function render() {
$view = new View("menu.html");
$view->menu = $this;
return $view;
}
}
12 changes: 6 additions & 6 deletions modules/gallery/libraries/Theme_View.php
Expand Up @@ -81,19 +81,19 @@ public function page_type() {
public function site_menu() {
$menu = Menu::factory("root");
module::event("site_menu", $menu, $this);
return $menu->compact();
return $menu->compact()->render();
}

public function album_menu() {
$menu = Menu::factory("root");
module::event("album_menu", $menu, $this);
return $menu->compact();
return $menu->compact()->render();
}

public function tag_menu() {
$menu = Menu::factory("root");
module::event("tag_menu", $menu, $this);
return $menu->compact();
return $menu->compact()->render();
}

public function photo_menu() {
Expand All @@ -107,13 +107,13 @@ public function photo_menu() {
}

module::event("photo_menu", $menu, $this);
return $menu->compact();
return $menu->compact()->render();
}

public function movie_menu() {
$menu = Menu::factory("root");
module::event("movie_menu", $menu, $this);
return $menu->compact();
return $menu->compact()->render();
}

public function context_menu($item, $thumbnail_css_selector) {
Expand All @@ -124,7 +124,7 @@ public function context_menu($item, $thumbnail_css_selector) {
->css_class("g-context-menu");

module::event("context_menu", $menu, $this, $item, $thumbnail_css_selector);
return $menu->compact();
return $menu->compact()->render();
}

public function pager() {
Expand Down
23 changes: 23 additions & 0 deletions modules/gallery/views/menu.html.php
@@ -0,0 +1,23 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<? if ($menu->is_root): ?>

<ul class="<?= $menu->css_class ?>">
<? foreach ($this->elements as $element): ?>
<?= $element->render() ?>
<? endforeach ?>
</ul>

<? else: ?>

<li title="<?= $menu->label->for_html_attr() ?>">
<a href="#">
<?= $menu->label->for_html() ?>
</a>
<ul>
<? foreach ($this->elements as $element): ?>
<?= $element->render() ?>
<? endforeach ?>
</ul>
</li>

<? endif ?>
10 changes: 10 additions & 0 deletions modules/gallery/views/menu_ajax_link.html.php
@@ -0,0 +1,10 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<li>
<a id="<?= $menu->css_id ?>"
class="g-ajax-link <?= $menu->css_class ?>"
href="<?= $menu->url ?>"
title="<?= $menu->label->for_html_attr() ?>"
ajax_handler="<?= $menu->ajax_handler ?>">
<?= $menu->label->for_html() ?>
</a>
</li>
9 changes: 9 additions & 0 deletions modules/gallery/views/menu_dialog.html.php
@@ -0,0 +1,9 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<li>
<a id="<?= $menu->css_id ?>"
class="g-dialog-link <?= $menu->css_class ?>"
href="<?= $menu->url ?>"
title="<?= $menu->label->for_html_attr() ?>">
<?= $menu->label->for_html() ?>
</a>
</li>
9 changes: 9 additions & 0 deletions modules/gallery/views/menu_link.html.php
@@ -0,0 +1,9 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<li>
<a id="<?= $menu->css_id ?>"
class="g-menu-link <?= $menu->css_class ?>"
href="<?= $menu->url ?>"
title="<?= $menu->label->for_html_attr() ?>">
<?= $menu->label->for_html() ?>
</a>
</li>

0 comments on commit 76c0c7f

Please sign in to comment.