internal void PopContext() { if( _templateContextStack.Count > 0 ) _currentTemplateContext = _templateContextStack.Pop() as TemplateContext; else _currentTemplateContext = null; }
internal TemplateContext(TemplateContext parentTemplateContext):this(parentTemplateContext.RelativePath, parentTemplateContext.TemplateDirectory, parentTemplateContext.TemplateGeneratorHost) { //Inherit parent context too if( parentTemplateContext.Context != null ) { foreach(DictionaryEntry entry in parentTemplateContext.Context) { if( !_context.ContainsKey(entry.Key) ) _context.Add(entry.Key, entry.Value); } } }
public Template(TemplateEngine templateEngine, ITemplateGeneratorHost host, TemplateContext templateContext) { _templateEngine = templateEngine; _host = host; _templateContext = templateContext; }
public string Preview(TemplateInfo templateInfo, ITemplateGeneratorHost host, TemplateEngineSettings templateEngineSettings) { ValidationUtils.ArgumentNotNull(templateInfo, "templateInfo"); ValidationUtils.ArgumentNotNull(host, "host"); ValidationUtils.ArgumentNotNull(templateEngineSettings, "templateEngineSettings"); log.Debug(string.Format("Running template {0} from {1}", templateInfo.Name, templateInfo.Directory)); string result; try { _templateEngineSettings = templateEngineSettings; string relativePath = ""; string templateFile = Path.Combine(templateInfo.Directory, "preview.template"); TemplateContext templateContext = new TemplateContext(relativePath, templateInfo.Directory, host); PushContext(templateContext); result = InternalProcessTemplate(relativePath, templateFile, "preview", host); PopContext(); } catch(Exception ex) { log.Error("Failed generating template preview", ex); throw ex; } //log.Debug(result); return result; }
void PushContext(TemplateContext templateContext) { if( _currentTemplateContext != null ) _templateContextStack.Push(_currentTemplateContext); _currentTemplateContext = templateContext; }
/// <summary> /// Called from a Template class. /// </summary> /// <param name="templateFileName"></param> /// <param name="outFileName"></param> internal void ProcessTemplate(string templateFileName, string outFileName) { TemplateContext templateContext = new TemplateContext(_currentTemplateContext); PushContext(templateContext); string templateFile = Path.Combine(_currentTemplateContext.TemplateDirectory, templateFileName); ProcessTemplate(_currentTemplateContext.RelativePath, templateFile, outFileName, _currentTemplateContext.TemplateGeneratorHost); PopContext(); }
private void ProcessTemplates(string relativePath, string srcDirectory, ITemplateGeneratorHost host) { //foreach(string file in Directory.GetFiles(srcDirectory, "*.config.template")) foreach(string file in Directory.GetFiles(srcDirectory)) { if( Path.GetExtension(file).ToLower() == ".template" ) { string outFileName = Path.GetFileName(file); outFileName = outFileName.Substring(0, outFileName.Length - ".template".Length); TemplateContext templateContext = new TemplateContext(relativePath, srcDirectory, host); PushContext(templateContext); ProcessTemplate(relativePath, file, outFileName, host); PopContext(); } else if( Path.GetExtension(file).ToLower() == ".subtemplate" ) { //ignore subtemplates } else { host.AddFile(relativePath, file); } } foreach(string dir in Directory.GetDirectories(srcDirectory)) { DirectoryInfo info = new DirectoryInfo(dir); if( info.Name.StartsWith("_") ) continue;//Ignore special folders string relativePathTmp = Path.Combine(relativePath, info.Name); ProcessTemplates(relativePathTmp, dir, host); } }