private void RegisterTemplates(IHandlebars hb, Templates templates) { foreach (var(name, template) in templates) { hb.RegisterTemplate(name, new Action <TextWriter, object>(template)); } }
private void Initialize() { var assembly = Assembly.GetExecutingAssembly(); var templateNames = assembly.GetManifestResourceNames().Where(x => x.EndsWith(".hbs")); foreach (var resourceName in templateNames) { Stream stream = null; try { stream = assembly.GetManifestResourceStream(resourceName) ?? new MemoryStream(); using (var reader = new StreamReader(stream)) { stream = null; var partialTemplate = Handlebars.Compile(reader); _handlebars.RegisterTemplate(ShortTemplateName(resourceName), partialTemplate); } } finally { stream?.Dispose(); } } }
/// <summary> /// There is no callback mecanism to resolve the TemplateNames when compiling an handlebars Template /// So we have to manually lookup the file, find the dependencies and precompile them. /// /// Each template found is resolved by the ViewEngine to benefit from all caching mecanisms. /// </summary> /// <param name="controllerContext"></param> /// <param name="virtualPath"></param> private Dictionary <string, HandlebarsView> GetDependenciesFromCache(ControllerContext controllerContext, string virtualPath) { Dictionary <string, HandlebarsView> dependencies = new Dictionary <string, HandlebarsView>(); foreach (var templateName in GetViewPartialNames(virtualPath)) { //Find the partial view from cache var result = FindPartialView(controllerContext, templateName, true); if (result.View == null) { //Partial view was not in cache, make sure we update the registred template result = FindPartialView(controllerContext, templateName, false); } if (result.View is HandlebarsView view) { _handlebars.RegisterTemplate(templateName, view.CompiledTemplate); dependencies[templateName] = view; foreach (var subDependency in view.Dependencies) { dependencies[subDependency.Key] = subDependency.Value; } } } return(dependencies); }
/// <summary> /// /// </summary> /// <param name="viewName"></param> public void AddTemplate(string viewName, string key = null, string externalViewPath = null) { if (string.IsNullOrEmpty(viewName)) { return; } _hbsService.RegisterTemplate(string.IsNullOrEmpty(key) ? viewName : key, _templateResolver.Resolve(viewName, externalViewPath)); }
public bool TryRegisterPartial(IHandlebars env, string partialName, string templatePath) { if (partialName == "person") { env.RegisterTemplate("person", "{{name}}"); return(true); } return(false); }
private static bool TryRegisterPartialFile(string directory, IHandlebars env, string partialName) { var partialPath = Path.Combine(directory, $"_{partialName}.hbs"); if (!File.Exists(partialPath)) { return(false); } env.RegisterTemplate(partialName, File.ReadAllText(partialPath)); return(true); }
private void RegisterTemplate(StringBuilder sb, string currentTemplateName) { if (currentTemplateName == null) { this.template = engine.Compile(sb.ToString().Trim()); } else { engine.RegisterTemplate(currentTemplateName, sb.ToString().TrimEnd()); } sb.Clear(); }
private void RegisterTemplatesFrom(string templateRoot) { if (!Directory.Exists(templateRoot)) { throw new ArgumentException($"The templateRoot directory {templateRoot} does not exist"); } var templates = Directory.GetFiles(templateRoot, "*.html.hbs"); foreach (var template in templates) { var name = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(template)).ToLower(); var content = File.ReadAllText(template); _engine.RegisterTemplate(name, content); } }
/// <summary> /// /// </summary> /// <param name="templateResolver"></param> /// <param name="viewSettings"></param> public TemplateService(ITemplateResolver templateResolver, ViewSettings viewSettings) { _templateResolver = templateResolver; _hbsService = Handlebars.Create(); RegisterHelpers(viewSettings); _templateResolver.GetAllPartialTemplates().ForEach(template => { try { _hbsService.RegisterTemplate(template.Key, template.Value); } catch (System.Exception ex) { throw new System.Exception($"Error at template key = {template.Key}", ex); } }); }
public void StandalonePartials() { string source = "Here are:\n {{>person}} \n {{>person}} "; var template = _handlebars.Compile(source); var data = new { name = "Marc" }; var partialSource = "{{name}}"; using (var reader = new StringReader(partialSource)) { var partialTemplate = _handlebars.Compile(reader); _handlebars.RegisterTemplate("person", partialTemplate); } var result = template(data); Assert.Equal("Here are:\nMarcMarc", result); }
public static void RegisterTemplate(string templateName, Action <TextWriter, object> template) { Instance.RegisterTemplate(templateName, template); }
/// <inheritdoc /> protected override async Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context) { context.LogDebug( "Processing Handlebars {0} for {1}", string.IsNullOrEmpty(_sourceKey) ? string.Empty : ("in" + _sourceKey), input.ToSafeDisplayString()); string content; if (string.IsNullOrEmpty(_sourceKey)) { content = await input.GetContentStringAsync(); } else if (input.ContainsKey(_sourceKey)) { content = input.GetString(_sourceKey) ?? string.Empty; } else { // Don't do anything if the key doesn't exist return(input.Yield()); } IHandlebars handlebars = HandlebarsDotNet.Handlebars.Create(); // Configure if (_configure != null) { await _configure(context, input, handlebars); } // Register partials foreach ((string name, Config <string> partial) in _partials) { handlebars.RegisterTemplate(name, await partial.GetValueAsync(input, context)); } // Register helpers foreach ((string name, Config <HandlebarsHelper> helper) in _helpers) { handlebars.RegisterHelper(name, await helper.GetValueAsync(input, context)); } // Register block helpers foreach ((string name, Config <HandlebarsBlockHelper> blockHelper) in _blockHelpers) { handlebars.RegisterHelper(name, await blockHelper.GetValueAsync(input, context)); } string result = handlebars.Compile(content)(_model is null ? input.AsDynamic() : await _model.GetValueAsync(input, context)); return(string.IsNullOrEmpty(_sourceKey) ? input.Clone(await context.GetContentProviderAsync(result, MediaTypes.Html)).Yield() : input .Clone(new MetadataItems { { string.IsNullOrEmpty(_destinationKey) ? _sourceKey : _destinationKey, result } }) .Yield()); }