Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8300081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:25:10+00:00 2026-06-08T16:25:10+00:00

Could someone give an example of highlighting the active link on the menu on

  • 0

Could someone give an example of highlighting the “active” link on the menu on the site with Snap? Or at least tell me how you would go about it – as I have no idea.

In other web-frameworks I usually set a context variable called active to what the active page should be, and then my html simply checks it:

<ul> 
   {% ifequal active "home" %}
   <li class="active">
   {% else %}
   <li>
   {% endifqual %}
   <a href="/">Home</a>
   </li>

   {% ifequal active "about" %}
   <li class="active">
   {% else %}
   <li>
   {% endifequal %}
   <a href="/about">About Us</a>
   </li>

 </ul>

There are splices in heist, but I am not sure how you would use them to figure out what the current url is or set the context variable.

My Solution

Thanks to @mightybyte and @Adam Bergmark, I’ve settled on the following:

Haskell code:

menuenuEntrySplice :: MonadSnap m => HeistT m Template
menuEntrySplice = do
               requestPath <- lift $ withRequest (return . rqURI)
               node <- getParamNode
               let setActive n = if getAttribute "href" node == Just (decodeUtf8 requestPath)
                                    then setAttribute "class" "active" n
                                    else n


               let aNode = Element "a" [("href", fromMaybe "/" $ getAttribute "href" node)] $ [TextNode (nodeText node)]
               let liNode = setActive $ Element "li" [] [aNode]

               return [liNode]


app :: SnapletInit App App
app = makeSnaplet "app" "An snaplet example application." Nothing $ do
    ....
    addSplices [ ("menuEntry", liftHeist menuEntrySplice) ]
    return $ App h s a

And here is now it’s used in HTML:

<ul class="nav">
      <menuEntry href="/">Home</menuEntry>
          <menuEntry href="/contact">Contact</menuEntry>
</ul>

which produces:

<ul class="nav">
     <li class="active"> <a href="/">Home</a> </li>
     <li> <a href="/contact">Contact</a> </li>
</ul>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:25:12+00:00Added an answer on June 8, 2026 at 4:25 pm

    In the snapframework.com site, we do it with javascript. At the end of site.js you’ll find this code that adds the appropriate class to the appropriate link.

    if ($.url.segment(0)) {
      $('.nav li.'+$.url.segment(0)).addClass('active');
    } else {
      $('.nav .home').addClass('active');
    }
    

    If you want to do this with Heist, then you’ll need to use a paradigm a bit different from what your template above uses. The whole point of Heist (aided by Haskell’s strong static type system) is to provide the strongest possible separation between view and business logic, so you can’t use Haskell constructs like loops or conditionals directly in your templates. The answer is to create new tag (implemented with a splice) that does exactly what you want. In your template you will use it like this:

    <ul>
      <menuLink href="/">Home</menuLink>
      <menuLink href="/about">About Us</menuLink>
    </ul>
    

    First of all, notice how much cleaner this is. In your approach, you had to repeat the same code for every link. Here we’re able to keep the template DRY and it reads quite nicely.

    To implement the menuLink tag you’ll need a splice splice that looks something like this:

    import Control.Monad.Trans.Class (lift) -- transformers 
    import Data.Text.Encoding (decodeUtf8)
    import Text.XmlHtml (getAttribute, setAttribute, elementTag)
    
    menuLinkSplice :: MonadSnap m => HeistT m Template
    menuLinkSplice = do
        requestPath <- lift $ withRequest (return . rqURI)
        node <- getParamNode
        let addActive n = if getAttribute "href" n == Just (decodeUtf8 requestPath)
                               then setAttribute "class" "active" n
                               else n
        return [addActive (node { elementTag = "a" })]
    

    Then, to put it all together, you need to bind that splice to the menuLink tag. In a Snap application you’ll do that with:

    addSplices [ ("menuLink", liftHeist menuLinkSplice) ]
    

    You also might find my blog post Looping and Control Flow in Heist helpful (as well as some of my other posts with the “heist” tag).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please give me a example of a function that would use this
could someone give an example of the meaning of this word? i read about
Could someone give me an example of a .htaccess that would remove www from
Could someone give me an example of how to use the ContactPicker.PickSingleContactAsync() functionality correctly
Could someone please give me a link on how to create a query in
Could someone give me an example of how to I can remove [QUOTE=author;3095231] Author
Could someone give me an example of Duck Typing inheritance in Javascript? I'm exploring
Could someone give an example of how to do country-level geolocation in JavaScript, using
Could someone please give me an example on how can I convert a byte[]
Could someone give me an example of the best way to add an iterator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.