Generate a PHP Navigation Bar

Using path placeholders in collect data actions, you can easily compile indices of files.

Let’s say you are developing a web site in PHP. You keep all the main PHP files in a separate folder. Now, you want to create a navigation bar in PHP. While you could do this by hand, automating this in PowerGREP will save you a lot of time, certainly if pages are frequently added and removed. Though I am using PHP in this example, the same principles apply to any other web scripting language.

The final PHP file should look like this, with the complete if statement repeated for every main page on the site:

function navbar($mainpage) {
  if ('currentpage' == $mainpage) {
    print '<B>currentpagetitle</B><BR>';
  } else {
    print '<A HREF="currentpage">currentpagetitle</A><BR>';
  }
}

Obviously a very simple navigation bar, but good enough to illustrate the idea.

  1. Select the files you want to add to the navigation bar in the File Selector.
  2. Start with a fresh action.
  3. Set the action type to “collect data”. Leave the search type as “regular expression”.
  4. In the Search box, enter the regular expression <title>(.*?)</title> and make sure to leave “case sensitive search” off. This regular expression matches an HTML title tag, capturing the title into the first backreference.
  5. In the Collect box, enter the following six lines of text.
      # \1
      if ('%FILENAME%' == $mainpage) {
        print '<B>\1</B><BR>';
      } else {
        print '<A HREF="%FILENAME%">\1</A><BR>';
      }
  6. Select “save results into a single file” in the target file creation list.
  7. Click the ellipsis (...) button next to “target file location”, and select the name of the file you want to save your PHP navigation bar into.
  8. Leave “between collected text” set to “line break”.
  9. Turn on the “collect headers and footers” checkbox.
  10. In the list that appears, click on “target file header”. In the edit box next to that list, paste <? function navbar($mainpage) { and press Enter to add a line break after the {.
  11. Select “target file footer” in the list and type in } ?>. These two steps make sure we collect a valid PHP file.
  12. Click the Collect button to run the action to create a complete navigation bar.

Two techniques make this action work. The regular expression that searches for the title tag captures its contents into a backreference \1 which we use to insert the title into the collected data three times. The path placeholder %FILENAME% inserts the name of the file being searched through into the collected data. This example assumes that all HTML files are in the same folder. If not, you’ll need to use another path placeholder. The finishing touch is to use the headers and footers option in PowerGREP to wrap our PHP code into a complete PHP function.

You can find this action in the PowerGREP5.pgl standard library as “Generate a PHP navigation bar”.

All that is left to do now is to include a reference to the navigation bar in each of the web pages, something you can also easily do with PowerGREP.