Dynamic Nav Bar

Discussion corner for Developers of Serendipity.
Post Reply
Ctwizzy
Regular
Posts: 19
Joined: Thu Sep 07, 2006 10:41 pm

Dynamic Nav Bar

Post by Ctwizzy »

Just getting into playing around with S9Y now, liking it so far, just got a quick Q.

I added the following code to index.tpl

Code: Select all

<ul class="nav_bar">
		<li><a href=""></a></li>
		<li><a href=""></a></li>
		<li><a href=""></a></li>
		<li><a href=""></a></li>
		<li><a href=""></a></li>
		<li><a href=""></a></li>
  	</ul>
Now keeping with the seperation of design and data, I read that index.php reads from the tpl file (this is how smarty works?), so I cracked open the index.php file and saw all the php code.

It would be nice if I could just develop a plug-in that way the user could specify the number and paths to each new page. But that would only work in the side bar, there isnt a nav-bar section.

So I am wondering how others who did add a nav bar did it, as currently I was going to just whip up a xml file with the names and paths, and then have index.php read that in, which then I assume populates the index.tpl smarty template.

Am I on the right path here, or going totally overboard...

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

Re: Dynamic Nav Bar

Post by garvinhicking »

Hi!

There are actually several ways to cope with this.

The most easy, but manual approach is to put your HTML markup with the links into a "HTML Page nugget" event plugin. This will allow you to place arbitrary HTML into the template, without needing to modify the template.

A second way would be to write your own event plugin that listens on the same event hook like the Page Nugget: frontend_header. Then your event plugin can use PHP code to read in an XML file and output it as plain HTML.

A third way, and probably the one you might want to go is to create/register a custom smarty function. This is explained in depth in the s9y documentation. The short way is to edit/create a config.inc.php file in your selected template's directory. In that file you enter your function:

Code: Select all

<?php
function my_menu($params, &$smarty) {
$out = '<ul class="nav_bar">';
$items = array('A' => 'www.a.de', 'B' => 'www.b.de, 'C' => 'www.c.de');
foreach($items AS $item => $link) {
  $out .= '<li><a href="' . $link . '">' . $item . '</a>';
}
return $out;
}

$serendipity['smarty']->register_function('my_menu', 'my_menu');
Then you can edit your index.tpl template file and just insert {my_menu} at any place where you later want to have your menu. Of course you need to adjust your PHP function so that it reads an XML file, or whatever else. :)

HTH and 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/
Ctwizzy
Regular
Posts: 19
Joined: Thu Sep 07, 2006 10:41 pm

Post by Ctwizzy »

Thanks for the info.

Im not exactly sure how the event plug-ins work, but can they insert code anywhere on the page or just act as listeners?

Ideally I would like to make a plugin where when installing the plug-in via serendipity's admin page you then specify the links and titles of each navigation menu. Though I dont think this will be possible as then it would be increasingly harder to have the plug-in put your menu exactly where you want it (i.e. if you wanted it in the banner or below it etc etc).

It does seem like manually editing the tpl files is nessesary which is no big deal. Ill just create a serendipity_navbar.tpl, have the index.tpl insert that and have the navbar.tpl read the values in from a custom smarty function which of course reads in the info from a xml file, or just manually. Would there be a way of somehow tying in a plugin so that the xml file could be changed like my ideal solution above? It doesnt seem out of the question and maybe ill try it this weekend when I have some time, damn school lol.
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post by garvinhicking »

Hi!
Im not exactly sure how the event plug-ins work, but can they insert code anywhere on the page or just act as listeners?
They act as listeners. They can only output code where asked for using the serendipity_plugin_api::hook_event() or using the smarty {serendipity_hookPlugin} call.
Ideally I would like to make a plugin where when installing the plug-in via serendipity's admin page you then specify the links and titles of each navigation menu. Though I dont think this will be possible as then it would be increasingly harder to have the plug-in put your menu exactly where you want it (i.e. if you wanted it in the banner or below it etc etc).
Yes, this sounds like you want to use something like the Page Nugget plugin I spoke of. Have a look at other plugins like the downloadmanager, RSS aggregator or todolist plugins on how they offer custom admin interface.
It does seem like manually editing the tpl files is nessesary which is no big deal.
When you go the plugin route you would not need to edit the .tpl file!
Ill just create a serendipity_navbar.tpl, have the index.tpl insert that and have the navbar.tpl read the values in from a custom smarty function which of course reads in the info from a xml file, or just manually. Would there be a way of somehow tying in a plugin so that the xml file could be changed like my ideal solution above? It doesnt seem out of the question and maybe ill try it this weekend when I have some time, damn school lol.
This would also work, this would be the config.inc.php method I spoke of. :)

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/
Ctwizzy
Regular
Posts: 19
Joined: Thu Sep 07, 2006 10:41 pm

Post by Ctwizzy »

Hey Garvin, thanks for your help thus far.

Edit: Solved most of my own questions lol.

I was going to take a look at the html nugget you mentioned to see how plugins are done, but the link is down so I think ill start with this method as truth be told it seems simpler.

While were on the subject though and if you have the time I need a few things in the code clarifying. I am by no means a good programmer, I understand the fundamentals thats about it.

Code: Select all

<?php
	function my_menu($params, &$smarty) {
What do the two parameters pass in?

Code: Select all

		return $out;
	}
$serendipity['smarty']->register_function('my_menu', 'my_menu');
?>
When you are registering the function whats with 'my_menu' being listed twice?

To my understanding: config.inc.php is like a methods file, I could write hundreds of methods in there, and as long as I add the register line I can call on them. This to me is like a module list. So why does it have to be called config.inc.php why cant I call it functions.php and then just write a php include statement in index.php, albeit a lot of functions would slow down the site.

In what file do you have it specified to include the config.inc.php I looked at a few files but couldnt find any mention of it.

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

Post by garvinhicking »

Hi!
Ctwizzy wrote:Hey Garvin, thanks for your help thus far.

You're welcome. :)

Code: Select all

<?php
	function my_menu($params, &$smarty) {
What do the two parameters pass in?
Those are default variables that each smarty function needs to declare to work in the smarty environment. $params is an array of parameters; if you call the smarty function {my_menu param1=X param2=X} your $params array will hold:

array(
'param1' => 'X',
'param2' => 'X'
)

The &$smarty variable is simply a reference to the main smarty object; you might need that to call $smarty->assign('xxx') etc.

Code: Select all

		return $out;
	}
$serendipity['smarty']->register_function('my_menu', 'my_menu');
?>
When you are registering the function whats with 'my_menu' being listed twice?
The first value in the register_function is the name of the targeted smarty function name, the second is the actual PHP function name. So you could make the smarty function {my_smarty_menu_function} actually call {my_menu}.
To my understanding: config.inc.php is like a methods file, I could write hundreds of methods in there, and as long as I add the register line I can call on them. This to me is like a module list. So why does it have to be called config.inc.php why cant I call it functions.php and then just write a php include statement in index.php, albeit a lot of functions would slow down the site.
Right, the template's config.inc.php file is a means to enhance your template functions or assign custom variables to serendipity. IT is called config.inc.php because it touches the configuration of the templates. We just had to use a fixed name, and someone (probably me) decided that 'config.inc.php' sounded good.

Since it cannot be included in index.php but in include/functions_smarty.inc.php you can't rename it and call it yourself. You would need to patch the serendipity core then - and what we developers try to take very serously is that people should NEVER need to touch serendipity core files, for sakes of easy upgrading to future serendipity versions. I would hate to need to patch s9y core files every time I upgraded; thus falling back on conventions and APIs is the way to go.

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/
Post Reply