How to import re-usable blocks from another html file? (Scala)

1,098 views
Skip to first unread message

Drew Hamlett

unread,
Oct 24, 2011, 6:08:08 PM10/24/11
to play-framework
Basically I would like to have an html file with multiple re-usable
blocks such as this one:

@display(product:models.Product) = {
@product.name ($@product.price)
}

@display2(test:String) = {
<h1>@test</h1>
}
I can see how I can import an entire html file such as this:
@views.Application.html.states(customer). But I want to call the
blocks within that file. Such as
@views.Application.html.states.display(product) or
@views.Application.html.states.display2("Hello")

Thanks for any help.

Ahmed Mahmoud

unread,
Oct 25, 2011, 2:36:48 AM10/25/11
to play-fr...@googlegroups.com
you can use tags

http://www.playframework.org/documentation/1.2.3/templates#tags

or you can use the #{include /} to include a full html page =D   http://www.playframework.org/documentation/1.2.3/tags#include




--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.




--

Ahmed Mahmoud Mohammed Abd El-wahab 
el shaheer b Ahmed Fat7y.

Drew Hamlett

unread,
Oct 25, 2011, 8:11:50 AM10/25/11
to play-framework
Hi. I'm talking about Scala not the Groovy templates. In scala you
can write re-usable blocks with an html page and call them. I want to
fill an html page with these re-usable blocks and call them from
another page.

On Oct 25, 2:36 am, Ahmed Mahmoud <thediamondknig...@gmail.com> wrote:
> you can use tags
>
> http://www.playframework.org/documentation/1.2.3/templates#tags
>
> or you can use the #{include /} to include a full html page =Dhttp://www.playframework.org/documentation/1.2.3/tags#include
>
>
>
>
>
>
>
>
>
> On Tue, Oct 25, 2011 at 12:08 AM, Drew Hamlett <drewhj...@gmail.com> wrote:
> > Basically I would like to have an html file with multiple re-usable
> > blocks such as this one:
>
> > @display(product:models.Product) = {
> >    @product.name ($...@product.price)

Drew Hamlett

unread,
Oct 26, 2011, 2:01:45 PM10/26/11
to play-framework
Anyone?

luiza...@gmail.com

unread,
Oct 26, 2011, 2:05:02 PM10/26/11
to play-fr...@googlegroups.com

Matt Hildebrand

unread,
Oct 26, 2011, 3:21:43 PM10/26/11
to play-fr...@googlegroups.com
I ran "javap" on the bytecode generated from such a template, and no symbols for the included blocks are visible.  In other words, the generated template classes do not expose individual blocks within a template file.  So it appears there is no way to do what you describe.

Example template:

@()

@block1(text: String) = @{
    "1" + text
}

@block2(text: String) = {
    Block 2 says [@text]
}

Generated template class:

Compiled from "views.Application.html.index.scala"
public final class views.Application.html.index extends java.lang.Object{
    public static final play.templates.Html apply(java.lang.String);
    public static final scala.collection.Iterator productElements();
    public static final scala.collection.Iterator productIterator();
    public static final boolean canEqual(java.lang.Object);
    public static final java.lang.Object productElement(int);
    public static final int productArity();
    public static final java.lang.String productPrefix();
    public static final play.templates.Format copy$default$1();
    public static final play.templates.BaseScalaTemplate copy(play.templates.Format);
    public static final play.templates.Appendable _display_(java.lang.Object);
    public static final play.templates.Format format();
}

No "block1" or "block2" in sight.

Another obstacle is that the syntax for invoking a template specifies the template file name but not a method/block within the specified file.

Is there a compelling reason not to put each block in its own template file?

-Matt

Matt Hildebrand

unread,
Oct 26, 2011, 4:40:42 PM10/26/11
to play-fr...@googlegroups.com
One footnote:  Methods for the named blocks do exist, but they are private, so getting at them would require some bytecode/classloader trickery.

Alternatively, a simple change to the scala module could make these methods public.  They could then be accessed freely, just like you envisioned.  Untested example:  @views.Application.html.include.block1("foo")

Compiled from "views.Application.html.include.scala"
public final class views.Application.html.include$ extends play.templates.BaseScalaTemplate implements scala.ScalaObject{
    public static final views.Application.html.include$ MODULE$;
    public static {};
    public play.templates.Html apply();
    private final play.templates.Html block2$1(java.lang.String);
    private final java.lang.String block1$1(java.lang.String);
    private views.Application.html.include$();
}
Reply all
Reply to author
Forward
0 new messages