|
/*
* Content Watchdog Wordpress C# Plugin
*
* Author:
* Miloslav Beno (miloslav@devsense.com)
*
* Copyright (c) 2012 DEVSENSE s.r.o
*/
using System;
using System.Globalization;
using System.Text;
using System.Dynamic;
using System.Reflection;
namespace Devsense.WordPress.Plugins.ContentWatchdog
{
/// <summary>
/// Provides a base implementation of a template.
/// </summary>
public class PhpRazorTemplateBase
{
public PhpRazorTemplateBase Layout { get; set; }
protected PHP.Core.ScriptContext context = PHP.Core.ScriptContext.CurrentContext;
public virtual void Execute()
{
}
public void WriteLiteral(string textToAppend)
{
if (String.IsNullOrEmpty(textToAppend))
{
return;
}
PHP.Core.ScriptContext.Echo(textToAppend, context);
}
public void Write(object value)
{
if ((value == null))
{
return;
}
WriteLiteral(Convert.ToString(value, CultureInfo.InvariantCulture));
}
}
/// <summary>
/// Provides a base implementation of a template with a model.
/// </summary>
public class PhpRazorTemplateBase<T> : PhpRazorTemplateBase
{
#region Fields
private object model;
#endregion
#region Constructor
/// <summary>
/// Initialises a new instance of <see cref="TemplateBase{T}"/>
/// </summary>
protected PhpRazorTemplateBase()
{
var type = GetType();
}
#endregion
#region Methods
/// <summary>
/// Gets whether this template has a dynamic model.
/// </summary>
protected bool HasDynamicModel { get; private set; }
/// <summary>
/// Gets or sets the model.
/// </summary>
public virtual T Model
{
get { return (T)model; }
set
{
if (HasDynamicModel)
model = new RazorDynamicObject { Model = value };
else
model = value;
}
}
#endregion
#region Types
/// <summary>
/// Defines a dynamic object.
/// </summary>
class RazorDynamicObject : DynamicObject
{
#region Properties
/// <summary>
/// Gets or sets the model.
/// </summary>
public object Model { get; set; }
#endregion
#region Methods
/// <summary>
/// Gets the value of the specified member.
/// </summary>
/// <param name="binder">The current binder.</param>
/// <param name="result">The member result.</param>
/// <returns>True.</returns>
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = Model
.GetType()
.InvokeMember(
binder.Name,
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
null,
Model,
null);
return (result != null);
}
#endregion
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.