We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

if-clause in link_to

Good morning,

I'm using the latest AdminLTE version (3.0). For sidebars, it uses the <nav> tags, like this:

<nav class="mt-2">
    <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
        <li class="nav-item">
            <a href="/dashboard" class="nav-link{% if active_dashboard is defined %} active{% endif %}">
                <i class="nav-icon fas fa-tachometer-alt"></i>
                <p>Dashboard</p>
            </a>
        </li>

As you can see, I'm using the active_dashboard variable to set the active tag on the link (to highlight it). Problem is, that I need to use link_to, since the live server has another baseURI and I can't just use my absolute paths.

Now I wanted to come up with something like this:

 <li class="nav-item">
  {{ link_to('dashboard', '<i class="nav-icon fas fa-tachometer-alt"></i><p>Dashboard</p>', 'class': 'nav-link{% if active_dashboard is defined %} active{% endif %}' )}}
</li>

But the if-clause won't get parsed, it just gets printed out.

So, question: How can I get this if-clause to get executed inside the link_to?

Thank you.



77.7k
Accepted
answer
{{ link_to('dashboard', '<i class="nav-icon fas fa-tachometer-alt"></i><p>Dashboard</p>', 'class': 'nav-link' ~ (active_dashboard is defined ? ' active': '') )}}

I might've made a typo, but the basic idea is that you are delaing with regular strings here, not template blocks.

(~ is the string concatenation for volt)



4.0k
{{ link_to('dashboard', '<i class="nav-icon fas fa-tachometer-alt"></i><p>Dashboard</p>', 'class': 'nav-link' ~ (active_dashboard is defined ? ' active': '') )}}

I might've made a typo, but the basic idea is that you are delaing with regular strings here, not template blocks.

(~ is the string concatenation for volt)

Thank you! Actually tried it with ~, but still had the {% if ... %} in it, but this works like a charm.