Filtering Tricks: Highlight the Current URI
- Mason has a feature called "component calls with content".
- This is a component call that has access to part of the caller's output.
- Looks like this:
<&| /lib/highlight_current_link.mas &>
<a href="/">Home</a>
<a href="/news/">News</a>
<a href="/events/">Events</a>
</&>
- So we want to highlight the section we're currently in.
- The /lib/highlight_current_link.mas component:
<%init>
# remember that $r global?
my $uri = $r->uri;
# Strip off any filename in the path
$uri =~ s{[^/]+$}{};
# Retrieve our caller's output.
my $content = $m->content;
# Find the matching URI in the content,
# and replace it with bold tags
s{<a href="$uri">([^<]+)</a>}
{<b>$1</b>};
# output the altered content
$m->print($content);
</%init>