We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7231
    • 4,205 Posts
    I like to use semi-static menus for 1) small sites, 2) sites where I don’t want the client to mess with the menu, and 3) when I want to simulate that I am in one container but am in another.

    I use a simple snippet (there may be others that do this) that outputs an array of document IDs as a list and has a simple "active" state. It also has a way to force an active state if you want to simulate that you are in one section while in another (used for multi language sites). The cool thing is that it does not need to follow the doc tree.

    I am sure it is crude but works for me so far but is limited to one level (I don’t know how to work with levels, leave that to wayfinder). I call it semi static since it gets the title dynamically from the documents using GetField snippet (I am lazy, don’t really need the snippet since the date can be easily retrieved using the api but at the time I did not know how and with GetField I can easily reach TVs as well with the same code):
    <?php
    // Build simple list menu based on array of doc ids
    // Note: must have GetField snippet installed
    // sample call: [[SimpleMenu? &docIds`1,2,3` &title=`longtitle`]]
    // sample call with active ID: [[SimpleMenu? &docIds`1,2,3` &activeId=`2` &title=`longtitle`]]
    
    $docIds = isset($docIds) ? $docIds : '';
    $docId = explode(',', $docIds);
    $activeId = isset($activeId) ? $activeId : $modx->documentIdentifier;
    $title = isset($title) ? $title : 'pagetitle';
    $output = '';
    
    $output = '<ul>';
    
    foreach ($docId as $z) {
    	$y = $modx->runSnippet("GetField", array("docid" => $z, "field" => $title ));
    	if($z == $activeId) {
    		$output .= "<li class='active'><a href='[~$z~]' title='$y'>$y</a></li>";
    	} else {
    		$output .= "<li><a href='[~$z~]' title='$y'>$y</a></li>\n";
    	}	
    }
    
    $output .= '</ul>';
    
    echo $output;
    
    ?>
      [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

      Something is happening here, but you don&#39;t know what it is.
      Do you, Mr. Jones? - [bob dylan]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      This would come in handy for simple static menus like the usual "privacy - TOS - site map" type of menus you see in a lot of footers, and sometimes something similar at the top of the page.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 7231
        • 4,205 Posts
        I think so also, maybe we can clean this up (add some customization features, classes etc...) and add it to the repo.
          [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

          Something is happening here, but you don&#39;t know what it is.
          Do you, Mr. Jones? - [bob dylan]
          • 3749
          • 24,544 Posts
          Quote from: ProWebscape at Feb 07, 2008, 01:26 AM

          wow thats great! It still wouldnt answer the ’active’ menu issue I dont think.

          I dug it up and, it turns out that I misremembered. I did in a previous life (before MODx).

          Here’s the menu code that would go in a MODx snippet. I’ve MODx-ified it but haven’t tested it:

          <?php
          
          $output = "";
          $output .= '<div id="myMenu"><ul>';
          $output .= '<li><a ';
          if ($modx->documentObject['menutitle']  == "home") {
          $output .= 'class="current" ';
          }
          $output .= 'href="index.htm">Home</a></li>';
          
          $output .= '<li><a ';
          if ($modx->documentObject['menutitle']  == "about") {
          $output .= 'class="current" ';
          }
          $output .= 'href="about.htm">About Us</a></li>';
          
          $output .= '<li><a ';
          if ($modx->documentObject['menutitle']  == "services") {
          $output .= 'class="current" ';
          }
          $output .= 'href="services.htm">Services</a></li>';
          
          $output .= '<li><a ';
          if ($modx->documentObject['menutitle']  == "directions") {
          $output .= 'class="current" ';
          }
          $output .= 'href="directions.htm">Directions</a></li>';
          
          $output .= '<li><a ';
          if ($modx->documentObject['menutitle']  == "specials") {
          $output .= 'class="current" ';
          }
          $output .= 'href="specials.htm">Specials</a></li>';
          
          $output .= '<li><a ';
          if ($modx->documentObject['menutitle']  == "news") {
          $output .= 'class="current" ';
          }
          $output .= 'href="news.htm">News</a></li>';
          
          $output .= '<li><a ';
          if ($modx->documentObject['menutitle']  == "contact") {
          $output .= 'class="current" ';
          }
          $output .= 'href="contact.htm">Contact Us</a></li>';
          
          $output .= '</ul></div>';
          
          
          return $output;
          
          ?>

          Only the active page gets the "current" class in the <a> tag.

          There’s no need for a TV since you can check the menutitle directly (or title, description, or alias if you prefer). You might rather have the class setting in the <li> tag instead of the <a> tag.

          It would get kind of cumbersome if you had a complex, multi-level menu but would still work.

          Bob
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 7455
            • 2,204 Posts
            so you made this because wayfinder takes to much time?
            or maybe to save time in db loading?

              follow me on twitter: @dimmy01
              • 3749
              • 24,544 Posts
              Quote from: Dimmy at Feb 08, 2008, 08:06 AM

              so you made this because wayfinder takes to much time?
              or maybe to save time in db loading?

              I wouldn’t say "too much time" but in a completely static site, the code above might put less strain on the server and make for somewhat faster page loading. With the pages cached, though, I’m not sure how much you’d gain. Someone with more time on their hands could test it. wink

              I actually wrote the original code before I discovered MODx, so tWayfinder wasn’t an option for me then.

              Bob
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting