本文整理汇总了C#中umbraco.cms.businesslogic.macro.MacroModel类的典型用法代码示例。如果您正苦于以下问题:C# MacroModel类的具体用法?C# MacroModel怎么用?C# MacroModel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MacroModel类属于umbraco.cms.businesslogic.macro命名空间,在下文中一共展示了MacroModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Macro_Is_File_Based
public void Macro_Is_File_Based(string macroType, bool expectedResult)
{
var mType = Enum<MacroTypes>.Parse(macroType);
var model = new MacroModel("Test", "test", "", "", "", "", 0, false, false);
model.MacroType = mType; //force the type
Assert.AreEqual(expectedResult, macro.MacroIsFileBased(model));
}
示例2: Execute
public virtual string Execute(MacroModel macro, INode currentPage) {
var fileEnding = macro.ScriptName.Substring(macro.ScriptName.LastIndexOf('.')).Trim('.');
var mse = MacroScriptEngine.LoadEngineByFileExtension(fileEnding);
var vars = new SortedDictionary<string, object> {
{"currentPage", new DynamicNode(currentPage)}
};
foreach (var prop in macro.Properties) {
vars.Add(prop.Key, prop.Value);
}
mse.ScriptVariables = vars;
return mse.ExecuteFile(IOHelper.MapPath(SystemDirectories.MacroScripts + "/" + macro.ScriptName));
}
示例3: SetUserControlProperty
public void SetUserControlProperty(string val, string macroPropName, Type convertTo)
{
var ctrl = new UserControlTest();
var macroModel = new MacroModel("test", "test", "", "~/usercontrols/menu.ascx", "", "", 0, false, false);
macroModel.Properties.Add(new MacroPropertyModel(macroPropName, val));
macro.UpdateControlProperties(ctrl, macroModel);
var ctrlType = ctrl.GetType();
var prop = ctrlType.GetProperty(macroPropName);
var converted = val.TryConvertTo(convertTo);
Assert.IsTrue(converted.Success);
Assert.NotNull(prop);
Assert.AreEqual(converted.Result, prop.GetValue(ctrl));
}
示例4: Render
public static string Render(string razorScript = "", string macroScriptFileName = "", int nodeId = 0, IDictionary<string, string> macroParameters = null)
{
var macroEngine = new RazorMacroEngine();
var macro = new MacroModel();
macro.ScriptCode = razorScript;
macro.ScriptLanguage = "cshtml";
macro.ScriptName = macroScriptFileName;
var node = new umbraco.NodeFactory.Node(nodeId);
if(macroParameters != null) {
foreach(var param in macroParameters) {
macro.Properties.Add(new MacroPropertyModel(param.Key, param.Value));
}
}
return macroEngine.Execute(macro, new umbraco.NodeFactory.Node(nodeId));
}
示例5: InjectContext
public static void InjectContext(WebPage razorWebPage, MacroModel macro, INode currentPage) {
var context = HttpContext.Current;
var contextWrapper = new HttpContextWrapper(context);
//inject http context - for request response
HttpContext.Current.Trace.Write("umbracoMacro", string.Format("Loading Macro Script Context (file: {0})", macro.Name));
razorWebPage.Context = contextWrapper;
HttpContext.Current.Trace.Write("umbracoMacro", string.Format("Done Loading Macro Script Context (file: {0})", macro.Name));
//Inject Macro Model And Parameters
if (razorWebPage is IMacroContext) {
HttpContext.Current.Trace.Write("umbracoMacro", string.Format("Boxing Macro Script MacroContext (file: {0})", macro.Name));
var razorMacro = (IMacroContext)razorWebPage;
HttpContext.Current.Trace.Write("umbracoMacro", string.Format("Done Boxing Macro Script MacroContext (file: {0})", macro.Name));
HttpContext.Current.Trace.Write("umbracoMacro", string.Format("Loading Macro Script Model (file: {0})", macro.Name));
razorMacro.SetMembers(macro, currentPage);
HttpContext.Current.Trace.Write("umbracoMacro", string.Format("Done Loading Macro Script Model (file: {0})", macro.Name));
}
}
示例6: Execute
public string Execute(MacroModel macro, INode currentPage)
{
var fileLocation = string.Empty;
if (!string.IsNullOrEmpty(macro.ScriptName))
{
fileLocation = macro.ScriptName;
}
else if (!string.IsNullOrEmpty(macro.ScriptCode))
{
var code = macro.ScriptCode.Trim();
var md5 = library.md5(code);
var filename = string.Concat("inline-", md5, ".php");
fileLocation = this.CreateTemporaryFile(code, filename, true);
}
if (string.IsNullOrEmpty(fileLocation))
{
return string.Empty;
}
var builder = new StringBuilder();
using (var writer = new StringWriter(builder))
{
var contents = File.ReadAllText(IOHelper.MapPath(fileLocation));
var context = ScriptContext.CurrentContext;
context.Output = writer;
Operators.SetVariable(context, null, "model", PhpSafeType(currentPage));
PhpEval(context, Parse(contents));
}
return builder.ToString();
}
示例7: CacheMacroAsString
/// <summary>
/// Determine if macro can be cached as string
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
/// <remarks>
/// Scripts and XSLT can be generated as strings, but not controls as page events wouldn't be hit (such as Page_Load, etc)
/// </remarks>
internal static bool CacheMacroAsString(MacroModel model)
{
switch (model.MacroType)
{
case MacroTypes.XSLT:
case MacroTypes.Python:
case MacroTypes.Script:
case MacroTypes.PartialView:
return true;
case MacroTypes.UserControl:
case MacroTypes.CustomControl:
case MacroTypes.Unknown:
default:
return false;
}
}
示例8: GetMacroFile
internal static string GetMacroFile(MacroModel model)
{
switch (model.MacroType)
{
case MacroTypes.XSLT:
return string.Concat("~/xslt/", model.Xslt);
case MacroTypes.Python:
case MacroTypes.Script:
return string.Concat("~/macroScripts/", model.ScriptName);
case MacroTypes.PartialView:
return model.ScriptName; //partial views are saved with the full virtual path
case MacroTypes.UserControl:
return model.TypeName; //user controls saved with the full virtual path
case MacroTypes.CustomControl:
case MacroTypes.Unknown:
default:
return "/" + model.TypeName;
}
}
示例9: MacroIsFileBased
internal static bool MacroIsFileBased(MacroModel model)
{
return model.MacroType != MacroTypes.CustomControl && model.MacroType != MacroTypes.Unknown;
}
示例10: ExecuteRazor
public string ExecuteRazor(MacroModel macro, INode currentPage) {
var context = HttpContext.Current;
var contextWrapper = new HttpContextWrapper(context);
string fileLocation = null;
if (!string.IsNullOrEmpty(macro.ScriptName)) {
//Razor Is Already Contained In A File
if (macro.ScriptName.StartsWith("~"))
fileLocation = macro.ScriptName;
else
fileLocation = SystemDirectories.MacroScripts + "/" + macro.ScriptName;
} else if (!string.IsNullOrEmpty(macro.ScriptCode) && !string.IsNullOrEmpty(macro.ScriptLanguage)) {
//Inline Razor Syntax
fileLocation = CreateInlineRazorFile(macro.ScriptCode, macro.ScriptLanguage);
}
if (string.IsNullOrEmpty(fileLocation))
return String.Empty; //No File Location
var razorWebPage = CompileAndInstantiate(fileLocation);
HttpContext.Current.Trace.Write("umbracoMacro", string.Format("Loading Macro Script Context (file: {0})", macro.Name));
InjectContext(razorWebPage, macro, currentPage);
HttpContext.Current.Trace.Write("umbracoMacro", string.Format("Done Loading Macro Script Context (file: {0})", macro.Name));
//Output Razor To String
var output = new StringWriter();
HttpContext.Current.Trace.Write("umbracoMacro", string.Format("Executing Macro Script (file: {0})", macro.Name));
razorWebPage.ExecutePageHierarchy(new WebPageContext(contextWrapper, razorWebPage, null), output);
HttpContext.Current.Trace.Write("umbracoMacro", string.Format("Done Executing Macro Script (file: {0})", macro.Name));
return output.ToString();
}
示例11: MacroNeedsToBeClearedFromCache
/// <summary>
/// check that the file has not recently changed
/// </summary>
/// <param name="model"></param>
/// <param name="dateAddedKey"></param>
/// <param name="macroFile"></param>
/// <returns></returns>
/// <remarks>
/// The only reason this is necessary is because a developer might update a file associated with the
/// macro, we need to ensure if that is the case that the cache not be used and it is refreshed.
/// </remarks>
internal static bool MacroNeedsToBeClearedFromCache(MacroModel model, string dateAddedKey, FileInfo macroFile)
{
if (MacroIsFileBased(model))
{
var cacheResult = ApplicationContext.Current.ApplicationCache.GetCacheItem<DateTime?>(dateAddedKey);
if (cacheResult != null)
{
var dateMacroAdded = cacheResult;
if (macroFile.LastWriteTime.CompareTo(dateMacroAdded) == 1)
{
TraceInfo("renderMacro", string.Format("Macro needs to be removed from cache due to file change '{0}'.", model.CacheIdentifier));
return true;
}
}
}
return false;
}
示例12: PartialViewMacroController
public PartialViewMacroController(UmbracoContext umbracoContext, MacroModel macro, INode currentPage)
{
_umbracoContext = umbracoContext;
_macro = macro;
_currentPage = currentPage;
}
示例13: Get_Macro_File
public void Get_Macro_File(string xslt, string scriptFile, string scriptType, string scriptAssembly, string expectedResult)
{
var model = new MacroModel("Test", "test", scriptAssembly, scriptType, xslt, scriptFile, 0, false, false);
var file = macro.GetMacroFile(model);
Assert.AreEqual(expectedResult, file);
}
示例14: loadControl
/// <summary>
/// Loads a custom or webcontrol using reflection into the macro object
/// </summary>
/// <param name="fileName">The assembly to load from</param>
/// <param name="controlName">Name of the control</param>
/// <returns></returns>
public Control loadControl(string fileName, string controlName, MacroModel model, Hashtable pageElements)
{
Type type;
Assembly asm;
try
{
string currentAss = IOHelper.MapPath(string.Format("{0}/{1}.dll", SystemDirectories.Bin, fileName));
if (!File.Exists(currentAss))
return new LiteralControl("Unable to load user control because is does not exist: " + fileName);
asm = Assembly.LoadFrom(currentAss);
TraceInfo("umbracoMacro", "Assembly file " + currentAss + " LOADED!!");
}
catch
{
throw new ArgumentException(string.Format("ASSEMBLY NOT LOADED PATH: {0} NOT FOUND!!",
IOHelper.MapPath(SystemDirectories.Bin + "/" + fileName +
".dll")));
}
TraceInfo("umbracoMacro", string.Format("Assembly Loaded from ({0}.dll)", fileName));
type = asm.GetType(controlName);
if (type == null)
return new LiteralControl(string.Format("Unable to get type {0} from assembly {1}",
controlName, asm.FullName));
var control = Activator.CreateInstance(type) as Control;
if (control == null)
return new LiteralControl(string.Format("Unable to create control {0} from assembly {1}",
controlName, asm.FullName));
AddCurrentNodeToControl(control, type);
// Properties
UpdateControlProperties(type, control, model);
return control;
}
示例15: Can_Cache_As_String
public void Can_Cache_As_String(string macroType, bool expectedResult)
{
var mType = Enum<MacroTypes>.Parse(macroType);
var model = new MacroModel("Test", "test", "", "", "", "", 0, false, false);
model.MacroType = mType; //force the type
Assert.AreEqual(expectedResult, macro.CacheMacroAsString(model));
}