我们知道在vm中可以直接使用
Session、
SiteRoot等,那么我们还可以使用哪些默认的系统变量呢?
其实可以直接在vm中使用的系统变量都是在NVelocityViewEngine类的CreateContext方法中定义的。下面我们就看看到底定义了哪些(详见代码中的注释说明):
其实可以直接在vm中使用的系统变量都是在NVelocityViewEngine类的CreateContext方法中定义的。下面我们就看看到底定义了哪些(详见代码中的注释说明):
1
private
IContextCreateContext(IRailsEngineContextcontext,Controllercontroller)
2
{
3
HashtableinnerContext=newHashtable(StringComparer.InvariantCultureIgnoreCase);
4
//对应的controller中的方法和属性可以直接在vm中使用
5
innerContext.Add(TemplateKeys.Controller,controller);
//当前请求的上下文,请求,Session等可以直接使用
6
innerContext.Add(TemplateKeys.Context,context);
7
innerContext.Add(TemplateKeys.Request,context.Request);
8
innerContext.Add(TemplateKeys.Response,context.Response);
9
innerContext.Add(TemplateKeys.Session,context.Session);
10
//对应的controller中的Resources中所有资源可以直接在vm中使用
11
if(controller.Resources!=null)
12

{
13
foreach(Stringkeyincontroller.Resources.Keys)
14

{
15
innerContext[key]=controller.Resources[key];
16
}
17
}
18
//所有Params中的值可以直接在vm中使用
19
foreach(Stringkeyincontext.Params.AllKeys)
20

{
21
if(key==null)continue;//Nastybug?
22
objectvalue=context.Params[key];
23
if(value==null)continue;
24
innerContext[key]=value;
25
}
26
27
//listfrom:http://msdn2.microsoft.com/en-us/library/hfa3fa08.aspx
28
object[]builtInHelpers=
29
newobject[]
30

{
31
newStaticAccessorHelper<Byte>(),
32
newStaticAccessorHelper<SByte>(),
33
newStaticAccessorHelper<Int16>(),
34
newStaticAccessorHelper<Int32>(),
35
newStaticAccessorHelper<Int64>(),
36
newStaticAccessorHelper<UInt16>(),
37
newStaticAccessorHelper<UInt32>(),
38
newStaticAccessorHelper<UInt64>(),
39
newStaticAccessorHelper<Single>(),
40
newStaticAccessorHelper<Double>(),
41
newStaticAccessorHelper<Boolean>(),
42
newStaticAccessorHelper<Char>(),
43
newStaticAccessorHelper<Decimal>(),
44
newStaticAccessorHelper<String>(),
45
newStaticAccessorHelper<Guid>(),
46
newStaticAccessorHelper<DateTime>()
47
};
48
//常见的系统类可以之间使用,比如可以直接在vm中使用$Int32.MaxValue
49
foreach(objecthelperinbuiltInHelpers)
50

{
51
innerContext[helper.GetType().GetGenericArguments()[0].Name]=helper;
52
}
53
//定义的Helper类可以直接在vm中使用
54
if(controller.Helpers!=null)
55

{
56
foreach(objectkeyincontroller.Helpers.Keys)
57

{
58
innerContext[key]=controller.Helpers[key];
59
}
60
}
61
62
//Addingflashasacollectionandeachindividualitem
63
//定义的Flash值可以直接使用
64
if(context.Flash!=null)
65

{
66
innerContext[Flash.FlashKey]=context.Flash;
67
68
foreach(DictionaryEntryentryincontext.Flash)
69

{
70
if(entry.Value==null)continue;
71
innerContext[entry.Key]=entry.Value;
72
}
73
}
74
//定义的PropertyBag值可以直接使用
75
if(controller.PropertyBag!=null)
76

{
77
foreach(DictionaryEntryentryincontroller.PropertyBag)
78

{
79
if(entry.Value==null)continue;
80
innerContext[entry.Key]=entry.Value;
81
}
82
}
83
//SiteRoot可以直接使用 ---取得的是应用程序的路径
84
innerContext[TemplateKeys.SiteRoot]=context.ApplicationPath;
85
86
returnnewVelocityContext(innerContext);
87
}

2



3

4

5

//当前请求的上下文,请求,Session等可以直接使用
6

7

8

9

10

//对应的controller中的Resources中所有资源可以直接在vm中使用
11

12



13

14



15

16

17

18

//所有Params中的值可以直接在vm中使用
19

20



21

22

23

24

25

26

27

28

29

30



31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

//常见的系统类可以之间使用,比如可以直接在vm中使用$Int32.MaxValue
49

50



51

52

53

//定义的Helper类可以直接在vm中使用
54

55



56

57



58

59

60

61

62

63

64

65



66

67

68

69



70

71

72

73

74

75

76



77

78



79

80

81

82

83

84

85

86

87

欢迎加入:http://www.itpob.cn/bbs