Play! helper to include external html page contents
Sometimes it could be useful to include a part of page from remote site. In this case this snippet can help you.
It is not a best solution, while it blocks the main thread for time it needs to get a response. But it can be improved using asynchronous invocation from page via some REST api or by using a cache in helper function.
Include.scala helper
import play.api.templates.Html
import play.api.libs.ws.WS
import scala.concurrent.Await
import scala.concurrent.duration._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
object Include {
def apply(path: String): Html = {
val future = WS.url(path).get()
val response = Await.result(future, 20 seconds).body
Html(response)
}
}
And it’s usage :
@helper.Include("http://www.myurl.com/header")