Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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