Iterate.java revision 56ed4167b942ec265f9cee70ac4d71d10b3835ce
1/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.clearsilver.jsilver.examples.basic;
18
19import com.google.clearsilver.jsilver.JSilver;
20import com.google.clearsilver.jsilver.data.Data;
21import com.google.clearsilver.jsilver.resourceloader.ClassResourceLoader;
22
23import java.io.IOException;
24
25/**
26 * A template that iterates over some items.
27 */
28public class Iterate {
29
30  public static void main(String[] args) throws IOException {
31
32    // Load resources (e.g. templates) from classpath, along side this class.
33    JSilver jSilver = new JSilver(new ClassResourceLoader(Iterate.class));
34
35    // Set up some data.
36    Data data = jSilver.createData();
37    data.setValue("query", "Fruit");
38    data.setValue("results.0.title", "Banana");
39    data.setValue("results.0.url", "http://banana.com/");
40    data.setValue("results.1.title", "Apple");
41    data.setValue("results.1.url", "http://apple.com/");
42    data.setValue("results.2.title", "Lemon");
43    data.setValue("results.2.url", "http://lemon.com/");
44
45    // Render template to System.out.
46    jSilver.render("iterate.cs", data, System.out);
47  }
48}
49