Ticket #162 (new enhancement)

Opened 3 months ago

Last modified 3 months ago

Solar_View_Helper_ActionList

Reported by: rodrigo moraes <rodrigo.moraes@…> Owned by: pmjones
Priority: trivial Component: code
Keywords: Solar_View_Helper Cc:

Description

Long long ago we had something like Solar_View_Helper_ActionList (I don't remember the exact name). Let's bring it back! Here's how it could look like:

<?php
/**
 * 
 * Helper to build a list of action links.
 * 
 * @category Tipos
 * 
 * @package Tipos_View
 * 
 * @author Rodrigo Moraes <rodrigo.moraes@gmail.com>
 * 
 * @license http://opensource.org/licenses/bsd-license.php BSD
 * 
 * @version $Id: ListNav.php 98 2007-12-18 11:52:58Z rodrigo.moraes $
 *
 */
class Tipos_View_Helper_ActionList extends Solar_View_Helper
{
    /**
     * 
     * User-defined configuration values.
     * 
     * Keys are...
     * 
     * `list_id`
     * : (string) The ID attribute for the list.
     * 
     * `list_class`
     * :(string) The class attribute for the list.
     *
     * `first_class`
     * : (string) The CSS class for the first item in the list.
     *
     * `curr_class`
     * : (string) The CSS class for the currently active item in the list.     
     * 
     * `last_class`
     * : (string) The CSS class for the last item in the list.
     * 
     * @var array
     * 
     */
    protected $_Tipos_View_Helper_ActionList = array(
        'list_id'     => null,
        'list_class'  => null,
        'first_class' => 'first',
        'curr_class'  => 'curr',
        'last_class'  => 'last',
    );
    
    /**
     *
     * Returns a list of action links.
     *
     * @param array $items List of items in the form href => content.
     *
     * @param string $active The active item in the list.
     *
     * @param array $config Additional config options to build the list.
     *
     * @return string
     *
     */
    public function actionList($items, $active = null, $config = null)
    {
        if ($config) {
            $config = array_merge($this->_config, (array) $config);
        } else {
            $config = $this->_config;
        }
        
        $html = array();
        
        // start the list
        $attribs = $this->_view->attribs(array(
            'id'    => $config['list_id'],
            'class' => $config['list_class'],
        ));
        
        $html[] = "<ul$attribs>";

        $iter = 1;
        $total = count($items);
        foreach ($items as $href => $text) {
            $attribs = '';
            $class = array();
            
            // is this the active item?
            if ($active && ($href == $active) && $config['curr_class']) {
                $class[] = $config['curr_class'];
            }
            
            // is this the first or last item?
            if ($iter == 1 && $config['first_class']) {
                $class[] = $config['first_class'];
            } elseif ($iter == $total && $config['last_class']) {
                $class[] = $config['last_class'];
            }
            
            if (! empty($class)) {
                $attribs = $this->_view->attribs(array(
                    'class' => $class,
                ));
            }
            
            $html[] = "<li$attribs>";
            
            if (is_string($href)) {
                // action item
                $html[] = $this->_view->action($href, $text);                
            } else {
                // no href
                $html[] = $text;                
            }
            
            $html[] = "</li>";
            $iter++;
        }
        
        // done.
        $html[] = "</ul>";
        return implode("\n", $html);
    }
}

Attachments

Change History

Changed 3 months ago by rodrigo moraes <rodrigo.moraes@…>

Updated code is in Lux:

http://code.google.com/p/lux/source/browse/trunk/Lux/View/Helper/ActionList.php

Some notes:

  • 'first_class' and 'last_class' are just CSS hooks to style some lists for browsers that don't support :first and :last.
  • "named" items are useful for items that have an unique name (stored in a data source for example); in some cases it is much easier to set the active item using its name than its href.

All in all, I tried to follow the simplicity found in the pager helper. I like how plain it is. And although in many cases we will build lists "by hand", I found that a helper like this is handy and flexible enough for simple menus.

Add/Change #162 (Solar_View_Helper_ActionList)

Author



Action
as new
 
Note: See TracTickets for help on using tickets.