Caching @Include Model data.

May 12, 2011 at 9:48 PM
Edited May 12, 2011 at 9:49 PM

When using a template that has an Include with a model object passed in, that template cache's the objects values on the next run.

Scenario:

We have a complex template that contains Includes of other templates and we also pass in a model to that template.

      <html><span>Model.Name</span> @Include("Header", Model.List)</html>

So, when I iterate thru this to send that same template with a different Model, the main template will have the correct values of the Model, but the subsequent templates will use the first Model's instance everytime it calls the Include. The value of Model.List will of the first Model, not the current Model.

Is there a way to force the parse, or run, to not cache the Include when it is given a Model?

I had to flatten my template by not using the Includes, which really sucked.

MOVED to Issue Tracker

 

 

 

Oct 25, 2011 at 2:18 AM

Hey Joe,

Can't believe it will not utilise the current model everytime? Seems quite the oversight?

Cheers

Andrew

Coordinator
Oct 31, 2011 at 1:46 PM

Hi,

The behaviour of the caching mechanism is corrected in v3, which are currently commiting to RazorEngine's new home at GitHub (https://www.github.com/Antaris/RazorEngine).

There are two fundamental changes that affect the @Include method:

  1. We no longer cache the last value, we correctly cache the template type, and spin up a new instance each time.
  2. If no model is passed to @Include, the model passed to the calling template is passed to the include.