A more flexible toc()

Discussions and requests related to new CMSimple features, plugins, templates etc. and how to develop.
Please don't ask for support at this forums!
cmb
Posts: 13273
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

A more flexible toc()

Post by cmb » Thu Jul 05, 2012 12:38 pm

Hello Community,

I wanted to have a navigation menu as a selectbox, what seems to be something quite practical for mobile sites (see e.g. http://www.touchtech.co.nz/ and resize your browser to a narrow width). I found that all I have to is to write a simple alternative to li(), copy the toc() code and change only the call to li() to my alternative li(). I can include this definitions to a mobile template. But if some functionality of toc() might change in the future, the new toc() has to be adapted. That's currently a problem with xtoc(), as even xtoc28 doesn't regard the new "Hidden pages toc" setting. So what about adding an optional parameter to toc() which can be given an alternative li() function? So toc() could be changed to:

Code: Select all

function toc($start = NULL, $end = NULL, $li = 'li') {
    ...
    return call_user_func($li, $ta, $start);
} 
This is fully compatible to the current solution, but allows to call e.g. toc(1,1,'myli'). Another potential benefit: this might be helpful in disentangling li(), which is somewhat hard to read and understand.

Christoph
Christoph M. Becker – Plugins for CMSimple_XH

Holger
Site Admin
Posts: 3102
Joined: Mon May 19, 2008 7:10 pm
Location: Hessen, Germany
Contact:

Re: A more flexible toc()

Post by Holger » Thu Jul 05, 2012 2:02 pm

Hi Christoph,

I really appreciate that.


Holger

svasti
Posts: 1649
Joined: Wed Dec 17, 2008 5:08 pm

Re: A more flexible toc()

Post by svasti » Thu Jul 05, 2012 3:03 pm

Interesting idea, could have used it, when I had to split the toc over several divs. As it doesn't hurt, let's do it.
svasti

Hm, just checked again, I also used a variable ($category) just as in xtoc() to be passed to li(). Probably could also be done with your solution, like toc(1,1,'myli1'), toc(1,1,'myli2'), toc(1,1,'myli3')

cmb
Posts: 13273
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

Re: A more flexible toc()

Post by cmb » Sat Jul 28, 2012 4:30 pm

Done (r222).
svasti wrote:Hm, just checked again, I also used a variable ($category) just as in xtoc() to be passed to li(). Probably could also be done with your solution, like toc(1,1,'myli1'), toc(1,1,'myli2'), toc(1,1,'myli3')
I'm not sure -- I didn't dive into xtoc() yet (just seen, that it has a xnewsbox() which offers similar possibilities as the Also plugin and Coco_XH). But basically one can even use a single myli() with an additional parameter and call it like this:

Code: Select all

toc(1, 1, create_function('$ta,$st', 'return myli($ta, $st, \'arg1\');')
toc(1, 1, create_function('$ta,$st', 'return myli($ta, $st, \'arg2\');') 
Christoph M. Becker – Plugins for CMSimple_XH

maeg
Posts: 525
Joined: Fri Feb 20, 2009 2:27 pm
Location: Agerbæk, Denmark
Contact:

Re: A more flexible toc()

Post by maeg » Sun Dec 08, 2013 10:48 am

I have been playing a littel around with this.

I need a solution for a mobiletemplate that works in windowsphone.

It seems not to work :(

cmb
Posts: 13273
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

Re: A more flexible toc()

Post by cmb » Sun Dec 08, 2013 12:58 pm

To have a navigation menu (aka. TOC) as a select box you can start with the following code. Put it in the file cmsimple/userfuncs.php:

Code: Select all

<?php

/**
 * Returns a navigation menu as select element.
 *
 * The navigation requires JavaScript to be enabled.
 *
 * The function is meant to passed as <var>$li</var> argument to toc().
 * Currently, it supports only full menus, i.e.
 * <code>toc(null, null, 'liSelect');</code>
 * 
 * @param array $pages Array of page indexes for the menu.
 * @param int   $start A start level.
 *
 * @return (X)HTML.
 */
function liSelect($pages, $start)
{
    global $sn, $h, $u, $l, $s;
    
    $prefix = '&nbsp;&nbsp;';
    $o = <<<HTM
    
<select class="tpl_selectnav" onchange="if (this.value) location = this.value">

HTM;
    if ($s < 0) {
        $o .= <<<HTM
    <option value=""></option>

HTM;
    }
    foreach ($pages as $i) {
        $indent = str_repeat($prefix, $l[$i] - 1);
        $selected = $i == $s ? ' selected="selected"' : '';
        $url = $sn . '?' . $u[$i];
        $o .= <<<HTM
    <option value="$url"$selected>{$indent}{$h[$i]}</option>

HTM;
    }
    $o .= <<<HTM
</select>

HTM;
    return $o;
}

?>
Then you can use it in the template:

Code: Select all

<?php echo toc(null, null, 'liSelect');?>
You can style it with:

Code: Select all

.tpl_selectnav {/* ... */}
(as far as styling select elements is possible)

Note, that such select menus require JavaScript to be enabled.
Christoph M. Becker – Plugins for CMSimple_XH

maeg
Posts: 525
Joined: Fri Feb 20, 2009 2:27 pm
Location: Agerbæk, Denmark
Contact:

Re: A more flexible toc()

Post by maeg » Sun Dec 08, 2013 3:57 pm

Hmm

That dosen't - it gives me a blank page.

Can there be a error in the code??

cmb
Posts: 13273
Joined: Tue Jun 21, 2011 11:04 am
Location: Mü-Sa, RLP, DE
Contact:

Re: A more flexible toc()

Post by cmb » Sun Dec 08, 2013 4:25 pm

maeg wrote:Can there be a error in the code??
That's possible. Enable the debug mode -- that shoud give a helpful error message.
Christoph M. Becker – Plugins for CMSimple_XH

maeg
Posts: 525
Joined: Fri Feb 20, 2009 2:27 pm
Location: Agerbæk, Denmark
Contact:

Re: A more flexible toc()

Post by maeg » Sun Dec 08, 2013 4:52 pm

cmb wrote: That's possible. Enable the debug mode -- that shoud give a helpful error message.
I can't do that.
When i upload the userfuncs.php file the site crash and gives me a blank site

ZiPs
Posts: 633
Joined: Thu May 22, 2008 6:17 pm
Location: Faxe, Denmark
Contact:

Re: A more flexible toc()

Post by ZiPs » Sun Dec 08, 2013 7:32 pm

Try this.

Code: Select all

    <?php

    /**
     * Returns a navigation menu as select element.
     *
     * The navigation requires JavaScript to be enabled.
     *
     * The function is meant to passed as <var>$li</var> argument to toc().
     * Currently, it supports only full menus, i.e.
     * <code>toc(null, null, 'liSelect');</code>
     * 
     * @param array $pages Array of page indexes for the menu.
     * @param int   $start A start level.
     *
     * @return (X)HTML.
     */
    function liSelect($pages, $start)
    {
        global $sn, $h, $u, $l, $s;
        
        $prefix = '&nbsp;&nbsp;';
        $o = '<<<HTM
        
    <select class="tpl_selectnav" onchange="if (this.value) location = this.value">

    HTM';
        if ($s < 0) {
            $o .= '<<<HTM
        <option value=""></option>

    HTM';
        }
        foreach ($pages as $i) {
            $indent = str_repeat($prefix, $l[$i] - 1);
            $selected = $i == $s ? ' selected="selected"' : '';
            $url = $sn . '?' . $u[$i];
            $o .= '<<<HTM
        <option value="$url"$selected>{$indent}{$h[$i]}</option>

    HTM';
        }
        $o .= '<<<HTM
    </select>

    HTM';
        return $o;
    }

    ?>
Preben Dahl | Webmaster cmsimple.dk | Projekt-og domæne ejer Gert Ebersbach

Post Reply