Jump to content

Code optimization for 'complex' menu


maba
 Share

Recommended Posts

Hi All,

with this I start to create this menu where:

  • not show all root children
  • show all company children
  • show products following sectors/applications way
  • show new label for the newest products (see Products->Water->Derivation)
  • (I would) not show applications without products (see Products->Gas->Derivation)
  • show contacts
  • (todo) show galleries (more complex than products, based on category, static sample now)
  • (todo) show blog children
  • (done) multi language

In attach my pages structure.

My code: https://gist.github.com/4348501

Now: how to clear this code? How DRY it (e.g. $class declaration)? How optimize it?

Thanks,

Marco

post-846-0-58566300-1356034291_thumb.png

Link to comment
Share on other sites

  • 2 weeks later...

Hi everybody..

Soma, I've some problems with the module.

Ok, I can delete my Products page (and children) but how I can inject my "edited" Products menu?

Can you post an example?

Thanks!

Marco

Link to comment
Share on other sites

Sorry to not get back any sooner.

What do you mean to clean up the code and make it dry? It actually looks good and clean. I don't think you can easily reduce or simplify with the special case of menu you got.

I havent looked closely at your code before when i posted that. I dont see an easy way to use my module to create this sort of navigation. only partial at best. Sorry to confuse you.

I think nobody really poped in to help you because it is already good and people dont like to clean up or code it for you. I guess you know why.

Ive done it occasionally and only because ive thought the requester has no clue.

So you come with a big list of how it should work and it looks as you want it to someone else make it for you. That most likely wont work here.

If you post a specific question of something you cant get to work Im sure somebody will try to help.

Again your code looks fine to me and you get the most out of it with simple foreachs. I don't think theres much to dry here.

Link to comment
Share on other sites

Soma, thanks for the answer. :)

What do you mean to clean up the code and make it dry? It actually looks good and clean. I don't think you can easily reduce or simplify with the special case of menu you got.

Is enough.

My fear is to don't use all the features that PW give us.

When you said that I can use MarkupSimpleNavigation I thinked that probably I don't understand its potential. :-[ I search far and wide in the forum but I don't found (module) examples that can help me.

  • (I would) not show applications without products (see Products->Gas->Derivation)
  • (todo) show galleries (more complex than products, based on category, static sample now)

These are the open points.. other things is already done with the posted code.

I've some problems with the first, I'll try to fix it. ???

So you come with a big list of how it should work and it looks as you want it to someone else make it for you. That most likely wont work here.

Sorry, isn't my goal.

Again.. thanks. ;)

Marco

Link to comment
Share on other sites

Fact is your code is perfectly fine for what it archives. You'll not find any modules that could help you doing it, at best the MarkupSimpleNavigation. It is well for quickly rendering straight nested navigations. You can overwrite with you own templates and selector (global) etc, as the manual shows. However it is not suited for things like you do with products and categories as it just traverses the page tree or parts of it. For more complex navigation the PW API and the hierarchical structure makes it very easy to construct them the way you did. I wouldn't start putting it to pieces with abstraction and functions for this menu.

You problem with navigation not showing when no products found would be easy with moving the output after iterating the products and test if they're linked. Then echo only if any found. For example:

foreach($applications as $a) {
   $prod_str = '';
   foreach($products as $p) {
       if(!$p->applications->has($a)) continue;
       if(!$p->sectors->has($s)) continue;
       $span = $p->new == 1 ? " <span class='orange sub-menu-title'>" . $this->_('novita') . "</span>" : "";
       $prod_str .= "<li><a href='$p->url'>$p->title$span</a></li>";
   }
   if($prod_str){
       echo "<li><a href='$a->url'>$a->title</a><ul class='sub-menu'>$prod_str</ul></li>";
   }
}
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...