Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Play controllers inject variables with the proper name into templates?

In the Play getting started docs, they show this controller:

public static void index() {
  Post frontPost = Post.find("order by postedAt desc").first();
  List<Post> olderPosts = Post.find("order by postedAt desc").from(1).fetch(10);
  render(frontPost, olderPosts);
}

Then, in the template the frontPost and olderPosts are used without any special mapping!

<a href="#">${frontPost.title}</a>

How is Play preserving these names?

like image 953
Adam Rabung Avatar asked Oct 27 '10 19:10

Adam Rabung


1 Answers

It is made by code injection.

At compile, some classes are enhanced (with code injection, by Javassist), in order to add some informations, such as variable names.

In the render method, this operation is done by the "play.classloading.enhancers.LocalvariablesNamesEnhancer.LocalVariablesNamesTracer" class.

like image 76
Benoit Courtine Avatar answered Sep 21 '22 07:09

Benoit Courtine