Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

odd one-off improvements #99

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Core/RazorEngine.Core/Templating/ITemplateService.cs
Expand Up @@ -125,6 +125,15 @@ public interface ITemplateService : IDisposable
/// <param name="cacheName">The name of the template type in cache.</param>
/// <returns>Whether or not the template has been removed.</returns>
bool RemoveTemplate(string cacheName);

/// <summary>
/// Links the specified name in the cache to another already existing template with
/// the given cache name.
/// </summary>
/// <param name="cacheName">The name in the cache to link to an existing template.</param>
/// <param name="existingTemplateCacheName">The name in the cache of the existing template that should be linked to.</param>
/// <returns>true if the cache contained a mapping for the given existing template cache name</returns>
bool LinkTemplate(string cacheName, string existingTemplateCacheName);

/// <summary>
/// Parses and returns the result of the specified string template.
Expand Down
Expand Up @@ -350,6 +350,11 @@ public bool RemoveTemplate(string cacheName)
return _proxy.RemoveTemplate(cacheName);
}

public bool LinkTemplate(string cacheName, string existingTemplateCacheName)
{
return _proxy.LinkTemplate(cacheName, existingTemplateCacheName);
}

/// <summary>
/// Parses and returns the result of the specified string template.
/// </summary>
Expand Down
29 changes: 29 additions & 0 deletions src/Core/RazorEngine.Core/Templating/TemplateBase.cs
Expand Up @@ -123,7 +123,9 @@ string ITemplate.Run(ExecuteContext context)
using (var writer = new StringWriter(builder))
{
_context.CurrentWriter = writer;
BeforeExecute();
Execute();
AfterExecute();
_context.CurrentWriter = null;
}

Expand Down Expand Up @@ -336,6 +338,33 @@ public virtual void WriteTo(TextWriter writer, TemplateWriter helper)
{
helper.WriteTo(writer);
}

/// <summary>
/// Allows setting the model when the type of the template does not match the model type.
/// Subclasses can override this to handle other model types beyond the standard <see cref="ITemplate{T}.Model"/>.
/// </summary>
protected internal virtual void SetModel(object model)
{

}

/// <summary>
/// Called immediately before the template's <see cref="Execute"/> method. Can be overridden as a hook point into
/// template execution. The default implementation does nothing.
/// </summary>
protected virtual void BeforeExecute()
{

}

/// <summary>
/// Called immediately after the template's <see cref="Execute"/> method, if no errors have occured. Can be overridden as a hook point into
/// template execution. The default implementation does nothing.
/// </summary>
protected virtual void AfterExecute()
{

}
#endregion
}
}
16 changes: 15 additions & 1 deletion src/Core/RazorEngine.Core/Templating/TemplateBaseOfT.cs
@@ -1,4 +1,6 @@
namespace RazorEngine.Templating
using System;

namespace RazorEngine.Templating
{
using System.Dynamic;

Expand Down Expand Up @@ -73,6 +75,18 @@ protected override ITemplate ResolveLayout(string name)
{
return TemplateService.Resolve(name, (T)currentModel);
}

/// <summary>
/// Allows setting the model when the type of the template does not match the model type.
/// Subclasses can override this to handle other model types beyond the standard <see cref="ITemplate{T}.Model"/>.
/// The default implementation will cause a <see cref="InvalidCastException"/>.
/// </summary>
/// <param name="model">The model, which is not an instance of type T.</param>
protected internal override void SetModel(object model)
{
//default implementation is direct cast
this.Model = (T)model;
}
#endregion
}
}
24 changes: 24 additions & 0 deletions src/Core/RazorEngine.Core/Templating/TemplateService.cs
Expand Up @@ -549,6 +549,23 @@ public bool RemoveTemplate(string cacheName)
return _cache.TryRemove(cacheName, out item);
}

/// <summary>
/// Links the specified name in the cache to another already existing template with
/// the given cache name.
/// </summary>
/// <param name="cacheName">The name in the cache to link to an existing template.</param>
/// <param name="existingTemplateCacheName">The name in the cache of the existing template that should be linked to.</param>
/// <returns>true if the cache contained a mapping for the given existing template cache name</returns>
public bool LinkTemplate(string cacheName, string existingTemplateCacheName)
{
CachedTemplateItem item;
if (_cache.TryGetValue(existingTemplateCacheName, out item))
{
_cache.AddOrUpdate(cacheName, item, (n, i) => item);
return true;
}
return false;
}

/// <summary>
/// Resolves the template with the specified name.
Expand Down Expand Up @@ -632,6 +649,13 @@ private static void SetModel<T>(ITemplate template, T model)
return;
}

var templateBase = template as TemplateBase;
if (templateBase != null)
{
templateBase.SetModel(model);
return;
}

SetModelExplicit(template, model);
}

Expand Down