HTML_Menu
[ class tree: HTML_Menu ] [ index: HTML_Menu ] [ all elements ]

Source for file DirectRenderer.php

Documentation is available at DirectRenderer.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de>                           |
  17. // |          Sebastian Bergmann <sb@sebastian-bergmann.de>               |
  18. // |          Alexey Borzov <avb@php.net>                                 |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: DirectRenderer.php,v 1.4 2006/06/01 18:17:28 avb Exp $
  22. //
  23.  
  24. require_once 'HTML/Menu/Renderer.php';
  25.  
  26. /**
  27.  * The renderer that generates HTML for the menu all by itself.
  28.  * 
  29.  * Inspired by HTML_Menu 1.0 code
  30.  * 
  31.  * @version  $Revision: 1.4 $
  32.  * @author   Ulf Wendel <ulf.wendel@phpdoc.de>
  33.  * @author   Alexey Borzov <avb@php.net>
  34.  * @access   public
  35.  * @package  HTML_Menu
  36.  */
  37. {
  38.    /**
  39.     * Generated HTML for the menu
  40.     * @var string 
  41.     */
  42.     var $_html '';
  43.  
  44.    /**
  45.     * Generated HTML for the current menu "table"
  46.     * @var string 
  47.     */
  48.     var $_tableHtml '';
  49.     
  50.    /**
  51.     * Generated HTML for the current menu "row"
  52.     * @var string 
  53.     */
  54.     var $_rowHtml '';
  55.  
  56.    /**
  57.     * The HTML that will wrap around menu "table"
  58.     * @see setMenuTemplate()
  59.     * @var array 
  60.     */
  61.     var $_menuTemplate = array('<table border="1">''</table>');
  62.  
  63.    /**
  64.     * The HTML that will wrap around menu "row"
  65.     * @see setRowTemplate()
  66.     * @var array 
  67.     */
  68.     var $_rowTemplate = array('<tr>''</tr>');
  69.  
  70.    /**
  71.     * Templates for menu entries
  72.     * @see setEntryTemplate()
  73.     * @var array 
  74.     */
  75.     var $_entryTemplates = array(
  76.         HTML_MENU_ENTRY_INACTIVE    => '<td>{indent}<a href="{url}">{title}</a></td>',
  77.         HTML_MENU_ENTRY_ACTIVE      => '<td>{indent}<b>{title}</b></td>',
  78.         HTML_MENU_ENTRY_ACTIVEPATH  => '<td>{indent}<b><a href="{url}">{title}</a></b></td>',
  79.         HTML_MENU_ENTRY_PREVIOUS    => '<td><a href="{url}">&lt;&lt; {title}</a></td>',
  80.         HTML_MENU_ENTRY_NEXT        => '<td><a href="{url}">{title} &gt;&gt;</a></td>',
  81.         HTML_MENU_ENTRY_UPPER       => '<td><a href="{url}">^ {title} ^</a></td>',
  82.         HTML_MENU_ENTRY_BREADCRUMB  => '<td><a href="{url}">{title}</a> &gt;&gt; </td>'
  83.     );
  84.  
  85.     function finishMenu($level)
  86.     {
  87.         $this->_html     .=  $this->_menuTemplate[0$this->_tableHtml $this->_menuTemplate[1];
  88.         $this->_tableHtml '';
  89.     }
  90.  
  91.     function finishRow($level)
  92.     {
  93.         $this->_tableHtml .= $this->_rowTemplate[0$this->_rowHtml $this->_rowTemplate[1];
  94.         $this->_rowHtml    '';
  95.     }
  96.  
  97.     function renderEntry($node$level$type)
  98.     {
  99.         $keys = array('{indent}');
  100.         if ('tree' == $this->_menuType || 'sitemap' == $this->_menuType{
  101.             $values = array(str_repeat('&nbsp;&nbsp;&nbsp;'$level));
  102.         else {
  103.             $values = array('');
  104.         }
  105.         foreach ($node as $k => $v{
  106.             if ('sub' != $k && is_scalar($v)) {
  107.                 $keys[]   '{' $k '}';
  108.                 $values[$v;
  109.             }
  110.         }
  111.         $this->_rowHtml .= str_replace($keys$values$this->_entryTemplates[$type]);
  112.     }
  113.  
  114.  
  115.    /**
  116.     * returns the HTML generated for the menu
  117.     *
  118.     * @access public
  119.     * @return string 
  120.     */
  121.     function toHtml()
  122.     {
  123.         return $this->_html;
  124.     // end func toHtml
  125.  
  126.  
  127.    /**
  128.     * Sets the menu template (HTML that wraps around rows)
  129.     *  
  130.     * @access public
  131.     * @param  string    this will be prepended to the rows HTML
  132.     * @param  string    this will be appended to the rows HTML
  133.     */
  134.     function setMenuTemplate($prepend$append)
  135.     {
  136.         $this->_menuTemplate = array($prepend$append);
  137.     }
  138.  
  139.  
  140.    /**
  141.     * Sets the row template (HTML that wraps around entries)
  142.     *  
  143.     * @access public
  144.     * @param  string    this will be prepended to the entries HTML
  145.     * @param  string    this will be appended to the entries HTML
  146.     */
  147.     function setRowTemplate($prepend$append)
  148.     {
  149.         $this->_rowTemplate = array($prepend$append);
  150.     }
  151.  
  152.  
  153.    /**
  154.     * Sets the template for menu entry.
  155.     * 
  156.     * The template should contain at least the {title} placeholder, can also contain
  157.     * {url} and {indent} placeholders, depending on entry type.
  158.     * 
  159.     * @access public
  160.     * @param  mixed     either type (one of HTML_MENU_ENTRY_* constants) or an array 'type' => 'template'
  161.     * @param  string    template for this entry type if $type is not an array
  162.     */
  163.     function setEntryTemplate($type$template = null)
  164.     {
  165.         if (is_array($type)) {
  166.             // array_merge() will not work here: the keys are numeric
  167.             foreach ($type as $typeId => $typeTemplate{
  168.                 if (isset($this->_entryTemplates[$typeId])) {
  169.                     $this->_entryTemplates[$typeId$typeTemplate;
  170.                 }
  171.             }
  172.         else {
  173.             $this->_entryTemplates[$type$template;
  174.         }
  175.     }
  176. }
  177. ?>

Documentation generated on Mon, 11 Mar 2019 14:46:20 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.