Page Id Variables Not Updating

Hey, I’m trying to have a conditional statement that adds an active class the the link and do this I check if the page id is a specific number for each link. This, however, is not working as it should because when I go to another page, the variable does not update and the link from the previous page remains active (the page changed although). Any help would be appreciated.

This grabs the page id

{assign var="pg_id" value=$smarty.request.page_id|default:$smarty.session.current_page_id}

And this is what checks the page id