Passing a list of objects - example is missing also a template demo does show the use of it.

66 views
Skip to first unread message

Thomas Lehmann

unread,
Jun 23, 2014, 5:35:37 AM6/23/14
to rythm...@googlegroups.com
Hi,

at the bottom of this post an extraction of a MovieContainer class that maintains movies (List<Movie>)
trying to render the movies and behind the template code.

I get following ....  could somebody please help onb this?

org.rythmengine.exception.CompileException: movies cannot be resolved to a variable

Template: <html>    <body>    @for(demo.movies.Movie movie: movies){        @movie.getTitle()    }    </body></html>

Relevant template source lines:
-------------------------------------------------
>> 1: <html>    <body>    @for(demo.movies.Movie movie: movies){        @movie.getTitle()    }    </body></html>


Relevant Java source lines:
-------------------------------------------------
   42:
   43:     @Override public org.rythmengine.utils.TextBuilder build(){
   44:         buffer().ensureCapacity(106);
   45: p("<html> <body> "); //line: 1
   46: {
>> 47: __Itr<demo.movies.Movie> __v0 = __Itr.valueOf(movies); //line: 1
   48: int movie_size = __v0.size(); //line: 1
   49: if (movie_size > 0) { //line: 1
   50: int movie_index = 0; //line: 1
   51: for(demo.movies.Movie movie : __v0) { //line: 1
   52: movie_index++; //line: 1
   53: boolean movie_isOdd = movie_index % 2 == 1; //line: 1






Java code:

    public String fromTemplate(final String templateContent) {
        final NamedParams np = NamedParams.instance;
        return Rythm.render(templateContent, np.pair("movies", this.movies));
    }

HTML code:

<html>
    <body>
    @for(demo.movies.Movie movie: movies){
        @movie.getTitle()
    }
    </body>
</html>

Thomas Lehmann

unread,
Jun 23, 2014, 6:06:06 AM6/23/14
to rythm...@googlegroups.com
OK. I found a solution:

Anyway It would be good to show such things in your "simple" examples instead of let "us" searching an hour in your really big documentation. Passing just one variable is simply too simple and not the normal use case, right?

Java code:

    public String fromTemplate(final String templateContent) {
        return Rythm.render(templateContent, this.movies);
    }

HTML code:

@args()  {
    List<demo.movies.Movie> movies
}
<html><body>@for(demo.movies.Movie movie: movies){@movie.getTitle()<br/>}</body></html>

You example (for loop with list of "orders") ... you are not using package names. How did you handle that via Java code?

Regards,
Thomas

green

unread,
Jun 23, 2014, 6:07:38 AM6/23/14
to Rythm Engine, thomas.lehm...@googlemail.com
Add the following code before using "movies":

"@args List<demo.movies.Movie> movies"

BTW you can using import to save your typing, and your template could be something like:

@import demo.movies.*
@args List<Movie> movies
<html>
    <body>
    @for(movie: movies){
        @movie.getTitle()
    }
    </body>
</html>

Or even:

@import demo.movies.*
@args List<Movie> movies
<html>
    <body>
    @for(movies){
        @_.getTitle()
    }
    </body>
</html>



--
You received this message because you are subscribed to the Google Groups "rythmengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rythmengine...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

green

unread,
Jun 23, 2014, 6:15:25 AM6/23/14
to Rythm Engine
Hi Thomas,

You are right. I am considering redesign the home page to make it convey more information for the first time visitor! I am thinking of put the sample in http://rythmengine.org/doc/feature#developer-friendly, however it might be too complicated.

Probably a better way is to embed a fiddle (http://fiddle.rythmengine.org/) at home page and let visitor to play by themselves ;-)

Cheers,
Green

PS. about the package, use the @import directive


--
Reply all
Reply to author
Forward
0 new messages