-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use html tag / content in a stream template #25
Comments
Let's start with the simpler case: Try the following in a streaming template:
|
The import works for the "not found" issue. however when I just use @footer() I get the content escaped
instead of
I can use
which is better. I also try an implicit converter from Html to HtmlStream without success
|
OK, glad to hear the import thing works. You may want to add those imports to The escaping issue is something that would be good to fix. I suspect your approach with implicits should work. Let me experiment a bit. |
Ah, actually, Scala won't bother to look for an implicit in this case. That's because the template gets compiled into a series of calls to a |
Unfortunately, I don't see any way to fix this without a change to Play's Twirl template compiler. The core of the issue is that templates get compiled into a sequence of calls to the def _display_(o: Any)(implicit m: Manifest[T]): T = {
o match {
case escaped if escaped != null && escaped.getClass == m.runtimeClass => escaped.asInstanceOf[T]
case () => format.empty
case None => format.empty
case Some(v) => _display_(v)
case xml: scala.xml.NodeSeq => format.raw(xml.toString())
case escapeds: immutable.Seq[_] => format.fill(escapeds.map(_display_))
case escapeds: TraversableOnce[_] => format.fill(escapeds.map(_display_).to[immutable.Seq])
case escapeds: Array[_] => format.fill(escapeds.view.map(_display_).to[immutable.Seq])
case string: String => format.escape(string)
case v if v != null => format.escape(v.toString)
case _ => format.empty
}
} If the type of the item in your template is I can't find a way to modify this behavior. Implicits won't do it because the compiler has no reason to look for them. I can't override the The closest I've been able to come is to override
Defining this at the top of every template is a chore, and you can't import such a method from another class, or you get a "reference to display is ambiguous" error. If anyone has any suggestions, please let me know. In the meantime, the workaround is to wrap calls to |
Thanks for the quick answer. It looks like we can't do really better than @HtmlStream.fromHtml(footer()) so I'll stick we it I try to reuse an html template (aka menu/footer) in a stream page and can't figure it out.
I always get
|
In a @main(...) {
<div>Some markup</div>
} Gets roughly compiled to: main(...)(format.raw("""<div>Some markup</div>""")) The There are two possibilities:
<!-- staticMarkupForMain.scala.html -->
<div>Some markup</div> <!-- index.scala.html -->
@main(...)(staticMarkupForMain()) |
Thanks for your response. I think I understand way better how template works now I try to reverse the process : define template in stream format and use it from html page
But it only display a toString version of the HtmlStream
With the complexity of mixing 2 types of templates, is going full stream template a good option ? Do you see any drawbacks ? |
A In other words, if the base template you're rendering is a // Controller
def index = Action {
Ok.chunked(views.stream.main(...))
} <!-- main.scala.stream -->
<div>
<!-- Including other streaming templates is easy: -->
@someStreamingTemplate()
<!-- Including HTML templates requires a little wrapping: -->
@HtmlStream.fromHtml(someHtmlTemplate())
</div> If the base template you're rendering is a // Controller
def index = Action {
Ok(views.html.main(...))
} <!-- main.scala.html -->
<div>
<!-- You CANNOT include a streaming template -->
<!-- This won't work: @someStreamingTemplate() -->
<!-- Including other HTML templates is easy: -->
someHtmlTemplate()
</div> Therefore, it's more flexible to use |
In my app I have pages that use BigPipe and pages that don't.
I don't want to duplicate basic template tag in both format. e.g. analytics.scala.stream / analytics.scala.html or footer.scala.stream / footer.scala.html
In a classic html template I use the standard syntaxe :
If I do the same in detail.scala.stream I get the following error :
I can use something like that
but it's a bit ugly and I have the feeling we can do better.
Next level to the question : I have a template with header/menu/footer. I want to use it for stream and html pages.
The above doesn't work for the same reason : not found: value main
If I try to use the following I get : expected start of definition
The text was updated successfully, but these errors were encountered: