Page 1 of 2

A more flexible toc()

Posted: Thu Jul 05, 2012 12:38 pm
by cmb
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

Re: A more flexible toc()

Posted: Thu Jul 05, 2012 2:02 pm
by Holger
Hi Christoph,

I really appreciate that.


Holger

Re: A more flexible toc()

Posted: Thu Jul 05, 2012 3:03 pm
by svasti
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')

Re: A more flexible toc()

Posted: Sat Jul 28, 2012 4:30 pm
by cmb
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\');') 

Re: A more flexible toc()

Posted: Sun Dec 08, 2013 10:48 am
by maeg
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 :(

Re: A more flexible toc()

Posted: Sun Dec 08, 2013 12:58 pm
by cmb
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.

Re: A more flexible toc()

Posted: Sun Dec 08, 2013 3:57 pm
by maeg
Hmm

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

Can there be a error in the code??

Re: A more flexible toc()

Posted: Sun Dec 08, 2013 4:25 pm
by cmb
maeg wrote:Can there be a error in the code??
That's possible. Enable the debug mode -- that shoud give a helpful error message.

Re: A more flexible toc()

Posted: Sun Dec 08, 2013 4:52 pm
by maeg
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

Re: A more flexible toc()

Posted: Sun Dec 08, 2013 7:32 pm
by ZiPs
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;
    }

    ?>