theme configurator: number of navbar links
theme configurator: number of navbar links
Hey guys,
sorry if this has already been answered somewhere, but I found it pretty difficult to come up with search terms for this.
Here's what I'd like to do: A lot of templates now have navbars, mostly in the header, which in v1.1 can be configured using the fabulous theme configurator. Usually, the template author sets a fixed number of those links, but sometimes users need more or less navbar links. So it would be nice (forget nice, it would be awesome!) if users could actually set the number of navbar links in the admin frontend and then enter text and URL for each link.
Is that possible? Does anybody have a working code example for this?
YL
sorry if this has already been answered somewhere, but I found it pretty difficult to come up with search terms for this.
Here's what I'd like to do: A lot of templates now have navbars, mostly in the header, which in v1.1 can be configured using the fabulous theme configurator. Usually, the template author sets a fixed number of those links, but sometimes users need more or less navbar links. So it would be nice (forget nice, it would be awesome!) if users could actually set the number of navbar links in the admin frontend and then enter text and URL for each link.
Is that possible? Does anybody have a working code example for this?
YL
-
- Core Developer
- Posts: 30022
- Joined: Tue Sep 16, 2003 9:45 pm
- Location: Cologne, Germany
- Contact:
Re: theme configurator: number of navbar links
Hi!
This could already be done using more advanced PHP code.
The idea is this: The config.inc.php file calls up the serendipity_loadThemeOptions() function to pick the current configuration. Then depending on the output of that, you can check the resultset array on which number of elements the user wanted.
And then you use foreach() to expand the $template_options array with the number of required items.
An example (untested) would be this:
HTH,
Garvin
This could already be done using more advanced PHP code.
The idea is this: The config.inc.php file calls up the serendipity_loadThemeOptions() function to pick the current configuration. Then depending on the output of that, you can check the resultset array on which number of elements the user wanted.
And then you use foreach() to expand the $template_options array with the number of required items.
An example (untested) would be this:
Code: Select all
<?php
$template_config = array(
array(
'var' => 'amount',
'name' => 'Number of navlinks',
'description' => '',
'type' => 'string',
'default' => '5',
)
);
$vars = serendipity_loadThemeOptions($template_config);
for ($i = 0; $i <= $vars['amount']; $i++) {
$template_config[] = array(
'var' => 'navlink' . $i,
'name' => 'Navlink ' . $i,
'description' => '',
'type' => 'string',
'default' => 'http://',
);
}
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/
# 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/
Re: theme configurator: number of navbar links
Now it's tested, and it works! However, a little fix for your code (Gosh, I can't believe I just typed that ... ): I've set the initial value for $i to 1 instead of 0 (see code block below), since if it's set to 0, the foreach creates i+1 navlinks.garvinhicking wrote:An example (untested) would be this:
Also, there's a little problem which probably isn't very user-friendly: If the number of navlinks is changed, the Theme/Style options have to be reloaded to show the new setting. I'm no coder, but I guess it would need some AJAX stuff like in the new plugin configuration to do this dynamically ..?
Oh, and a follow-up question: How do I integrate other stuff into this like a) the text for the navlink and b) stuff for a 'single' configuration option (i.e. one that needs to be put out just once, I hope you understand what I mean ...)?
I tried to figure out a) by myself and came up with this code:
Code: Select all
$template_config = array(
array(
'var' => 'amount',
'name' => 'Number of navlinks',
'description' => '',
'type' => 'string',
'default' => '5',
)
);
$vars = serendipity_loadThemeOptions($template_config);
for ($i = 1; $i <= $vars['amount']; $i++) {
$template_config[] = array(
array(
'var' => 'navlink' . $i . 'text',
'name' => NAV_LINK_TEXT . $i,
'description' => NAV_LINK_DESC,
'type' => 'string',
'default' => NAV_DEFAULT_ . $i,
),
array(
'var' => 'navlink' . $i,
'name' => 'Navlink ' . $i,
'description' => ' ',
'type' => 'string',
'default' => '#',
);
}
-
- Core Developer
- Posts: 30022
- Joined: Tue Sep 16, 2003 9:45 pm
- Location: Cologne, Germany
- Contact:
Re: theme configurator: number of navbar links
Hi!
Your problem simply was that you created another level of arrays, which is not so good, because then the structure is no longer what s9y requires it to be.
Best regards,
Garvin
Yeah, that should be fine. You could have also used "<" instead of "<=" in the lop.Now it's tested, and it works! However, a little fix for your code (Gosh, I can't believe I just typed that ... ): I've set the initial value for $i to 1 instead of 0 (see code block below), since if it's set to 0, the foreach creates i+1 navlinks.
Yes, this definitely requires Ajax. You can emit custom HTML using your config.inc.php for the panel, but the ajax stuff creates more overhead and things to do than the whole stuff is really worth. So IMHO it's not so bad to make the user hit the save button if he changes the amount of navlinks...Also, there's a little problem which probably isn't very user-friendly: If the number of navlinks is changed, the Theme/Style options have to be reloaded to show the new setting. I'm no coder, but I guess it would need some AJAX stuff like in the new plugin configuration to do this dynamically ..?
Try this:Oh, and a follow-up question: How do I integrate other stuff into this like a) the text for the navlink and b) stuff for a 'single' configuration option (i.e. one that needs to be put out just once, I hope you understand what I mean ...)?
Code: Select all
$template_config = array(
array(
'var' => 'amount',
'name' => 'Number of navlinks',
'description' => '',
'type' => 'string',
'default' => '5',
),
array(
'var' => 'anyothervar',
'name' => 'Any other var that will not be looped',
'description' => 'blabla',
'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,
'type' => 'string',
'default' => NAV_DEFAULT_ . $i
);
$template_config[] = array(
'var' => 'navlink' . $i . 'text',
'name' => NAV_LINK_TEXT . $i,
'description' => NAV_LINK_DESC,
'type' => 'string',
'default' => NAV_DEFAULT_ . $i
);
}
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/
# 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/
Re: theme configurator: number of navbar links
I'll skip the part in which I promise you my first-born etc.
The part that isn't working (yet, I hope): the default values for NAV_LINK_TEXT, which are NAV_DEFAULT_1 to NAV_DEFAULT_5 (all of them @define-d in lang_en.inc.php and lang_de.inc.php) are not, well, evaluated obviously. Which means the default value is set to NAV_DEFAULT_1 instead of the value of NAV_DEFAULT_1. I hope you understand what I mean ... also, I just found out that when I change this to i.e. 'Index', the navbar link in question is still labeled 'NAV_DEFAULT_1', so obviously there is something wrong here ...
Can we by the way use arrays in lang_XY.inc.php? Maybe that would make it easier ..? (Just a non-coders thought ...)
YL
You're right. Well, actually the user has to hit save and reload the 'Manage styles' page, but let's just assume that s9y users are smart enough to do that and not overload thisgarvinhicking wrote:So IMHO it's not so bad to make the user hit the save button if he changes the amount of navlinks...
I did. Didn't work right out of the box (I think you had some copy & paste error in there, but I'll forgive you ), but I think I fixed it, except for one tiny little thing. Let's see the code first (this works), the single example is cocomment support, plus I switched the order to keep that navbar stuff together:garvinhicking wrote:Try this:
Code: Select all
$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' => NAV_DEFAULT_ . $i,
);
$template_config[] = array(
'var' => 'navlink' . $i,
'name' => NAV_LINK_URL . ' #' . $i,
'description' => NAV_LINK_URL_DESC . ' #' . $i,
'type' => 'string',
'default' => '#',
);
}
Can we by the way use arrays in lang_XY.inc.php? Maybe that would make it easier ..? (Just a non-coders thought ...)
YL
-
- Core Developer
- Posts: 30022
- Joined: Tue Sep 16, 2003 9:45 pm
- Location: Cologne, Germany
- Contact:
Re: theme configurator: number of navbar links
Hi!
you need to use:
So, constants are for the time being IMHO the better way.
Regards,
Garvin
A great name for a first-born!YellowLed wrote:I'll skip the part in which I promise you my first-born etc.
This is because constants cannot be concatenated. Instead of this:The part that isn't working (yet, I hope): the default values for NAV_LINK_TEXT, which are NAV_DEFAULT_1 to NAV_DEFAULT_5 (all of them @define-d in lang_en.inc.php and lang_de.inc.php) are not, well, evaluated obviously. Which means the default value is set to NAV_DEFAULT_1 instead of the value of NAV_DEFAULT_1. I hope you understand what I mean ... also, I just found out that when I change this to i.e. 'Index', the navbar link in question is still labeled 'NAV_DEFAULT_1', so obviously there is something wrong here ...
Code: Select all
'default' => NAV_DEFAULT_ . $i,
Code: Select all
'default' => constant('NAV_DEFAULT_' . $i),
Arrays actually are IMHO harder to read and initialize. Also they can be overwritten in user-scope which might lead to injection attacks by overwriting them...Can we by the way use arrays in lang_XY.inc.php? Maybe that would make it easier ..? (Just a non-coders thought ...)
So, constants are for the time being IMHO the better way.
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/
# 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/
Re: theme configurator: number of navbar links
Yeah, but if this becomes common sense, we'll have a planet of Garvins within a few years ...garvinhicking wrote:A great name for a first-born!
I hate to be the bearer of bad news, but this doesn't work. Also, although I have set the navlink URL default to '#', it still reads 'http://' on the 'Manage Styles' page, so I'm guessing that part of the information entered in this is cached or stored somewhere and not reset or overwritten when it's changed ... yes, I already cleared templates_c.garvinhicking wrote:you need to use:
Code: Select all
'default' => constant('NAV_DEFAULT_' . $i),
This can't be some kind of weird side effect because I'm still doing this on my local apache, can it? (Just guessing.)
YL
-
- Core Developer
- Posts: 30022
- Joined: Tue Sep 16, 2003 9:45 pm
- Location: Cologne, Germany
- Contact:
Re: theme configurator: number of navbar links
Hi!
Best regards,
Garvin
Are you sure that the default is displayed? Once you saved the theme configuration, the value that is shown in the input field is saved in the database! Have a look at the serendipity_options Database table...I hate to be the bearer of bad news, but this doesn't work. Also, although I have set the navlink URL default to '#', it still reads 'http://' on the 'Manage Styles' page, so I'm guessing that part of the information entered in this is cached or stored somewhere and not reset or overwritten when it's changed ... yes, I already cleared templates_c.
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/
# 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/
Re: theme configurator: number of navbar links
Gnagnagna. The false URL default was my fault, had the wrong $template_option variable in the index.tplgarvinhicking wrote:Are you sure that the default is displayed? Once you saved the theme configuration, the value that is shown in the input field is saved in the database! Have a look at the serendipity_options Database table...
However in the serendipity_options db table I have something like this:
navlink0text -> NAV_DEFAULT_0
navlink1text -> NAV_DEFAULT_1
and so on ...
YL
-
- Core Developer
- Posts: 30022
- Joined: Tue Sep 16, 2003 9:45 pm
- Location: Cologne, Germany
- Contact:
Re: theme configurator: number of navbar links
Hi!
If you have NAV_DEFAULT_0 in your database, that means that you have not defined a constant "NAV_DEFAULT_0"...?!
Regards,
Garvin
If you have NAV_DEFAULT_0 in your database, that means that you have not defined a constant "NAV_DEFAULT_0"...?!
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/
# 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/
Re: theme configurator: number of navbar links
True, I guess. But I also have NAV_DEFAULT_1 and so on in the database - and those are definitely defined in the lang_*.inc.php files, i.e. lang_en.inc.php:garvinhicking wrote:If you have NAV_DEFAULT_0 in your database, that means that you have not defined a constant "NAV_DEFAULT_0"...?!
Code: Select all
<?php
[ ... some other stuff ... ]
@define('NAV_DEFAULT_1', 'Index');
@define('NAV_DEFAULT_2', 'Email');
@define('NAV_DEFAULT_3', 'RSS');
@define('NAV_DEFAULT_4', 'About this site');
@define('NAV_DEFAULT_5', 'About Me');
?>
Code: Select all
constant(NAV_DEFAULT_ . $i)
-
- Core Developer
- Posts: 30022
- Joined: Tue Sep 16, 2003 9:45 pm
- Location: Cologne, Germany
- Contact:
Re: theme configurator: number of navbar links
Hi!
Are you aware that I did not post
but instead
?
Best regards,
Garvin
Are you aware that I did not post
Code: Select all
constant(NAV_DEFAULT_ . $i)
Code: Select all
constant('NAV_DEFAULT_' . $i)
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/
# 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/
Re: theme configurator: number of navbar links
No, I wasn't Sorry about that. However, the second code doesn't work eithergarvinhicking wrote:Are you aware that I did not postbut insteadCode: Select all
constant(NAV_DEFAULT_ . $i)
?Code: Select all
constant('NAV_DEFAULT_' . $i)
Also, I just saw that my local s9y installation (this is 1.1-beta5 with PHP 5.1.6 and MySQL 4.1.11 on an Apache 2.0.54 under Linux, if any of this is necessary) doesn't seem to switch the lang_*.inc.php files for the template options - I had switched this test blog to English earlier to be able to follow some thread in here better. I just now switched it back to German, but all the template options from config.inc.php are still displayed in English. I remember Carl mentioning a similar problem in a PM, by the way.
YL
-
- Core Developer
- Posts: 30022
- Joined: Tue Sep 16, 2003 9:45 pm
- Location: Cologne, Germany
- Contact:
Re: theme configurator: number of navbar links
Hi!
Regards,
Garvin
Did you make sure to delete the serendipity_options table before you replaced the code? Otherwise, not a default will be shown, but the last stored value...No, I wasn't Sorry about that. However, the second code doesn't work either
How does your config.inc.php file currently include the language files?Also, I just saw that my local s9y installation (this is 1.1-beta5 with PHP 5.1.6 and MySQL 4.1.11 on an Apache 2.0.54 under Linux, if any of this is necessary) doesn't seem to switch the lang_*.inc.php files for the template options - I had switched this test blog to English earlier to be able to follow some thread in here better. I just now switched it back to German, but all the template options from config.inc.php are still displayed in English. I remember Carl mentioning a similar problem in a PM, by the way.
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/
# 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/
Re: theme configurator: number of navbar links
No, but I also don't remember you mentioning this earlier Yeah, now it works!garvinhicking wrote:Did you make sure to delete the serendipity_options table before you replaced the code? Otherwise, not a default will be shown, but the last stored value...
Hm. But how will this possibly work in other environments, especially if users don't have access to their database tables? Since I copied most of the variables used from another theme, should I give them unique names like i.e. YAML_NAV_DEFAULT_0 and so on?
That's also code I borrowed somewhere else, I think from the new default theme I did with Carl ...garvinhicking wrote:How does your config.inc.php file currently include the language files?
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';
}