Dev:Templates

Aus YaCyWiki
Wechseln zu: Navigation, Suche

This article explains how to use the templateengine. The Engine works recursively from the outer to the inner level. More on that later

basic servlet

A Servlet consists of two parts: a Htmltemplate and a corresponding Javafile. Basic HTML-Template:

#[test]#<br />
#[test2]#

The simplest corresponing javafile:

import net.yacy.cora.protocol.RequestHeader;
import net.yacy.server.serverObjects;
import net.yacy.server.serverSwitch;
public class test {
  public static serverObjects respond(RequestHeader header, serverObjects post, serverSwitch env) {

  serverObjects prop = new serverObjects();
 
  prop.put("test", "first test");
  prop.put("test2", "second test");

  return prop;
  }
}

Texttemplates

The most basic form of templates are texttemplates. they just insert a text into the template:

#[test]#

and in the javafile:

prop.put("test", "a test");


Alternatives

With alternatives, the program chooses one text from some different texts:

#(alternatives)#
test(index 0)
::
second test(index 1)
::
index 2, the third string #[test]#
#(/alternatives)#

to choose one of them:

prop.put("alternatives", 1); //select second text.
prop.put("alternatives_test", "texttemplate"); // fill the texttemplate inside the alternative

multi-templates

HTML

#{multi}#
#[test]#
#{/multi}#

Java:

int i;
for(i=0;i<=10;i++){
    prop.put("multi_"+i+"_test", (i+1)+". Item");
}
prop.put("multi", i);

Here we see some recursion in the HTML. First the #{multi}# will be used, and the result will be handled as text-template.

So the multi-template be used and then sent to the texttemplate-handler:

#[multi_0_test]#
#[multi_1_test]#
#[multi_2_test]#
#[multi_3_test]#
#[multi_4_test]#
#[multi_5_test]#
#[multi_6_test]#
#[multi_7_test]#
#[multi_8_test]#
#[multi_9_test]#

so you can see how to address the normal templates and how to tell the multitemplatehandler, how often to repeat, set "multi"=number of elements.(index of the last +1)