How the view generator creates view classes
When you build a theme, the view generator executes the following steps:
Open the file
styleguide/_config.json
and look up the value forjavaPackage
. This value is the first component of the package name for the generated view files. Assign this value to<package>
.Loop through each JSON file in the directory
<root>/styleguide/
. For each file—Check for the existence of either a
_template
key. If not found, continue to the next JSON file.Copy the file's path, and duplicate the path under the directory
target/generated-sources/styleguide/<package>/
.In the JSON file, look up the value for
_template
, and extract the base name (file name without the hbs extension). Assign this value to<base>
.Create a new class file
<base>View.java
in the path from step b.Declare the class's package as
package <package>.<currentpath>
, where<currentpath>
reflects the path from/styleguide/
to the current file.Each key inside the JSON file not starting with an underscore is assumed to be a displayed field. For each such key—
Create a constructor inside the file
<base>View.java
.Create a getter inside the file
<base>View.java
. (At runtime, the getters populate the corresponding field in the template.)
Suppose you want to create a view for articles. Your theme files may look as follows:
_config.json
{ "javaPackage" : "brightspotnews" }
Article.hbs
<div class="Article"> <div class="Article-headline"> {{headline}} </div> <div class="Article-body"> {{body}} </div> <div class="Article-image"> <img src="{{image}}" alt="Alternate Text" /> </div> </div>
Article.json
{ "_template": "path/to/Article.hbs", "headline": "{{words(4)}}", "body": "{{paragraphs(2, 5, 15)}}", "image": "{{image(200, 200)}}" }
Resulting file structure
The following diagram shows your directory structure after building the Brightspot project; the gray shading indicates those directories and files Brightspot created based on the data files.
Resulting interface ArticleView.java
This file is a Java interface automatically generated by Brightspot's view generator.
package brightspotnews.content.article; 1 /* Imports */ /* Various annotations */ public interface ArticleView { 2 CharSequence getHeadline(); 3 CharSequence getBody(); CharSequence getImage(); }
Interface's package name.
| |
Declares the interface's name. The name is bas.ed on the name of the discovered data file | |
Declares a method |