private void CacheTemplateHelper(ICompiledTemplate template, ITemplateKey templateKey, Type modelTypeKey) { var uniqueKey = templateKey.GetUniqueKeyString(); _cache.AddOrUpdate(uniqueKey, key => { // new item added _assemblies.Add(template.TemplateAssembly); var dict = new ConcurrentDictionary<Type, ICompiledTemplate>(); dict.AddOrUpdate(modelTypeKey, template, (t, old) => { throw new Exception("Expected the dictionary to be empty."); }); return dict; }, (key, dict) => { dict.AddOrUpdate(modelTypeKey, t => { // new item added (template was not compiled with the given type). _assemblies.Add(template.TemplateAssembly); return template; }, (t, old) => { // item was already added before return template; }); return dict; }); }
private void CacheTemplateHelper(ICompiledTemplate template, ITemplateKey templateKey, Type modelTypeKey) { var uniqueKey = templateKey.GetUniqueKeyString(); _cache.AddOrUpdate(uniqueKey, key => { // new item added _assemblies.Add(template.TemplateAssembly); var dict = new ConcurrentDictionary <Type, ICompiledTemplate>(); dict.AddOrUpdate(modelTypeKey, template, (t, old) => { throw new Exception("Expected the dictionary to be empty."); }); return(dict); }, (key, dict) => { dict.AddOrUpdate(modelTypeKey, t => { // new item added (template was not compiled with the given type). _assemblies.Add(template.TemplateAssembly); return(template); }, (t, old) => { // item was already added before return(template); }); return(dict); }); }
/// <summary> /// 缓存模板助手 /// </summary> /// <param name="template">模板</param> /// <param name="templateKey">模板键</param> /// <param name="modelTypeKey">模板类型键</param> private void CacheTemplateHelper(ICompiledTemplate template, ITemplateKey templateKey, Type modelTypeKey) { var uniqueKey = templateKey.GetUniqueKeyString(); _cache.AddOrUpdate(uniqueKey, key => { // 添加新项 _assemblies.Add(template.TemplateAssembly); var dict = new ConcurrentDictionary <Type, ICompiledTemplate>(); dict.AddOrUpdate(modelTypeKey, template, (t, old) => template); return(dict); }, (key, dict) => { dict.AddOrUpdate(modelTypeKey, t => { // 添加新条目(模板没有编译给定类型) _assemblies.Add(template.TemplateAssembly); return(template); }, (t, old) => { // 项目已经添加 return(template); }); return(dict); }); }
private Template(string templateFile, string destinationPath, bool onlyBody) { _destinationPath = destinationPath; _fileName = templateFile; _templateParser = WebAppConfig.ViewEngines.Find(templateFile).Parser; string contents = TemplateUtil.ReadTemplateContents(_fileName, _destinationPath); Match matchTitle = Regex.Match(contents, @"<title\s*>(?<title>.*?)</title>"); Match matchHead = Regex.Match(contents, @"<!--\s*#STARTCOPY#\s*-->(?<text>.*?)<!--\s*#ENDCOPY#\s*-->", RegexOptions.IgnoreCase | RegexOptions.Singleline); if (matchTitle.Success) { _pageTitle = CompileTemplate(matchTitle.Groups["title"].Value); } if (matchHead.Success) { if (_headSections == null) { _headSections = new List <ICompiledTemplate>(); } _headSections.Add(CompileTemplate(matchHead.Groups["text"].Value)); } if (onlyBody) { contents = TemplateUtil.ExtractBody(contents); } _parsedTemplate = CompileTemplate(contents); _parsedTemplate.FileName = templateFile; }
/// <summary> /// Try to retrieve a template from the cache. See <see cref="ICachingProvider.TryRetrieveTemplate"/>. /// </summary> /// <param name="templateKey"></param> /// <param name="modelType"></param> /// <param name="compiledTemplate"></param> /// <returns></returns> public bool TryRetrieveTemplate(ITemplateKey templateKey, Type modelType, out ICompiledTemplate compiledTemplate) { compiledTemplate = null; if (templateKey == null) { return(false); } var uniqueKey = templateKey.GetUniqueKeyString(); if (string.IsNullOrEmpty(uniqueKey)) { return(false); } var modelTypeKey = GetModelTypeKey(modelType); ConcurrentDictionary <Type, ICompiledTemplate> dict; if (!_cache.TryGetValue(uniqueKey, out dict)) { return(false); } if (modelTypeKey == null) { return(false); } return(dict.TryGetValue(modelTypeKey, out compiledTemplate)); }
/// <summary>Runs the specified template and returns the result.</summary> /// <param name="template">The template to run.</param> /// <param name="writer"></param> /// <param name="model"></param> /// <param name="configTemplateData"></param> /// <returns>The string result of the template.</returns> public void RunTemplate(ICompiledTemplate template, TextWriter writer, object model, Action <dynamic> configTemplateData) { if (template == null) { throw new ArgumentNullException(nameof(template)); } this.CreateTemplate(template, model, configTemplateData).Run(writer); }
/// <summary> /// Creates an instance of <see cref="ITemplate" /> from the specified string template. /// </summary> /// <param name="template">The compiled template.</param> /// <param name="model">The model instance or NULL if no model exists.</param> /// <param name="configTemplateData"></param> /// <returns>An instance of <see cref="ITemplate" />.</returns> internal virtual ITemplate CreateTemplate(ICompiledTemplate template, object model, Action <dynamic> configTemplateData) { var instance = (ITemplate)Activator.CreateInstance(template.TemplateType); instance.InternalTemplateService = new InternalTemplateService(this); instance.SetModel(model); configTemplateData?.Invoke(instance.TemplateData); return(instance); }
public RenamingProgressDialog (ICompiledTemplate<DatabaseTrackInfo> template) { if (template == null) { throw new ArgumentNullException ("template"); } this.template = template; this.Build (); buttonCancel.Clicked += OnButtonCancelClicked; buttonOk.Clicked += OnButtonOkClicked; }
/// <summary> /// Runs the specified template and returns the result. /// </summary> /// <param name="template">The template to run.</param> /// <param name="writer"></param> /// <param name="model"></param> /// <param name="viewBag">The ViewBag contents or NULL for an initially empty ViewBag.</param> /// <returns>The string result of the template.</returns> public async Task RunTemplate(ICompiledTemplate template, System.IO.TextWriter writer, object model, DynamicViewBag viewBag) { if (template == null) { throw new ArgumentNullException("template"); } var instance = CreateTemplate(template, model, viewBag); await instance.Run(CreateExecuteContext(), writer); }
internal virtual ITemplate CreateTemplate(ICompiledTemplate template, object model, DynamicViewBag viewbag) { var context = CreateInstanceContext(template.TemplateType); ITemplate instance = _config.Activator.CreateInstance(context); instance.InternalTemplateService = new InternalTemplateService(this, template.Key); instance.TemplateService = new TemplateService(_cached); instance.RazorEngine = _cached; instance.SetData(model, viewbag); return(instance); }
public bool TryRetrieveTemplate(ITemplateKey templateKey, Type modelType, out ICompiledTemplate compiledTemplate) { bool b = inner.TryRetrieveTemplate(templateKey, modelType, out compiledTemplate); if (application.debugMode) { inner.InvalidateCache(templateKey); } return(b); }
internal virtual ITemplate CreateTemplate(ICompiledTemplate template, object model, DynamicViewBag viewbag) { var context = CreateInstanceContext(template.TemplateType); ITemplate instance = _config.Activator.CreateInstance(context); instance.InternalTemplateService = new InternalTemplateService(this, template.Key); #pragma warning disable 0618 // Backwards Compat. instance.TemplateService = new TemplateService(_cached); #pragma warning restore 0618 // Backwards Compat. instance.Razor = _cached; instance.SetData(model, viewbag); return instance; }
/// <summary> /// 尝试检索模板 /// </summary> /// <param name="key">键名</param> /// <param name="modelType">模型名称</param> /// <param name="template">模板</param> /// <returns>检索到返回true,否则返回false</returns> public bool TryRetrieveTemplate(ITemplateKey key, Type modelType, out ICompiledTemplate template) { template = null; var uniqueKey = key.GetUniqueKeyString(); var modelTypeKey = GetModelTypeKey(modelType); if (!_cache.TryGetValue(uniqueKey, out ConcurrentDictionary <Type, ICompiledTemplate> dict)) { return(false); } return(dict.TryGetValue(modelTypeKey, out template)); }
internal virtual ITemplate CreateTemplate(ICompiledTemplate template, object model) { var context = CreateInstanceContext(template.TemplateType); ITemplate instance = _config.Activator.CreateInstance(context); instance.InternalTemplateService = new InternalTemplateService(this, template.Key); instance.TemplateService = new TemplateService(_cached); instance.RazorEngine = _cached; if (model != null) { instance.SetModel(model); } return instance; }
internal virtual ITemplate CreateTemplate(ICompiledTemplate template, object model, DynamicViewBag viewbag) { var context = CreateInstanceContext(template.TemplateType); ITemplate instance = _config.Activator.CreateInstance(context); instance.InternalTemplateService = new InternalTemplateService(this, template.Key); #pragma warning disable 0618 // Backwards Compat. instance.TemplateService = new TemplateService(_cached); #pragma warning restore 0618 // Backwards Compat. instance.Razor = _cached; instance.SetData(model, viewbag); return(instance); }
public void Render(ITemplateContext context) { object value = context.GetValue(this.templateName); if (value != null) { ICompiledTemplate compiledTemplate = context.GetTemplate(this.templateName); if (compiledTemplate != null) { context.Write(compiledTemplate.Render(value, context.TemplateLocator)); } } }
internal virtual ITemplate CreateTemplate(ICompiledTemplate template, object model) { var context = CreateInstanceContext(template.TemplateType); ITemplate instance = _config.Activator.CreateInstance(context); instance.InternalTemplateService = new InternalTemplateService(this, template.Key); instance.TemplateService = new TemplateService(_cached); instance.RazorEngine = _cached; if (model != null) { instance.SetModel(model); } return(instance); }
/// <summary> /// Caches a template. See <see cref="ICachingProvider.CacheTemplate"/>. /// </summary> /// <param name="template"></param> /// <param name="templateKey"></param> public void CacheTemplate(ICompiledTemplate template, ITemplateKey templateKey) { var modelTypeKey = GetModelTypeKey(template.ModelType); CacheTemplateHelper(template, templateKey, modelTypeKey); var typeArgs = template.TemplateType.BaseType.GetGenericArguments(); if (typeArgs.Length > 0) { var alternativeKey = GetModelTypeKey(typeArgs[0]); if (alternativeKey != modelTypeKey) { // could be a template with an @model directive. CacheTemplateHelper(template, templateKey, typeArgs[0]); } } }
public void RunTemplate(ICompiledTemplate template, System.IO.TextWriter writer, object model, DynamicViewBag viewBag) #endif { if (template == null) { throw new ArgumentNullException("template"); } var instance = CreateTemplate(template, model, viewBag); #if RAZOR4 await instance.Run(CreateExecuteContext(), writer); #else instance.Run(CreateExecuteContext(), writer); #endif }
/// <summary> /// Try to retrieve a template from the cache. See <see cref="ICachingProvider.TryRetrieveTemplate"/>. /// If cached template has different modification time, then the cache is invalidated. /// </summary> /// <param name="templateKey"></param> /// <param name="modelType"></param> /// <param name="compiledTemplate"></param> /// <returns></returns> public new bool TryRetrieveTemplate(ITemplateKey templateKey, Type modelType, out ICompiledTemplate compiledTemplate) { ICompiledTemplate foundTemplate; if (base.TryRetrieveTemplate(templateKey, modelType, out foundTemplate)) { if (KeysHaveEqualModifiedTime(foundTemplate.Key, templateKey)) { compiledTemplate = foundTemplate; return true; } InvalidateCache(foundTemplate.Key); } compiledTemplate = null; return false; }
/// <summary> /// 缓存模板 /// </summary> /// <param name="template">模板</param> /// <param name="key">模板键</param> public void CacheTemplate(ICompiledTemplate template, ITemplateKey key) { var modelTypeKey = GetModelTypeKey(template.ModelType); CacheTemplateHelper(template, key, modelTypeKey); var typeArgs = template.TemplateType.BaseType.GetGenericArguments(); if (typeArgs.Length > 0) { var alternativeKey = GetModelTypeKey(typeArgs[0]); if (alternativeKey != modelTypeKey) { // 可能是模板 @model 指令。 CacheTemplateHelper(template, key, typeArgs[0]); } } }
protected override CompiledTemplate OnEvalParseFile(ExpressionParser parser, TemplateParser templateParser, string fileName, TemplateToken token, IParserContext context, out Dictionary <string, IValueWithType> parameters) { TemplateParserContext parserContext = (TemplateParserContext)context; TemplateToken.ParameterizedExpression pExpr = token.ExtractParameters(); parameters = new Dictionary <string, IValueWithType>(StringComparer.InvariantCultureIgnoreCase); foreach (KeyValuePair <string, string> var in pExpr.Parameters) { parameters[var.Key] = parser.Evaluate(var.Value, context, token.TokenPosition); } ICompiledTemplate subTemplate = parserContext.Template.CompileSubTemplate(parser.Evaluate <string>(pExpr.MainExpression, context, token.TokenPosition)); //TODO : fix this. It should be possible to include templates with different syntax return(((ViciCompiledTemplate)subTemplate).CompiledTemplate); }
private static ICompiledTemplate GetFromCache(string filePath) { string cacheKey = "{0}::{1}".FormatString(CacheKey, filePath); ICache cache = Container.Get <ICache>(); ITemplateEngine engine = Container.Get <ITemplateEngine>(); ICompiledTemplate compiledTemplate = cache.Get <ICompiledTemplate>(cacheKey); if (compiledTemplate == null) { compiledTemplate = engine.CompileFile(filePath); cache.Set(cacheKey, compiledTemplate, filePath); } return(compiledTemplate); }
/// <summary> /// Caches a template. See <see cref="ICachingProvider.CacheTemplate"/>. /// </summary> /// <param name="template"></param> /// <param name="templateKey"></param> public void CacheTemplate(ICompiledTemplate template, ITemplateKey templateKey) { var modelTypeKey = GetModelTypeKey(template.ModelType); if (_registerForCleanup != null) { _registerForCleanup(template.CompilationData.TmpFolder); } CacheTemplateHelper(template, templateKey, modelTypeKey); var typeArgs = template.TemplateType.BaseType.GetGenericArguments(); if (typeArgs.Length > 0) { var alternativeKey = GetModelTypeKey(typeArgs[0]); if (alternativeKey != modelTypeKey) { // could be a template with an @model directive. CacheTemplateHelper(template, templateKey, typeArgs[0]); } } }
public IVisualStudioProject Render <T>(CodeGenSettings settings, object template, IEnumerable <T> collection, IVisualStudioProject projectFile) { ICompiledTemplate _template = (ICompiledTemplate)template; IEnumerable <DatabaseTable> tables = (IEnumerable <DatabaseTable>)collection; IClassMetadata classmeta = _template; //Creating project specific variables string projectNamespace = string.Format("{0}.{1}", settings.SolutionNamespace, classmeta.ProjectNamespace); string projectDirectory = string.Format(@"{0}\{1}", settings.CodeGenerationDirectory, projectNamespace); //Set the project properties. These should never change. ClassesLogic.SetProjectProperties(settings, classmeta, projectFile, projectDirectory); string fileDirectory = (string.IsNullOrEmpty(classmeta.ClassFilePath) ? projectDirectory : string.Format(@"{0}\{1}", projectDirectory, classmeta.ClassFilePath)); //create the project and project sub directory CreateProjectAndClassDirectories(projectDirectory, fileDirectory); string fileNamespace = projectNamespace + ClassesLogic.AddFolderNamespaces(classmeta); //Render out the Templates by passing in the collection of Tables foreach (DatabaseTable table in tables) { string filename = table.Name + classmeta.FilenameAppending; string projectClassPath = (string.IsNullOrEmpty(classmeta.ClassFilePath) ? filename : classmeta.ClassFilePath + filename); projectFile.Classes.Add(new ProjectArtifact(projectClassPath + ".cs", true)); string fullQualifiedPathOfClass = string.Format(@"{0}\{1}.cs", fileDirectory, filename); string content = _template.RenderTemplate(table, settings); //Write out Generated code content = SetNamespaceAndClass(content, filename, fileNamespace); File.WriteAllText(fullQualifiedPathOfClass, content); } return(projectFile); }
/// <summary> /// Try to retrieve a template from the cache. See <see cref="ICachingProvider.TryRetrieveTemplate"/>. /// If cached template has different modification time, then the cache is invalidated. /// </summary> /// <param name="templateKey"></param> /// <param name="modelType"></param> /// <param name="compiledTemplate"></param> /// <returns></returns> public new bool TryRetrieveTemplate(ITemplateKey templateKey, Type modelType, out ICompiledTemplate compiledTemplate) { ICompiledTemplate foundTemplate; if (base.TryRetrieveTemplate(templateKey, modelType, out foundTemplate)) { if (KeysHaveEqualModifiedTime(foundTemplate.Key, templateKey)) { compiledTemplate = foundTemplate; return(true); } InvalidateCache(foundTemplate.Key); } compiledTemplate = null; return(false); }
/// <summary> /// Runs the specified template and returns the result. /// </summary> /// <param name="template">The template to run.</param> /// <param name="writer"></param> /// <param name="model"></param> /// <param name="viewBag">The ViewBag contents or NULL for an initially empty ViewBag.</param> /// <returns>The string result of the template.</returns> #if RAZOR4 public async Task RunTemplate(ICompiledTemplate template, System.IO.TextWriter writer, object model, DynamicViewBag viewBag)
string ITemplateParser.Render(ICompiledTemplate compiledTemplate, IParserContext parserContext) { return(Render(((ViciCompiledTemplate)compiledTemplate).CompiledTemplate, parserContext)); }
/// <summary> /// Caches a template. See <see cref="ICachingProvider.CacheTemplate"/>. /// </summary> /// <param name="template"></param> /// <param name="templateKey"></param> public void CacheTemplate(ICompiledTemplate template, ITemplateKey templateKey) { inner.CacheTemplate(template, templateKey); }
/// <summary> /// Try to retrieve a template from the cache. See <see cref="ICachingProvider.TryRetrieveTemplate"/>. /// </summary> /// <param name="templateKey"></param> /// <param name="modelType"></param> /// <param name="compiledTemplate"></param> /// <returns></returns> public bool TryRetrieveTemplate(ITemplateKey templateKey, Type modelType, out ICompiledTemplate compiledTemplate) { return inner.TryRetrieveTemplate(templateKey, modelType, out compiledTemplate); }
/// <summary> /// Try to retrieve a template from the cache. See <see cref="ICachingProvider.TryRetrieveTemplate"/>. /// </summary> /// <param name="templateKey"></param> /// <param name="modelType"></param> /// <param name="compiledTemplate"></param> /// <returns></returns> public bool TryRetrieveTemplate(ITemplateKey templateKey, Type modelType, out ICompiledTemplate compiledTemplate) { compiledTemplate = null; var uniqueKey = templateKey.GetUniqueKeyString(); var modelTypeKey = GetModelTypeKey(modelType); ConcurrentDictionary<Type, ICompiledTemplate> dict; if (!_cache.TryGetValue(uniqueKey, out dict)) { return false; } return dict.TryGetValue(modelTypeKey, out compiledTemplate); }
public void CacheTemplate(ICompiledTemplate template, ITemplateKey templateKey) { inner.CacheTemplate(template, templateKey); }
private Template(string templateFile, string destinationPath, bool onlyBody) { _destinationPath = destinationPath; _fileName = templateFile; _templateParser = WebAppConfig.ViewEngines.Find(templateFile).Parser; string contents = TemplateUtil.ReadTemplateContents(_fileName, _destinationPath); Match matchTitle = Regex.Match(contents, @"<title\s*>(?<title>.*?)</title>"); Match matchHead = Regex.Match(contents, @"<!--\s*#STARTCOPY#\s*-->(?<text>.*?)<!--\s*#ENDCOPY#\s*-->", RegexOptions.IgnoreCase | RegexOptions.Singleline); if (matchTitle.Success) _pageTitle = CompileTemplate(matchTitle.Groups["title"].Value); if (matchHead.Success) { if (_headSections == null) _headSections = new List<ICompiledTemplate>(); _headSections.Add(CompileTemplate(matchHead.Groups["text"].Value)); } if (onlyBody) contents = TemplateUtil.ExtractBody(contents); _parsedTemplate = CompileTemplate(contents); _parsedTemplate.FileName = templateFile; }
public TemplateContext(ICompiledTemplate compiledTemplate) { this.CompiledTemplate = compiledTemplate; this.Results = CompilationResults.Success(); }
public void CacheTemplate(ICompiledTemplate template, ITemplateKey key) { _proxy.CacheTemplate(template, key); }
public bool TryRetrieveTemplate(ITemplateKey key, Type modelType, out ICompiledTemplate template) { return(_proxy.TryRetrieveTemplate(key, null, out template)); }
public void RunTemplate(ICompiledTemplate template, System.IO.TextWriter writer, object model, DynamicViewBag viewBag) #endif { if (template == null) throw new ArgumentNullException("template"); var instance = CreateTemplate(template, model, viewBag); #if RAZOR4 await instance.Run(CreateExecuteContext(), writer); #else instance.Run(CreateExecuteContext(), writer); #endif }