@@ -23,42 +23,45 @@ public IRazorTemplate ExecuteTemplate<T>(T model, string name, string defaultTem
2323 if ( instance == null )
2424 throw new ArgumentException ( "No compiled template exists with the specified name." ) ;
2525
26- SetService ( instance , this ) ;
27- SetModel ( instance , model ) ;
28- TemplateBase . ViewBag = new ExpandoObject ( ) ;
26+ using ( instance as IDisposable )
27+ {
28+ SetService ( instance , this ) ;
29+ SetModel ( instance , model ) ;
30+ TemplateBase . ViewBag = new ExpandoObject ( ) ;
2931
30- var razorTemplate = ( IRazorTemplate ) instance ;
31- razorTemplate . Init ( viewEngine , new ViewDataDictionary < T > ( model ) , httpReq , httpRes ) ;
32-
33- instance . Execute ( ) ;
32+ var razorTemplate = ( IRazorTemplate ) instance ;
33+ razorTemplate . Init ( viewEngine , new ViewDataDictionary < T > ( model ) , httpReq , httpRes ) ;
3434
35- var template = httpReq . GetTemplate ( ) ;
36- if ( ! string . IsNullOrEmpty ( template ) )
37- template = viewEngine . HasTemplate ( template ) ? template : null ;
35+ instance . Execute ( ) ;
3836
39- if ( template == null && ! razorTemplate . Layout . IsNullOrEmpty ( ) )
40- template = razorTemplate . Layout . MapServerPath ( ) ;
37+ var template = httpReq . GetTemplate ( ) ;
38+ if ( ! string . IsNullOrEmpty ( template ) )
39+ template = viewEngine . HasTemplate ( template ) ? template : null ;
4140
42- if ( template == null )
43- template = defaultTemplatePath ;
41+ if ( template == null && ! razorTemplate . Layout . IsNullOrEmpty ( ) )
42+ template = razorTemplate . Layout . MapServerPath ( ) ;
4443
45- var layoutTemplate = GetTemplate ( template ?? RazorFormat . DefaultTemplate ) ;
46- if ( layoutTemplate != null )
47- {
48- layoutTemplate . ChildTemplate = razorTemplate ;
49- SetService ( layoutTemplate , this ) ;
50- SetModel ( layoutTemplate , model ) ;
51- layoutTemplate . Execute ( ) ;
44+ if ( template == null )
45+ template = defaultTemplatePath ;
5246
53- return layoutTemplate ;
54- }
55- else if ( defaultTemplatePath != null )
56- {
57- throw new ArgumentException (
58- "No template exists with the specified Layout: " + defaultTemplatePath ) ;
59- }
47+ var layoutTemplate = GetTemplate ( template ?? RazorFormat . DefaultTemplate ) ;
48+ if ( layoutTemplate != null )
49+ {
50+ layoutTemplate . ChildTemplate = razorTemplate ;
51+ SetService ( layoutTemplate , this ) ;
52+ SetModel ( layoutTemplate , model ) ;
53+ layoutTemplate . Execute ( ) ;
6054
61- return razorTemplate ;
55+ return layoutTemplate ;
56+ }
57+ else if ( defaultTemplatePath != null )
58+ {
59+ throw new ArgumentException (
60+ "No template exists with the specified Layout: " + defaultTemplatePath ) ;
61+ }
62+
63+ return razorTemplate ;
64+ }
6265 }
6366
6467 readonly Dictionary < string , string > pagePathAndNames = new Dictionary < string , string > ( StringComparer . CurrentCultureIgnoreCase ) ;
@@ -86,7 +89,8 @@ public ITemplate GetAndCheckTemplate(string name)
8689
8790 ITemplate instance ;
8891 templateCache . TryGetValue ( name , out instance ) ;
89- return instance ;
92+
93+ return instance . CloneTemplate ( ) ;
9094 }
9195
9296 public IRazorTemplate GetTemplate ( string name )
@@ -103,7 +107,7 @@ public IRazorTemplate GetTemplate(string name)
103107 //Re-check after all templates have been compiled
104108 viewEngine . EnsureAllCompiled ( ) ;
105109 if ( templateCache . TryGetValue ( name , out instance ) )
106- return instance as IRazorTemplate ;
110+ return instance . CloneTemplate ( ) as IRazorTemplate ;
107111
108112 view = viewEngine . GetView ( name ) ;
109113 if ( view == null )
@@ -113,33 +117,36 @@ public IRazorTemplate GetTemplate(string name)
113117
114118 templateCache . TryGetValue ( name , out instance ) ;
115119 }
116- return instance as IRazorTemplate ;
120+ return instance as IRazorTemplate ; //Cloned in GetAndCheckTemplate()
117121 }
118122
119123 public IRazorTemplate RenderPartial < T > ( T model , string name )
120124 {
121125 var template = GetTemplate ( name ) ;
122- SetService ( template , this ) ;
123- SetModel ( template , model ) ;
126+ using ( template as IDisposable )
127+ {
128+ SetService ( template , this ) ;
129+ SetModel ( template , model ) ;
124130
125- //TODO: make less ugly,
126- //since executing templates clears the buffer we need to capture
127- //what's been rendered and prepend after.
128- var capture = template . Result ;
131+ //TODO: make less ugly,
132+ //since executing templates clears the buffer we need to capture
133+ //what's been rendered and prepend after.
134+ var capture = template . Result ;
129135
130- try
131- {
132- template . Execute ( ) ;
133- }
134- catch ( Exception ex )
135- {
136- throw new InvalidOperationException (
137- "Could not execute partial: " + name + ", model: " + model ) ;
138- }
136+ try
137+ {
138+ template . Execute ( ) ;
139+ }
140+ catch ( Exception ex )
141+ {
142+ throw new InvalidOperationException (
143+ "Could not execute partial: " + name + ", model: " + model ) ;
144+ }
139145
140- template . Prepend ( capture ) ;
141-
142- return template ;
146+ template . Prepend ( capture ) ;
147+
148+ return template ;
149+ }
143150 }
144151 }
145152}
0 commit comments