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
/styleguide/
. For each file—
- Check for the existence of 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/
. - In the JSON file, look up the value for
_template
, and extract the base name (file name without thehbs
extension). Assign this value to<base>
. - Create a new class file
View.java
in the path from step b. - Declare the class’s package as package
<package>.<currentpath>
, where 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
View.java
. - Create a getter inside the file
View.java
. (At runtime, the getters populate the corresponding field in the template.)
- Create a constructor inside the file
- Check for the existence of a
Suppose you want to create a view for articles. Your theme files may look as follows:
_config.json
Data file specifying package name
{
"javaPackage" : "brightspotnews"
}
Article.hbs
Template
<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
Data file specifying associated template and source for method names
{
"_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;
/* Imports */
/* Various annotations */
public interface ArticleView {
CharSequence getHeadline();
CharSequence getBody();
CharSequence getImage();
}
-
Interface’s package name.
- The first component of the package name is the value for javaPackage as show in the snippet "Data file specifying package name."
- The remaining components reflect the directory structure to
Article.json
under/styleguide/
.
-
Declares the interface’s name. The name is based on the name of the discovered data file
Article.json
. -
Declares a method getHeadline. The view generator derives this method’s name from the key headline as appearing in the snippet "Data file specifying associated template and source for method names." The other method names are derived similarly.
See also:
Previous Topic
Generating data files from published content
Next Topic
Generating Java view interfaces from data files