theme configurator: number of navbar links

Skinning and designing Serendipity (CSS, HTML, Smarty)
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Post by yellowled »

Garvin, I really don't want to annoy you, but I just stumbled across another problem with this method. At the moment, the code for this navbar we're trying to 'create dynamically' looks like this:

Code: Select all

<li><a href="{$template_option.navlink0url}" title="{$template_option.navlink0text}">{$template_option.navlink0text}</a></li>
<li><a href="{$template_option.navlink1url}" title="{$template_option.navlink1text}">{$template_option.navlink1text}</a></li>
<li><a href="{$template_option.navlink2url}" title="{$template_option.navlink2text}">{$template_option.navlink2text}</a></li>
<li><a href="{$template_option.navlink3url}" title="{$template_option.navlink3text}">{$template_option.navlink3text}</a></li>
<li><a href="{$template_option.navlink4url}" title="{$template_option.navlink4text}">{$template_option.navlink4text}</a></li>
Of course this means that if the user opts to reduce or increase the number of navbar links, there are are either more or less links given out than needed. How do I do this? I already tried to figure it out using a {section} but the code I came up with on my poor own didn't work ...

YL
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

First, the DEFAULT constant stuff will work flawless for users without purging their database, once you got the config.inc.php coded right, the way you have it now. What you did only was necesary because your config.inc.php was wrong before.

Second, about the language inclusion: The PHP code tells that language constants are only loaded when the user is in the admin panel. If you want your own constants to be loaded all the time, you need to remove that IF-construct that checks on the $serendipty['GET'] variables.
Of course this means that if the user opts to reduce or increase the number of navbar links, there are are either more or less links given out than needed. How do I do this? I already tried to figure it out using a {section} but the code I came up with on my poor own didn't work ...
Hm, you would need to use {section} of {foreach} loops using the $template_option.amount variable as a counter. I don'T know how, since I don'T know enough about smarty for that. Maybe the Smarty documentation can help you to see how to create a loop with a variable number of iterations...

HTH,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Post by yellowled »

garvinhicking wrote:Second, about the language inclusion: The PHP code tells that language constants are only loaded when the user is in the admin panel.
But that doesn't happen. I am in a German admin panel and yet I get English language constants. By the way this happens with other templates using the same method, too. Whether I use the new default template or my YAML port, I always get English language constants in an otherwise German admin panel.
garvinhicking wrote:Hm, you would need to use {section} of {foreach} loops using the $template_option.amount variable as a counter. I don'T know how, since I don'T know enough about smarty for that. Maybe the Smarty documentation can help you to see how to create a loop with a variable number of iterations...
Gee, and here I thought you knew everything :wink: Well, I'll try and dig into the Smarty docs some time next week, since without this the whole possibility to set the number of navbar links is pretty much pointless ... if anyone else reads this and has a way to do this, please raise your voice - I'm a pretty lousy coder ... :)
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!
But that doesn't happen. I am in a German admin panel and yet I get English language constants. By the way this happens with other templates using the same method, too. Whether I use the new default template or my YAML port, I always get English language constants in an otherwise German admin panel.
Maybe those themes are missing a UTF-8 subdirectory with a lang_de.inc.php file?
Gee, and here I thought you knew everything :wink:
Nah. Templates are not my best area of expertise. :)
Well, I'll try and dig into the Smarty docs some time next week, since without this the whole possibility to set the number of navbar links is pretty much pointless ... if anyone else reads this and has a way to do this, please raise your voice - I'm a pretty lousy coder ... :)
:) Tell us if you find anything! I'm also sure d_cee or carl might get you a headstart? Or judebert. He tends to know smarty sections :)

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Post by yellowled »

garvinhicking wrote:Maybe those themes are missing a UTF-8 subdirectory with a lang_de.inc.php file?
Erm ... well, yeah. So I need a UTF-8 subdir in the template folder and a copy of each language file encoded in UTF-8 in there? I've never seen that in any template, although it surely makes sense.
garvinhicking wrote::) Tell us if you find anything! I'm also sure d_cee or carl might get you a headstart? Or judebert. He tends to know smarty sections :)
Yeah, where are you guys, huh? :wink:

YL
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Post by yellowled »

Me again :)

So that UTF-8 thing worked out, but I'm stuck with the Smarty code. Here's my current config.inc.php:

Code: Select all

<?php
// Be nice to the frontend users. They don't need the additional constants
// and file lookups. Only load them when in Admin mode.
if ($serendipity['GET']['adminModule'] == 'templates' || $serendipity['POST']['adminModule'] != 'templates') {
    // Probe for a language include with constants. Still include defines 
    // later on, if some constants were missing
    $probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
    if (file_exists($probelang)) {
        include $probelang;
    } 
    
    include dirname(__FILE__) . '/lang_en.inc.php';
}

$template_config = array(
    array(
        'var'           => 'cocommentactive',
        'name'          => COCOMMENT_ACTIVE,
        'description'   => COCOMMENT_ACTIVE_DESC,
        'type'          => 'select',
        'default'       => 'inactive',
        'select_values' => array('active' => USE_COCOMMENT,
                                 'inactive' => NO_COCOMMENT)
    ),
    array(
        'var'           => 'amount',
        'name'          => 'Number of navlinks',
        'description'   => 'Enter the number of navlinks you want to use in the navbar.',
        'type'          => 'string',
        'default'       => '5',
    )
);

$vars = serendipity_loadThemeOptions($template_config);

for ($i = 0; $i < $vars['amount']; $i++) {
    $template_config[] = array(
        'var'           => 'navlink' . $i . 'text',
        'name'          => NAV_LINK_TEXT . ' #' . $i,
        'description'   => NAV_LINK_DESC . ' #' .$i,
        'type'          => 'string',
        'default'       => constant('NAV_DEFAULT_' . $i),
	);
    $template_config[] = array(
        'var'           => 'navlink' . $i . 'url',
        'name'          => NAV_LINK_URL . ' #' . $i,
        'description'   => NAV_LINK_URL_DESC . ' #' . $i,
        'type'          => 'string',
        'default'       => '#',
    );
}
and here's the Smarty code for the index.tpl I have so far:

Code: Select all

{section name=buildnav start=0 loop=$template_option.amount step=1}
<li><a href="#" title="foo">bar</a></li>
{/section}
But how do I get the relevant '$navlink . $i . url' here? I can't use the dot concatenation here, right? Something like:

Code: Select all

<li><a href="#" title="foo">{$template_option.navlink|cat: $smarty.section.buildnav.index|cat: "text"}</a></li>
produces an error ...

Help! :)

YL
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!

Try this PHP

Code: Select all

<?php
// Be nice to the frontend users. They don't need the additional constants
// and file lookups. Only load them when in Admin mode.
if ($serendipity['GET']['adminModule'] == 'templates' || $serendipity['POST']['adminModule'] != 'templates') {
    // Probe for a language include with constants. Still include defines
    // later on, if some constants were missing
    $probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
    if (file_exists($probelang)) {
        include $probelang;
    }
   
    include dirname(__FILE__) . '/lang_en.inc.php';
}

$template_config = array(
    array(
        'var'           => 'cocommentactive',
        'name'          => COCOMMENT_ACTIVE,
        'description'   => COCOMMENT_ACTIVE_DESC,
        'type'          => 'select',
        'default'       => 'inactive',
        'select_values' => array('active' => USE_COCOMMENT,
                                 'inactive' => NO_COCOMMENT)
    ),
    array(
        'var'           => 'amount',
        'name'          => 'Number of navlinks',
        'description'   => 'Enter the number of navlinks you want to use in the navbar.',
        'type'          => 'string',
        'default'       => '5',
    )
);

$vars = serendipity_loadThemeOptions($template_config);

$navlinks = array();
for ($i = 0; $i < $vars['amount']; $i++) {
    $navlinks[] = array(
        'title' => $vars['navlink' . $i . 'text'],
        'href'  => $vars['navlink' . $i . 'url']
    );
    $template_config[] = array(
        'var'           => 'navlink' . $i . 'text',
        'name'          => NAV_LINK_TEXT . ' #' . $i,
        'description'   => NAV_LINK_DESC . ' #' .$i,
        'type'          => 'string',
        'default'       => constant('NAV_DEFAULT_' . $i),
   );
    $template_config[] = array(
        'var'           => 'navlink' . $i . 'url',
        'name'          => NAV_LINK_URL . ' #' . $i,
        'description'   => NAV_LINK_URL_DESC . ' #' . $i,
        'type'          => 'string',
        'default'       => '#',
    );
}
$serendipity['smarty']->assign_by_ref('navlinks', $navlinks);
and this smarty:

Code: Select all

{foreach from=$navlinks item="navlink"}
<li><a href="{$navlink.href}" title="{$navlink.title}">{$navlink.title}</a></li>
{/foreach}
HTH,
Garvin
[/code]
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Post by yellowled »

garvinhicking wrote:Try this PHP [...] and this smarty [...]
Is that 'Garvin is awesome ...' thread still open? :D

Boy, I gotta say, I don't have the slightest idea how you do all this and are still able to earn a living ... and JFTR: of course this works. Thanks a lot.

YL
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

:)

You're welcome. Sorry I didn't think of it earlier, i was so much thinking it should be possible with smarty, but it weren't.

You cannot fetch a $variable$variable2 concatenation in Smarty; this is only possible in PHP. Smarty sadly is a bit picky in parsing variables that should be concatenated, so that you simply can't do it.

Thus, the PHP code prepares a different Smarty variable that holds the proper contents iterated in the config.inc.php file :)

Best regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
judebert
Regular
Posts: 2478
Joined: Sat Oct 15, 2005 6:57 am
Location: Orlando, FL
Contact:

Post by judebert »

garvinhicking wrote::) Tell us if you find anything! I'm also sure d_cee or carl might get you a headstart? Or judebert. He tends to know smarty sections :)
Yeah, where are you guys, huh? :wink:

YL[/quote]

Busy, busy, busy... I'm sorry, but I'm so swamped at work I haven't been able to visit the forum more than once a week or so.

You ought to be able to concatenate Smarty variables. For instance, to get 'navlink'.$i.$text as a class, you *should* be able to:

Code: Select all

<a class="navlink{$i}{$text}" href="....">
However, to fetch a Smarty variable *called* $navlink0url, you'd need to do what Garvin showed you.

Glad it's all working!
Judebert
---
Website | Wishlist | PayPal
yellowled
Regular
Posts: 7111
Joined: Fri Jan 13, 2006 11:46 am
Location: Eutin, Germany
Contact:

Post by yellowled »

judebert wrote:Busy, busy, busy... I'm sorry, but I'm so swamped at work I haven't been able to visit the forum more than once a week or so.
No problem at all, Jude. I guess we've all experienced this, whether the reason was work or simply ... uhm ... life?

YL
carl_galloway
Regular
Posts: 1331
Joined: Sun Dec 04, 2005 5:43 pm
Location: Andalucia, Spain
Contact:

Post by carl_galloway »

I'm using this code in a new template ceejay has designed which we hope to release in a couple of days, and the template actually has two sets of navbars, one in the header, and second in a sidebar. Now I've played with the code and both work flawlessly but the config screen in the admin area is a bit messy because the two array items that specify the number of links are displayed first and then the first block of links followed by the second block of links.

The problem is that everything seems to just follow on from the previous item making it hard to read. Is there any way of adding some extra spacing or even a <div> that I can style, or do I just put up with ugly. I tried a php echo, and it worked, but only at the top or bottom of the config screen, I couldn't get it to sit between the two blocks of code that generate the links.

I hope that isn't too confusing but if anyone can help that would be awesome,

Carl
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi Carl!

The order of which elements are emitted when are specified by the order of when they are put in the sequential $template_config array. First in, first out, so to say.

You can also add "spacer" blocks in your template options, like this:

Code: Select all

$template_config[] = array(
 'var' => 'spacestuff1',
  'name' => '',
  'type' => 'seperator',
);
To emit custom HTML use this:

Code: Select all

$template_config[] = array(
 'var' => 'spacestuff1',
  'name' => '',
  'type' => 'content',
  'default' => '<b>My HTML!</b>'
);
HTH,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
carl_galloway
Regular
Posts: 1331
Joined: Sun Dec 04, 2005 5:43 pm
Location: Andalucia, Spain
Contact:

Post by carl_galloway »

Genius!

Thanks Garvin, worked perfectly.

Carl
Post Reply