FreeMarker

一目了然的数据模型
As you have seen, the data-model is basically a tree.正如你所看到的,数据模型基本上是一个树。 This tree can be arbitrarily complicated and deep, for example:这种树可以任意复杂和深刻的,例如:

  
(root) (根)
  | |
  +- animals + - 动物
  | | | |
  | +- mouse | + - 鼠标
  | | | | | |  
  | | +- size = "small" | | + - 大小=“小”
  | | | | | |  
  | | +- price = 50 | | + - 价格= 50
  | | | |
  | +- elephant | + - 大象
  | | | | | |  
  | | +- size = "large" | | + - 大小=“大”
  | | | | | |  
  | | +- price = 5000 | | + - 价格= 5000
  | | | |
  | +- python | + - 蟒蛇
  | | | |  
  | +- size = "medium" | + - 尺寸=“中等”
  | | | |  
  | +- price = 4999 | + - 价格= 4999
  | |
  +- test = "It is a test" + - 测试=“这是一个测试”
  | |
  +- whatnot + - 诸如此类
      | |
      +- because = "don't know" + - 因为“不知道”  
 
  

The variables that act as directories (the root, animals , mouse , elephant , python , whatnot ) are called hashes .充当目录(根, 动物 , 老鼠 , 大象 , 蟒蛇 , 诸如此类 )被称为哈希变量。 Hashes store other variables (the so called哈希存储其他变量(即所谓的 subvariables ) by a lookup name (eg, "animals", "mouse" or "price").查找名称(如“动物”,“鼠标”或“价格”) 的子变量) 。

The variables that store a single value ( size , price , test and because ) are called scalars .存储一个值( 尺寸 , 价格 , 测试和因为 )的变量被称为标量 。

When you want to use a subvariable in a template, you specify its path from the root, and separate the steps with dots.当您要使用的模板中的一个子变量,从根本上指定其路径,并用点分开的步骤。 To access the price of a mouse , you start from the root and go into animals , and then go into mouse then go into price .要访问鼠标的价格,从根开始, 进入动物,然后进入鼠标然后进入价格。 So you write animals.mouse.price .所以,你写animals.mouse.price。 When you put the special ${ ... } codes around an expression like this, you are telling FreeMarker to output the corresponding text at that point.当你把周围表达这样的特殊的$ {...}代码,你告诉FreeMarker的输出在这一点上相应的文本。

There is one more important kind of variable: sequences .还有一个更重要的一种变量: 序列 。 They are similar to hashes, but they don't store names for the variables they contain.它们类似于哈希,但他们不存储它们所包含的变量的名称。 Instead, they store the subvariables sequentially, and you can access them with a numerical index.相反,他们的子变量存储顺序,您可以访问数字索引。 For example, in this data-model, animals and whatnot.fruits are sequences:例如,在这个数据模型, 动物和whatnot.fruits序列:

  
(root) (根)
  | |
  +- animals + - 动物
  | | | |
  | +- (1st) | + - (第1)
  | | | | | |
  | | +- name = "mouse" | | + - 名称=“鼠标”
  | | | | | |
  | | +- size = "small" | | + - 大小=“小”
  | | | | | |
  | | +- price = 50 | | + - 价格= 50
  | | | |
  | +- (2nd) | + - (第2)
  | | | | | |
  | | +- name = "elephant" | | + - 名称=“大象”
  | | | | | |
  | | +- size = "large" | | + - 大小=“大”
  | | | | | |
  | | +- price = 5000 | | + - 价格= 5000
  | | | |
  | +- (3rd) | + - (第3)
  | | | |
  | +- name = "python" | + - 名称=“蟒蛇”
  | | | |
  | +- size = "medium" | + - 尺寸=“中等”
  | | | |
  | +- price = 4999 | + - 价格= 4999
  | |
  +- whatnot + - 诸如此类
      | |
      +- fruits + - 水果
          | |
          +- (1st) = "orange" + - (第1)=“橙”
          | |
          +- (2nd) = "banana" + - (第二)=“香蕉”  
 
  

To access a subvariable of a sequence you use a numerical index in square brackets.要访问您使用方括号中的数字索引的一个序列的子变量。 Indexes start from 0 (it's a programmer tradition to start with 0), thus the index of the first item is 0, the index of the second item is 1, and so on.索引从0(从0开始,这是一个程序员的传统)开始,因此该指数的第一个项目是0,第二项的索引是1,依此类推。 So to get the name of the first animal you write animals[0].name .所以获得的第一个动物的名称, 你写的动物[0]。名称。 To get the second item in whatnot.fruits (which is the string "banana" ) you write whatnot.fruits[1] .在whatnot.fruits要获得的第二项(这是字符串“香蕉”),你写whatnot.fruits [1] 。

Scalars can be further divided into these categories:标量可进一步分为这些类别:

String: Text, that is, an arbitrary sequence of characters such as ''m'', ''o'', ''u'', ''s'', ''e'' above.字符串:文本,这是一个任意的字符,如序列“米”,“O”,“ü”,“S”,“E”上面。 For example the name -s and size -s are strings above.例如名称 - S和大小 - S是字符串以上。

Number: It's a numerical value, like the price -s above.编号:像s以上的价格 ,这是一个数值。 The string "50" and the number 50 are two totally different things in FreeMarker.字符串“50”和50号是两个完全不同的东西在FreeMarker。 The former is just a sequence of two characters (which happens to be readable as a number for humans), while the latter is a numerical value that you can use, say, in arithmetical calculations.前者仅仅是两个字符的序列(这恰好是一些对人类可读),而后者则是一个数值,您可以使用,例如在算术计算,。

Date/time: A date or time.日期/时间:日期或时间。 Like the date an animal were captured, or the time the shop opens.日期一样的动物被抓获,或打开店铺的时间。

Boolean: A true/false (yes/no, on/off, etc.) thing.布尔:一个真/假(是/否,开/关,等)的事情。 Like animals could have a protected subvariable, which store if the animal is protected or not.喜欢的动物可能有一个受保护的子变量,它存储如果动物是保护或不保护。

Summary:摘要:

The data-model can be visualized as a tree.数据模型可以可视化树。

Scalars store a single value.标量存储一个值。 The value can be a string or a number or a date/time or a boolean.该值可以是一个字符串或一个数字或日期/时间或布尔。

Hashes are containers that store other variables and associate them with a unique lookup name.哈希存储其他变量和一个独特的查找名称相关联的容器。

Sequences are containers that store other variables in an ordered sequence.序列是一个有序的序列,存储在其他变量的容器。 The stored variables can be retrieved via their numerical index, starting from 0.检索存储的变量可以通过其数字索引,从0开始。

你可能感兴趣的:(freemarker)