Is it possible to use scalate with java controller of play?

156 views
Skip to first unread message

Freewind

unread,
Aug 6, 2010, 8:58:03 AM8/6/10
to Scalate
Now I'm using play-framework, and I found the play-scalate just worked
with play-scala module.

I've learned scala a little, but because of the bad support of scala
in Eclipse(compares to Java), it's a pain to use scala, so I decided
to use Java controllers.

But I love scaml, I hope there is a module to let us use scalate with
Java controllers. Is it possible?

James Strachan

unread,
Aug 6, 2010, 9:00:33 AM8/6/10
to sca...@googlegroups.com

I don't know enough about play-scala and play to be sure; what happens
if you try use play-scalate with Java controllers?

--
James
-------
http://macstrac.blogspot.com/

Open Source Integration
http://fusesource.com/

Freewind

unread,
Aug 6, 2010, 11:09:15 AM8/6/10
to Scalate
In play, every controller extends a base controller provided by play
framework.

If we use java controller, we extends 'Controller'. If we want use
scalate, we need to extends 'ScalateController', which is provided by
a 'play-scalate' module(http://github.com/pk11/play-scalate), and is a
scala one.

If I use Java controller and extend the play-scalate
'ScalateController', I can't pass correct params to render
templates(which requires 'scala.Seq'), so I can't use it.

I thought that module developed by the author of scalate, and thought
it was impossible to let java to support scalate directly.

Please tell me if I can use scalate in plain java code? If is, I think
we can use scalate from java controller too.

On 8月6日, 下午9时00分, James Strachan <james.strac...@gmail.com> wrote:

James Strachan

unread,
Aug 6, 2010, 11:12:33 AM8/6/10
to sca...@googlegroups.com
2010/8/6 Freewind <nowi...@gmail.com>:

> In play, every controller extends a base controller provided by play
> framework.
>
> If we use java controller, we extends 'Controller'. If we want use
> scalate, we need to extends 'ScalateController', which is provided by
> a 'play-scalate' module(http://github.com/pk11/play-scalate), and is a
> scala one.
>
> If I use Java controller and extend the play-scalate
> 'ScalateController', I can't pass correct params to render
> templates(which requires 'scala.Seq'), so I can't use it.
>
> I thought that module developed by the author of scalate, and thought
> it was impossible to let java to support scalate directly.

I wonder if play-scalate should add a JavaScalateController to make it
easier to extend from Java code?

> Please tell me if I can use scalate in plain java code? If is, I think
> we can use scalate from java controller too.

There's no reason why you can't use Scalate from Java code. We might
want to make a Java API that hides some of its Scala-isms maybe.
Though a new JavaScalateController is maybe a good place for this?

Freewind

unread,
Aug 6, 2010, 11:21:57 AM8/6/10
to Scalate
Yes, I hope there is a "JavaScalateController"!

On 8月6日, 下午11时12分, James Strachan <james.strac...@gmail.com> wrote:
> 2010/8/6 Freewind <nowind...@gmail.com>:
>
> > In play, every controller extends a base controller provided by play
> > framework.
>
> > If we use java controller, we extends 'Controller'. If we want use
> > scalate, we need to extends 'ScalateController', which is provided by
> > a 'play-scalate' module(http://github.com/pk11/play-scalate), and is a
> > scala one.
>
> > If I use Java controller and extend the play-scalate
> > 'ScalateController', I can't pass correct params to render
> > templates(which requires 'scala.Seq'), so I can't use it.
>
> > I thought that module developed by the author of scalate, and thought
> > it was impossible to let java to support scalate directly.
>
> I wonder if play-scalate should add a JavaScalateController to make it
> easier to extend from Java code?
>
> > Please tell me if I can use scalate in plain java code? If is, I think
> > we can use scalate from java controller too.
>
> There's no reason why you can't use Scalate from Java code. We might
> want to make a Java API that hides some of its Scala-isms maybe.
> Though a new JavaScalateController is maybe a good place for this?
>
> --
> James

Freewind

unread,
Aug 6, 2010, 11:27:40 AM8/6/10
to Scalate
And, I have a suggestion.

When I use haml with ruby, I can invoke a lot of helper methods like
'link_to', 'link_to_remote', and which makes the template simple and
clear, very useful.

And I hope we can define some helper classes for the scaml template
too, that we can just invoke them in the template like:

=link_to_remote 'Click Me', :url=>'/xxx/xxx', :update=>'#chaged'

Although scalate has not provide such methods like 'link_to_remote',
but if it support helper classes, it will be easy to write such
methods ourselves.

On 8月6日, 下午11时12分, James Strachan <james.strac...@gmail.com> wrote:
> 2010/8/6 Freewind <nowind...@gmail.com>:
>
> > In play, every controller extends a base controller provided by play
> > framework.
>
> > If we use java controller, we extends 'Controller'. If we want use
> > scalate, we need to extends 'ScalateController', which is provided by
> > a 'play-scalate' module(http://github.com/pk11/play-scalate), and is a
> > scala one.
>
> > If I use Java controller and extend the play-scalate
> > 'ScalateController', I can't pass correct params to render
> > templates(which requires 'scala.Seq'), so I can't use it.
>
> > I thought that module developed by the author of scalate, and thought
> > it was impossible to let java to support scalate directly.
>
> I wonder if play-scalate should add a JavaScalateController to make it
> easier to extend from Java code?
>
> > Please tell me if I can use scalate in plain java code? If is, I think
> > we can use scalate from java controller too.
>
> There's no reason why you can't use Scalate from Java code. We might
> want to make a Java API that hides some of its Scala-isms maybe.
> Though a new JavaScalateController is maybe a good place for this?
>
> --
> James

Tim

unread,
Aug 6, 2010, 11:28:52 AM8/6/10
to sca...@googlegroups.com
Sry to OT your post slightly but ss there a way to use scaml outside of scalate? I want to use it with a webapp that I currently have built using stripes. It would be great if i could use scaml syntax and still use my stripes taglibs even.

I was planning on looking into it but if anyone knows a solution that is already in place that would be great.
Thanks :)

2010/8/6 Freewind <nowi...@gmail.com>

Hiram Chirino

unread,
Aug 6, 2010, 11:56:40 AM8/6/10
to sca...@googlegroups.com
Scalate can be integrated into any framework with a little bit of
work. Checkout the embedding guide, it will show you how:
http://scalate.fusesource.org/documentation/scalate-embedding-guide.html

2010/8/6 Tim <che...@gmail.com>:

--
Regards,
Hiram

Blog: http://hiramchirino.com

Open Source SOA
http://fusesource.com/

James Strachan

unread,
Aug 6, 2010, 12:03:49 PM8/6/10
to sca...@googlegroups.com
2010/8/6 Tim <che...@gmail.com>:

> Sry to OT your post slightly but ss there a way to use scaml outside of
> scalate? I want to use it with a webapp that I currently have built using
> stripes. It would be great if i could use scaml syntax and still use my
> stripes taglibs even.

You can use Scalate as a servlet for scaml / jade / ssp / mustache.

Integrating Scalate into JSPs so it can work with JSP tag libraries is
going to be harder; but there's no reason why you couldn't write a JSP
tag which evaluates its body using Scalate maybe

Tim

unread,
Aug 6, 2010, 12:08:16 PM8/6/10
to sca...@googlegroups.com
Maybe I'm not clear on what you mean Hiram. But if I do that wouldn't it try to interpret my taglibs as scaml markup?

2010/8/6 Hiram Chirino <hi...@hiramchirino.com>

Tim

unread,
Aug 6, 2010, 12:07:40 PM8/6/10
to sca...@googlegroups.com
It was more so that I could use the scaml syntax and still use stripes/jstl taglibs still. :P

James Strachan

unread,
Aug 6, 2010, 12:09:05 PM8/6/10
to sca...@googlegroups.com
On 6 August 2010 16:27, Freewind <nowi...@gmail.com> wrote:
> And, I have a suggestion.
>
> When I use haml with ruby, I can invoke a lot of helper methods like
> 'link_to', 'link_to_remote', and which makes the template simple and
> clear, very useful.
>
> And I hope we can define some helper classes for the scaml template
> too, that we can just invoke them in the template like:

You can do exactly the same thing in scaml too. Just use scala
expressions. In scaml thats "=" on the start of a line or {} in
attribute values or #{foo} inside text

%p
= someMethodCall(1, 2, 3)

%h1 Hello #{user.name}

%a{:href => {createSomeLinkUrl("hey")}, :title => "Some title"} My Link


etc

To expose functions to Scalate scripts (ssp/scaml) you can either just
import methods from companion objects, or import attribute methods or
create objects and import their methods (or use the objects directly)

-@ import val someAttribute: MyType

or create a helper object in your template...

- val helper = new SomeHelperObject
- import helper._

To avoid adding common imports to all templates you can set the
imports you want to add on the TemplateEngine globally if you wish.
Also if you put your templates in the sample package structure as your
code, you don't need to explicitly import the fully qualified class
name.

James Strachan

unread,
Aug 6, 2010, 12:10:22 PM8/6/10
to sca...@googlegroups.com
2010/8/6 Tim <che...@gmail.com>:

> Maybe I'm not clear on what you mean Hiram. But if I do that wouldn't it try
> to interpret my taglibs as scaml markup?

We should be able to include JSP fragments inside servlets (so they
can evaluate using JSP and evaluate whatever tag libraries are in
them) then process them or lay them out using Scalate.

Or vice versa.

James Strachan

unread,
Aug 6, 2010, 12:12:00 PM8/6/10
to sca...@googlegroups.com
On 6 August 2010 17:10, James Strachan <james.s...@gmail.com> wrote:
> 2010/8/6 Tim <che...@gmail.com>:
>> Maybe I'm not clear on what you mean Hiram. But if I do that wouldn't it try
>> to interpret my taglibs as scaml markup?
>
> We should be able to include JSP fragments inside servlets (so they
> can evaluate using JSP and evaluate whatever tag libraries are in
> them) then process them or lay them out using Scalate.

That didn't come out right - let me try again.

A Scalate template should be able to do a kind of server-side include,
evaluating an arbitrary servlet URI (passing parameters etc) so that
it can evaluate any servlet - such as a JSP page with some custom tags
on it - then the Scalate template can render/process the servlets
output.


> Or vice versa.

Or the JSP page can include the Scalate template using servlet includes

Freewind

unread,
Aug 6, 2010, 1:29:26 PM8/6/10
to Scalate
I just successfully rendered a .scaml file in java code, but it cost
nearly 8 seconds to load the file.

This is my java code:

public static void main(String[] args) {
TemplateEngine engine = new TemplateEngine(new Some<File>(null),
"production");
Template template = engine.load("test.scaml"); //-> cost 8 seconds
StringWriter buffer = new StringWriter();
DefaultRenderContext context = new DefaultRenderContext(engine, new
PrintWriter(buffer));
context.setAttribute("name", new Some<String>("Freewind"));
template.render(context);
System.out.println(buffer.toString());
}

This is a scaml file:

-@ var name:String
.main
=name

So I think we should precompile the .scaml files to .class files, and
let the java controllers invoke them directly. But how to do this? It
needs I know play-framework well, and I can't do it.

If the 'play-scalate' module provide a 'JavaScalateController' and
"precompile" command, that will be great.



On 8月7日, 上午12时12分, James Strachan <james.strac...@gmail.com> wrote:
> On 6 August 2010 17:10, James Strachan <james.strac...@gmail.com> wrote:
>
> > 2010/8/6 Tim <che...@gmail.com>:
> >> Maybe I'm not clear on what you mean Hiram. But if I do that wouldn't it try
> >> to interpret my taglibs as scaml markup?
>
> > We should be able to include JSP fragments inside servlets (so they
> > can evaluate using JSP and evaluate whatever tag libraries are in
> > them) then process them or lay them out using Scalate.
>
> That didn't come out right - let me try again.
>
> A Scalate template should be able to do a kind of server-side include,
> evaluating an arbitrary servlet URI (passing parameters etc) so that
> it can evaluate any servlet - such as a JSP page with some custom tags
> on it - then the Scalate template can render/process the servlets
> output.
>
> > Or vice versa.
>
> Or the JSP page can include the Scalate template using servlet includes
>
> --
> James

Tim

unread,
Aug 6, 2010, 1:35:54 PM8/6/10
to sca...@googlegroups.com
I'm using the maven-scalate-plugin to precompile when using scalate with scalatra and it works fine.

                <plugin>
                    <groupId>org.fusesource.scalate</groupId>
                    <artifactId>maven-scalate-plugin</artifactId>
                    <version>${scalate-version}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>precompile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

Maybe that can help you too?

Hiram Chirino

unread,
Aug 6, 2010, 2:52:48 PM8/6/10
to sca...@googlegroups.com
Hi,

The TemplateEngine instance is meant to be used like a application
wide singleton. It is thread safe and handles caching the scala
compiler instance and compiled templates so that subsequent compiles
are faster. The first page accessed is usually extra slow since it's
setting up the compiler. After that, subsequent compiles are faster
too. But like TIm said, if you precompile, the compiler is never even
booted up so that's even faster still, but then you loose the ability
to hot reload changes to the templates.

--

Freewind

unread,
Aug 7, 2010, 3:05:53 AM8/7/10
to Scalate
This will be helpful, but now the problem is to integrate with java
controller of play framework. I just get confirmation of the author of
play-scalate module, he said it's impossible to work with java
controller and scalate in play-framework. It's a pity

James Strachan

unread,
Aug 7, 2010, 3:28:31 AM8/7/10
to sca...@googlegroups.com
2010/8/7 Freewind <nowi...@gmail.com>:

> This will be helpful, but now the problem is to integrate with java
> controller of play framework. I just get confirmation of the author of
> play-scalate module, he said it's impossible to work with java
> controller and scalate in play-framework. It's a pity

As I said, maybe we can have a JavaScalateController? Ideally we'd be
able to reuse the code in play-scalate but just provide a different
controller. Or create a more Java-friendly facade for Scalate so its
easier to use from a Java controller in Play?

http://github.com/pk11/play-scalate/issues#issue/5
http://scalate.assembla.com/spaces/scalate/support/tickets/129

李鹏

unread,
Aug 8, 2010, 2:34:26 PM8/8/10
to sca...@googlegroups.com
Thank for doing this!
Reply all
Reply to author
Forward
0 new messages