0

Can I execute that line of code

nav = request().path().toString()

inside of scala template like index.scala.html

I would like to have that code to check on witch side is user and to mark it on menu using code like this in main.scala.html:

                        <li class="@("active".when(nav == "contact"))">
                        <a href="">Contacts</a>
                    </li>

2 Answers 2

1

I would recommend you different approach, create tag - resuable template, which takes Integer as an argument, it will render menu and mark as an active different menuitem depends on value.

@(menuItem: Int)    
<ul >
           <li  @if(menuItem==1){ class="active" } >
               ////
           </li>
           <li @if(menuItem==2){ class="active" }>               
           </li>
           <li @if(menuItem==3){ class="active" }>               
               ///
           </li>        
</ul>

from your contact page and any other page, call this tag with corresponding value, @views.html.tags.menu(1)

Sign up to request clarification or add additional context in comments.

2 Comments

Why an int? If you use ints than you have to keep a table in your head which int corresponds to which menu item. Thats not a good practice imho. Also if you have several menuitems I would use match and not many ifs. But I agree that a reusable Tag is good...
It can be any value, base on this value corresponding menu item will be activated, you could use pattern matching as well. My point is that you should use reusable template which would render menu, and invoke it from different pages (like contact).
1

You can define variables like that if that is your question. If it is not your question than please try to explain your problem in more detail.

@nav = { @request().path().toString() }

2 Comments

Hey. thanks. it Helps me with setting up a variable @nav this code [code]@nav = { @request().path().toString() }[/code] works. But now I would like to use it in "when statment" in code as follow [code] <li class="@("active".when(nav == "/pomoc"))"> [/code] But it dosent work. When I use variable with '@' char I am getting error: " illegal start of simple expression " Please help
Have your printed request().path().toString to see if the string corresponds to your expectation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.