0

On my webpage I'd like to have a global search box i.e. search box which is available on every subpage (let's say in header of web page like on SO). To do this, I am putting search form in "base" template which is used for every subpage and it works fine. But my problems start when I want to show page which displays search results. On such page I'd like to achieve one of:

  1. Have a query string value appear in this global search box.
  2. Hide global search box (so I can put more advanced one in result page itself).

To do the first thing, I would have to provide query string to base template. It means, I will have to add new parameter for base templete so every view will also need such parameter (which is empty string most of the time). Am I right? Is there simpler way to do this?

For the second thing I would have to detect current URL and check if this is search result page. It seems that to achieve this, I would have to add request parameter to base template (which basically means it's the same problem as with providing query string).

Is there simpler approach to do this?

By the way, I am using java.

2 Answers 2

1
  • First you can use optionall parameter in your base layout just like

    @(title: String, query: String = null)
    

    So you can use it in results view only like:

    @myBaseLayout(title="Search results", query=query) 
    
  • For hiding searchbox on search results you can use Scala condition OR javascript, no magick here
  • You can just use separate layout for search results
1

Your question is in the same spirit as what is currently the top-voted playframework-2.0 question on this site. I'd recommend checking out the accepted answer to the question, particularly the section about the HTTP Context. Using the args map in the context will allow you to implement either behaviour:

Have a query string value appear in this global search box.

Have the actions that handle search requests put the search term into the context map under a specific key, and then check for this key in the search box markup.

Hide global search box (so I can put more advanced one in result page itself).

Have the actions that handle search requests put a boolean value into the context map under a specific key, and then conditionally render your search box markup depending on the existence/value of this key.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.