Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Menu, regular expression, templates, and javascript and href

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
devnul
Smarty Rookie


Joined: 26 Aug 2003
Posts: 32

PostPosted: Thu Aug 28, 2003 7:57 am    Post subject: Menu, regular expression, templates, and javascript and href Reply with quote

Hello,

I have a template (call it "menu_bar.tpl"), which is basically a table with
Code:
<a href="blah.php"><img scr="blah_base.gif" onmouseover="rollover('blah','blah_over.gif');" onmouseout="rollover('blah','blah_base.gif');" name="blah"></a>
in various cells...

.. hopefully you get the idea.. Basicallly I want the menu buttons to rollover when the mouse is over them.. I got this working fine...

But, now I want it so that when a user navigates to one of these pages the menu item stays "sticky" (it is in a "rolled over" state when selected)... I thought about this at length and decided I wanted to keep the "menu_bar.tpl" as simple as possible - currently there is not a single line of PHP or Smarty tags inside it and I want to keep it that way...

I also, personally, am very ideological about the use of Javascript (I only use it to enhance a design - in my opinion a site should still be presentable and 100% functional without javascript enabled).. To that end, using javascript to determine if a button should be "sticky" or not is not something I want to do...

I also do not want to have to design a seperate menu bar for each page... If I want to add a new menu item I want it to be painless and instantly available to all the pages of the site....

So, since the menu bar is a small element I thought about doing something like this on pages which use it:

Code:

function toggle_menu($tpl_output,&$smarty)
{
        $php_self=$_SERVER['PHP_SELF'];

        $php_self=preg_replace('/\//','\\\/',$php_self);
        $ophp_self=$php_self;

        //Prepare yourself by drinking a bottle of Wild Turkey [tm]
        $tpl_output = preg_replace('/
<a(.*?)href="'.$php_self.'"(.*?)>(.*?)<img(.*?)src="(.*?)_base\.(.*?)"(.*
?)>(.*?)<\/a>/','$3<img$4src="$5_over.$6"$7>',$tpl_output);

        //Now take at least two Tylenol [tm] (or whatever makes you feel good)
        return $tpl_output; 
       
}

//Toggle the currently selected pages appropriate menu item
$smarty->register_outputfilter("toggle_menu");
$menu=$smarty->fetch('menu_bar.tpl');
$smarty->unregister_outputfilter("toggle_menu");

//Assign a variable $menu which can then be incorporated in other smarty templates by the {$menu} method
$smarty->assign_by_ref('menu',$menu);



..... of course currently my regular expression does not handle tags which span multiple lines and is case-insensitive (I'm not exactly sure how to do that in PHP, but I haven't researched it either)... But, I digress... My question is: is this a sensible way of doing this?... I tried to write a regular expression which would work with almost any type of HTML code which has a <a><img></a> structure...

Is there a better way of doing what I am trying to do here?... Has this been tried before?.. I note that most of the Smarty sites I visit do not have rollover buttons... I'm not sure if these were design constraints, ideological complaints, or representative of the fact that templating schemes can't handle this very well...

What do ya'all think?... Is this nutty to even attempt?... If not, can it be made better?..

Thanks for all yer help!

- Greg


Last edited by devnul on Thu Aug 28, 2003 9:09 am; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Thu Aug 28, 2003 8:36 am    Post subject: Reply with quote

hey devnul:

Interesting idea, though in my mind, the implemention is at the wrong point. I guess I'm not a big fan of rewriting output where there are clear ways of getting the proper content written in the first place.

Your desire not to touch the menu source is a bit limiting, but it occurs to me that you can implement your toggle_menu functionality as a plugin fucntion. Then instead of {include file="menu.tpl"}, you would do something like {include_menu file="menu.tpl"}. Same functionality and it leaves it to the designer where the sticky funcitonality is applied. Or you can implement it inside a block function (say {sticky_menu} .. {/sticky_menu}) which you would wrap around the menu code (yes, introduces some Smarty into the template, but then keeps related features together).

Still, I would think that the ideal solution would be to create some sort of custom link plugin function that automatically generates the required code for each link instead of having to manually produce the onmouseover (and other stuff) code for each link.

2c.
Back to top
View user's profile Send private message
devnul
Smarty Rookie


Joined: 26 Aug 2003
Posts: 32

PostPosted: Thu Aug 28, 2003 9:08 am    Post subject: Reply with quote

Yes I see your point - I did forget to mention one other point though that might change your view... I use GoLive as my primary HTML design tool (just got tired of coding everything in vi and GoLive actually writes pretty solid HTML).. If I implement it as a plugin then I can't visually see what is going on... Seeing what is going on is very important because of the integration of GoLive and Photoshop/Illustrator... With the combination of these I can design a button one time and simply assign variables to it within GoLive... if I ever update the look of the button all my menu bars get the new look without any effort...

Also, I don't use the { include } method.. I actually have reconfigured the delimiters and have them just do <!--{$menu}--> and the class actually does a "fetch" on the template then assign_ref's it to menu... It's interesting, everytime I got ready to write Smarty code {if}/etc in the HTML I stepped back from it a few feet and thought of a way to put that logic in the class.. Usually I'd end up with an extra HTML file to generate, but it's worked out fine.. As an example, I have one menu bar which is displayed when the user is logged in and another when they are not.. So yeah there are two html files to edit/maintain, but it has allowed me to truly look at each issue (design/business logic) completely seperately...

I do realize that writing a template with {if}/etc tags in it would be pretty straightforward and that any designer could just {include} it where they wanted it.... But, what I am after here is a method which would allow the designer to rig up whatever kind of menu scheme/design they want and still give them the sticky functionality.... For a Smarty/PHP person this is pretty straightforward, for a graphics artist turned web designer it's a different matter entirely.... It seems like doing it this way the possibility of a designer breaking it is minimized (as long as there is a "_over" and a "_base" graphic the code should always work -at least I'm hoping that will be the case)...

The issue, of course, is by using a plugin/registered function (I reckon they both do about the same thing) is that I can't see what it actually is going to look like... Neither could my customers.. I like the idea of being able to tell my customers "You want a new menu bar.. No problem.. Just design it and upload it.."... I may have them put some extra attribute on it like '<img src="blah_base.gif" over="blah_over.gif" sticky="yes">', but I still can't decide if this whole thing is even a good idea... I flip-flop on it about every 5 minutes it seems like...

Does that change your thoughts on this (I.E. being able to visually design the HTML)... Or are you still thinking that a different approach is warranted here?..

I also did a performance test, something like:

Code:

//Start timing it
$time_start=getmicrotime();

$smarty->register_outputfilter("toggle_menu");
$menu=$smarty->fetch($menu_bar);
$smarty->unregister_outputfilter("toggle_menu");

//Stop timing it
$time_end=getmicrotime();
$time=$time_end-$time_start;
echo "HIT: $time seconds<br>";


And even though my FreeBSD system here is quite slow (300mhz), it floats between .015 and .016 seconds of overhead....

- Greg
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP