Skip to content
Newer
Older
100644 129 lines (110 sloc) 3.54 KB
7be9e8c @jdiamond Simple template includes.
jdiamond authored
1 using System;
5c2d2db @jdiamond Lots of renaming.
jdiamond authored
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.IO;
5
6 namespace Nustache.Core
7 {
8 public class RenderContext
9 {
d9b6a16 @jdiamond Prevent infinite template recursion.
jdiamond authored
10 private const int IncludeLimit = 1024;
4b61e74 @jdiamond Some renaming and refactoring.
jdiamond authored
11 private readonly Stack<Section> _sectionStack = new Stack<Section>();
31a9ee8 @jdiamond First cracp at internal template definitions.
jdiamond authored
12 private readonly Stack<object> _dataStack = new Stack<object>();
5c2d2db @jdiamond Lots of renaming.
jdiamond authored
13 private readonly TextWriter _writer;
7be9e8c @jdiamond Simple template includes.
jdiamond authored
14 private readonly Func<string, Template> _templateLocator;
d9b6a16 @jdiamond Prevent infinite template recursion.
jdiamond authored
15 private int _includeLevel;
5c2d2db @jdiamond Lots of renaming.
jdiamond authored
16
d295887 @jdiamond Small GetValue refactoring.
jdiamond authored
17 public RenderContext(Section section, object data, TextWriter writer, Func<string, Template> templateLocator)
5c2d2db @jdiamond Lots of renaming.
jdiamond authored
18 {
d295887 @jdiamond Small GetValue refactoring.
jdiamond authored
19 _sectionStack.Push(section);
e88302c @jdiamond Extracted Container base class. Looking for internal template definit…
jdiamond authored
20 _dataStack.Push(data);
5c2d2db @jdiamond Lots of renaming.
jdiamond authored
21 _writer = writer;
7be9e8c @jdiamond Simple template includes.
jdiamond authored
22 _templateLocator = templateLocator;
d9b6a16 @jdiamond Prevent infinite template recursion.
jdiamond authored
23 _includeLevel = 0;
5c2d2db @jdiamond Lots of renaming.
jdiamond authored
24 }
25
26 public object GetValue(string name)
27 {
d295887 @jdiamond Small GetValue refactoring.
jdiamond authored
28 if (name == ".")
43a6f05 @jdiamond Getting values looks up the stack.
jdiamond authored
29 {
d295887 @jdiamond Small GetValue refactoring.
jdiamond authored
30 return _dataStack.Peek();
43a6f05 @jdiamond Getting values looks up the stack.
jdiamond authored
31 }
32
31a9ee8 @jdiamond First cracp at internal template definitions.
jdiamond authored
33 foreach (var data in _dataStack)
43a6f05 @jdiamond Getting values looks up the stack.
jdiamond authored
34 {
d295887 @jdiamond Small GetValue refactoring.
jdiamond authored
35 if (data != null)
43a6f05 @jdiamond Getting values looks up the stack.
jdiamond authored
36 {
d295887 @jdiamond Small GetValue refactoring.
jdiamond authored
37 var value = ValueGetter.GetValue(data, name);
38
39 if (value != null)
40 {
41 return value;
42 }
43a6f05 @jdiamond Getting values looks up the stack.
jdiamond authored
43 }
44 }
45
46 return null;
47 }
48
5c2d2db @jdiamond Lots of renaming.
jdiamond authored
49 public IEnumerable<object> GetValues(string name)
50 {
51 object value = GetValue(name);
52
53 if (value is bool)
54 {
55 if ((bool)value)
56 {
57 yield return value;
58 }
59 }
60 else if (value is IEnumerable && !(value is string))
61 {
62 foreach (var item in ((IEnumerable)value))
63 {
64 yield return item;
65 }
66 }
67 else if (value != null)
68 {
69 yield return value;
70 }
71 }
72
73 public void Write(string text)
74 {
75 _writer.Write(text);
76 }
7be9e8c @jdiamond Simple template includes.
jdiamond authored
77
78 public void Include(string templateName)
79 {
d9b6a16 @jdiamond Prevent infinite template recursion.
jdiamond authored
80 if (_includeLevel >= IncludeLimit)
81 {
82 throw new NustacheException(
83 string.Format("You have reached the include limit of {0}. Are you trying to render infinitely recursive templates or data?", IncludeLimit));
84 }
85
86 _includeLevel++;
87
e88302c @jdiamond Extracted Container base class. Looking for internal template definit…
jdiamond authored
88 TemplateDefinition templateDefinition = GetTemplateDefinition(templateName);
31a9ee8 @jdiamond First cracp at internal template definitions.
jdiamond authored
89
e88302c @jdiamond Extracted Container base class. Looking for internal template definit…
jdiamond authored
90 if (templateDefinition != null)
31a9ee8 @jdiamond First cracp at internal template definitions.
jdiamond authored
91 {
e88302c @jdiamond Extracted Container base class. Looking for internal template definit…
jdiamond authored
92 templateDefinition.Render(this);
31a9ee8 @jdiamond First cracp at internal template definitions.
jdiamond authored
93 }
94 else if (_templateLocator != null)
95 {
e88302c @jdiamond Extracted Container base class. Looking for internal template definit…
jdiamond authored
96 var template = _templateLocator(templateName);
97 template.Render(this);
31a9ee8 @jdiamond First cracp at internal template definitions.
jdiamond authored
98 }
7be9e8c @jdiamond Simple template includes.
jdiamond authored
99
31a9ee8 @jdiamond First cracp at internal template definitions.
jdiamond authored
100 _includeLevel--;
7be9e8c @jdiamond Simple template includes.
jdiamond authored
101 }
e88302c @jdiamond Extracted Container base class. Looking for internal template definit…
jdiamond authored
102
103 private TemplateDefinition GetTemplateDefinition(string name)
104 {
4b61e74 @jdiamond Some renaming and refactoring.
jdiamond authored
105 foreach (var section in _sectionStack)
e88302c @jdiamond Extracted Container base class. Looking for internal template definit…
jdiamond authored
106 {
4b61e74 @jdiamond Some renaming and refactoring.
jdiamond authored
107 var templateDefinition = section.GetTemplateDefinition(name);
e88302c @jdiamond Extracted Container base class. Looking for internal template definit…
jdiamond authored
108
109 if (templateDefinition != null)
110 {
111 return templateDefinition;
112 }
113 }
114
115 return null;
116 }
117
4b61e74 @jdiamond Some renaming and refactoring.
jdiamond authored
118 public void Push(Section section, object data)
e88302c @jdiamond Extracted Container base class. Looking for internal template definit…
jdiamond authored
119 {
4b61e74 @jdiamond Some renaming and refactoring.
jdiamond authored
120 _sectionStack.Push(section);
e88302c @jdiamond Extracted Container base class. Looking for internal template definit…
jdiamond authored
121 _dataStack.Push(data);
122 }
123
124 public void Pop()
125 {
4b61e74 @jdiamond Some renaming and refactoring.
jdiamond authored
126 _sectionStack.Pop();
e88302c @jdiamond Extracted Container base class. Looking for internal template definit…
jdiamond authored
127 }
5c2d2db @jdiamond Lots of renaming.
jdiamond authored
128 }
129 }
Something went wrong with that request. Please try again.