Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added overloaded methods that support expressing the model type, fixes

  • Loading branch information...
commit 92e58399b073cbb1919d3955d2aed7ab8d2cf776 1 parent 66bed98
Matthew Abbott authored
View
72 src/Core/RazorEngine.Core/Templating/TemplateService.cs
@@ -149,7 +149,20 @@ public virtual ITemplate CreateTemplate(string razorTemplate)
[Pure]
public virtual ITemplate CreateTemplate(string razorTemplate, object model)
{
- var type = CreateTemplateType(razorTemplate, _objectType);
+ return CreateTemplate(razorTemplate, _objectType, model);
+ }
+
+ /// <summary>
+ /// Creates an instance of <see cref="ITemplate"/> from the specified string template.
+ /// </summary>
+ /// <param name="razorTemplate">The string template.</param>
+ /// <param name="modelType">The expected model type.</param>
+ /// <param name="model">The model instance.</param>
+ /// <returns>An instance of <see cref="ITemplate"/>.</returns>
+ [Pure]
+ public virtual ITemplate CreateTemplate(string razorTemplate, Type modelType, object model)
+ {
+ var type = CreateTemplateType(razorTemplate, modelType);
var instance = CreateTemplate(type, model);
return instance;
@@ -632,6 +645,20 @@ public virtual string Parse(string razorTemplate, object model)
/// <summary>
/// Parses and returns the result of the specified string template.
/// </summary>
+ /// <param name="razorTemplate">The string template.</param>
+ /// <param name="modelType">The expected model type.</param>
+ /// <param name="model">The model instance.</param>
+ /// <returns>The string result of the template.</returns>
+ [Pure]
+ public virtual string Parse(string razorTemplate, Type modelType, object model)
+ {
+ var instance = CreateTemplate(razorTemplate, modelType, model);
+ return Run(instance);
+ }
+
+ /// <summary>
+ /// Parses and returns the result of the specified string template.
+ /// </summary>
/// <typeparam name="T">The model type.</typeparam>
/// <param name="razorTemplate">The string template.</param>
/// <param name="model">The model instance.</param>
@@ -728,14 +755,32 @@ public virtual IEnumerable<string> ParseMany(IEnumerable<string> razorTemplates,
[Pure]
public virtual IEnumerable<string> ParseMany(string razorTemplate, IEnumerable<object> models, bool parallel = false)
{
+ return ParseMany(razorTemplate, _objectType, models, parallel);
+ }
+
+ /// <summary>
+ /// Parses the template and merges with the many models provided.
+ /// </summary>
+ /// <remarks>
+ /// This method makes assumptions that all models are of the same type, or have a common base type. This is done
+ /// by using the first model's GetType() method to resolve what type it should be. Feels dirty.
+ /// </remarks>
+ /// <param name="razorTemplate">The razor template.</param>
+ /// <param name="modelType">The expected model type.</param>
+ /// <param name="models">The set of models.</param>
+ /// <param name="parallel">Flag to determine whether parsing in parallel.</param>
+ /// <returns>The set of parsed template results.</returns>
+ [Pure]
+ public virtual IEnumerable<string> ParseMany(string razorTemplate, Type modelType, IEnumerable<object> models, bool parallel = false)
+ {
Contract.Requires(razorTemplate != null);
+ Contract.Requires(modelType != null);
Contract.Requires(models != null);
// If we only have one model, leave early and process singular.
if (models.Count() == 1)
- return new[] { Parse(razorTemplate, models.First()) };
+ return new[] { Parse(razorTemplate, modelType, models.First()) };
- var modelType = models.First().GetType();
// Compile the type just the once, and reuse that for each model.
var type = CreateTemplateType(razorTemplate, modelType);
@@ -745,7 +790,7 @@ public virtual IEnumerable<string> ParseMany(string razorTemplate, IEnumerable<o
.Select(m => Run(CreateTemplate(type, m)))
.ToArray();
- return models.Select(m => Run(CreateTemplate(type, m))).ToArray();
+ return models.Select(m => Run(CreateTemplate(type, m))).ToArray();
}
/// <summary>
@@ -783,7 +828,22 @@ public virtual IEnumerable<string> ParseMany<T>(string razorTemplate, IEnumerabl
[Pure]
public virtual IEnumerable<string> ParseMany(IEnumerable<string> razorTemplates, IEnumerable<object> models, bool parallel = false)
{
+ return ParseMany(razorTemplates, _objectType, models, parallel);
+ }
+
+ /// <summary>
+ /// Parses the specified set of templates.
+ /// </summary>
+ /// <param name="razorTemplates">The set of string templates to parse.</param>
+ /// <param name="modelType">The expected model type.</param>
+ /// <param name="models">The set of models.</param>
+ /// <param name="parallel">Flag to determine whether parsing in parallel.</param>
+ /// <returns>The set of parsed template results.</returns>
+ [Pure]
+ public virtual IEnumerable<string> ParseMany(IEnumerable<string> razorTemplates, Type modelType, IEnumerable<object> models, bool parallel = false)
+ {
Contract.Requires(razorTemplates != null);
+ Contract.Requires(modelType != null);
Contract.Requires(models != null);
Contract.Requires(razorTemplates.Count() == models.Count(), "Expected same number of models as templates to be processed.");
@@ -792,10 +852,10 @@ public virtual IEnumerable<string> ParseMany(IEnumerable<string> razorTemplates,
if (parallel)
return GetParallelQueryPlan<string>()
.CreateQuery(razorTemplates)
- .Select((t, i) => Parse(t, modelList[i]))
+ .Select((t, i) => Parse(t, modelType, modelList[i]))
.ToArray();
- return razorTemplates.Select((t, i) => Parse(t, modelList[i])).ToArray();
+ return razorTemplates.Select((t, i) => Parse(t, modelList[i])).ToArray();
}
/// <summary>
View
26 src/Core/Tests/RazorEngine.Core.Tests/Issues/Release_3_0_TestFixture.cs
@@ -144,9 +144,7 @@ public void Issue11_TemplateServiceShouldCompileModellessTemplate()
[Test]
public void Issue16_LastNullValueShouldReturnEmptyString()
{
- var config = new TemplateServiceConfiguration() { Debug = true };
-
- using (var service = new TemplateService(config))
+ using (var service = new TemplateService())
{
const string template = "<h1>Hello @Model.Person.Forename</h1>";
const string expected = "<h1>Hello </h1>";
@@ -159,6 +157,28 @@ public void Issue16_LastNullValueShouldReturnEmptyString()
}
/// <summary>
+ /// We should support overriding the model type for templates that do not specify @model.
+ ///
+ /// Issue 17: https://github.com/Antaris/RazorEngine/issues/17
+ /// </summary>
+ [Test]
+ public void TemplateService_ShouldAllowTypeOverrideForNonGenericCompile()
+ {
+ using (var service = new TemplateService())
+ {
+ const string template = "@Model.Name";
+ const string expected = "Matt";
+
+ object model = new { Name = "Matt" };
+ Type modelType = model.GetType();
+
+ string result = service.Parse(template, modelType, model);
+
+ Assert.That(result == expected, "Result does not match expected: " + result);
+ }
+ }
+
+ /// <summary>
/// Subclassed models should be supported in layouts (and also partials).
///
/// Issue 21: https://github.com/Antaris/RazorEngine/issues/21
Please sign in to comment.
Something went wrong with that request. Please try again.