public void RegisterHelper(string helperName, HandlebarsBlockHelper helperFunction) { lock (typeof(Handlebars)) { _configuration.BlockHelpers.AddOrUpdate(helperName, helperFunction); } }
public static string Result() { HandlebarsBlockHelper _stringEqualityBlockHelper = (TextWriter output, HelperOptions options, dynamic context, object[] arguments) => { if (arguments.Length != 2) { throw new HandlebarsException("{{StringEqualityBlockHelper}} helper must have exactly two argument"); } string left = arguments[0] as string; string right = arguments[1] as string; if (left == right) { options.Template(output, null); } else { options.Inverse(output, null); } }; Handlebars.RegisterHelper("StringEqualityBlockHelper", _stringEqualityBlockHelper); var animals = new Dictionary <string, string>() { { "Fluffy", "cat" }, { "Fido", "dog" }, { "Chewy", "hamster" } }; Func <object, string> compiledTemplate = Handlebars.Compile(source); string templateOutput = compiledTemplate(animals); return(templateOutput); }
public void RegisterHelper(string helperName, HandlebarsBlockHelper helperFunction) { lock (_configuration) { _configuration.BlockHelpers.AddOrUpdate(helperName, helperFunction); } }
/// <summary> /// Allows to intercept calls to missing helpers. /// <para>For Handlebarsjs docs see: https://handlebarsjs.com/guide/hooks.html#helpermissing</para> /// </summary> /// <param name="configuration"></param> /// <param name="helperMissing">Delegate that returns interceptor for <see cref="HandlebarsReturnHelper"/> and <see cref="HandlebarsHelper"/></param> /// <param name="blockHelperMissing">Delegate that returns interceptor for <see cref="HandlebarsBlockHelper"/></param> /// <returns></returns> public static HandlebarsConfiguration RegisterMissingHelperHook( this HandlebarsConfiguration configuration, HandlebarsReturnWithOptionsHelper helperMissing = null, HandlebarsBlockHelper blockHelperMissing = null ) { return(configuration.RegisterMissingHelperHook( helperMissing != null ? new DelegateReturnHelperWithOptionsDescriptor("helperMissing", helperMissing) : null, blockHelperMissing != null ? new DelegateBlockHelperDescriptor("blockHelperMissing", blockHelperMissing) : null )); }
public void RegisterHelper(string helperName, HandlebarsBlockHelper helperFunction) { if (helperFunction == null) { throw new ArgumentNullException(nameof(helperFunction)); } RegisterHelper(helperName, (configuration, output, options, context, arguments) => { helperFunction(output, options, context, arguments); }); }
public static void RegisterHelper(string helperName, HandlebarsBlockHelper helperFunction) { if (_configuration.BlockHelpers.ContainsKey(helperName) == false) { lock (typeof(Handlebars)) { if (_configuration.BlockHelpers.ContainsKey(helperName) == false) { _configuration.BlockHelpers.Add(helperName, helperFunction); } } } }
public static void RegisterHelper(string helperName, HandlebarsBlockHelper helperFunction) { if(_configuration.BlockHelpers.ContainsKey(helperName) == false) { lock(typeof(Handlebars)) { if(_configuration.BlockHelpers.ContainsKey(helperName) == false) { _configuration.BlockHelpers.Add(helperName, helperFunction); } } } }
public static void RegisteHelpers() { HandlebarsBlockHelper pagination = (output, options, context, arguments) => { // helper args format: collection pagesize optional<firstpage_size> List <OrderInvoiceItem> enumerable = arguments[0] as List <OrderInvoiceItem>; int pageSize = int.Parse(arguments[1] as string); int firstPageSize = arguments.Length > 2 ? int.Parse(arguments[2] as string) : pageSize; Page <OrderInvoiceItem> firstPage = new Page <OrderInvoiceItem>( true, false, enumerable.Take(firstPageSize).ToImmutableList()); ImmutableList <Page <OrderInvoiceItem> > pages = enumerable .Skip(firstPageSize) .Split(pageSize) .Select((p, index) => new Page <OrderInvoiceItem>(false, false, p)) .ToImmutableList(); pages.Last().IsLast = true; ImmutableList <Page <OrderInvoiceItem> > result = ImmutableList <Page <OrderInvoiceItem> > .Empty .Add(firstPage) .AddRange(pages); result.ForEach(p => options.Template(output, p)); }; HandlebarsDotNet.Handlebars.RegisterHelper("paging", pagination); HandlebarsDotNet.Handlebars.RegisterHelper("date", (writer, context, args) => { DateTime dt = (DateTime)args[0]; var format = args[1] as string; writer.WriteSafeString(dt.ToString(format)); }); HandlebarsDotNet.Handlebars.RegisterHelper("price", (writer, context, args) => { string formated = args[0].GetType() switch { {} d when d == typeof(double) => ((double)args[0]).ToString("0.00"), {} i when i == typeof(int) => ((int)args[0]).ToString("0.00"), {} dd when dd == typeof(decimal) => ((decimal)args[0]).ToString("0.00"), _ => args[0].ToString() }; writer.WriteSafeString(formated); });
public static void RegisterHelper(string helperName, HandlebarsBlockHelper helperFunction) { _singleton.RegisterHelper(helperName, helperFunction); }
public static void RegisterHelper(string helperName, HandlebarsBlockHelper helperFunction) { Instance.RegisterHelper(helperName, helperFunction); }
public static bool GenerateWebsiteFull(Website website) { var assembly = typeof(Generators.CoreApiGenerator).GetTypeInfo().Assembly; var resources = assembly.GetManifestResourceNames(); using (ZipFile zipFile = ZipFile.Read(assembly.GetManifestResourceStream("site_generator_lib.Templates.dotnetcore.dotnetcore_template.zip"))) { FileInfo fileInfo = new FileInfo(assembly.Location); string serverPath = Path.Combine(fileInfo.DirectoryName, $"output{DateTime.Now.ToString("_ddMMyyyy_HHmmss")}", "server"); zipFile.ExtractAll(serverPath, ExtractExistingFileAction.OverwriteSilently); foreach (var entityModel in website.Entities) { var model = new { Name = entityModel.Name, Fields = entityModel.Fields.Select(field => new { Type = Shared.TypeConverter.PostgresToCsharp(field.Type), Name = field.Name }).ToArray() }; HandlebarsBlockHelper _propertyList = (TextWriter output, HelperOptions options, dynamic context, object[] arguments) => { var enumList = arguments[0] as object[]; for (int i = 0; i < enumList.Length; i++) { string text = $"{((dynamic)(enumList[i])).Type} _{((dynamic)(enumList[i])).Name}"; if (i < enumList.Length - 1) { text += ","; } output.WriteLine(text); } }; Handlebars.RegisterHelper("prop_list", _propertyList); HandlebarsBlockHelper _addModelList = (TextWriter output, HelperOptions options, dynamic context, object[] arguments) => { var enumList = arguments[0] as object[]; for (int i = 0; i < enumList.Length; i++) { string text = $"addModel.{((dynamic)(enumList[i])).Name}"; if (i < enumList.Length - 1) { text += ","; } output.WriteLine(text); } }; Handlebars.RegisterHelper("addmodel_list", _addModelList); // Build Domain Layer Directory.CreateDirectory(Path.Combine(serverPath, "source", "Domain", entityModel.Name)); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.domainentity.t", $"{Path.Combine(serverPath, "source", "Domain", $"{entityModel.Name}")}\\{entityModel.Name}Entity.cs", model); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.domainentityfactory.t", $"{Path.Combine(serverPath, "source", "Domain", $"{entityModel.Name}")}\\{entityModel.Name}EntityFactory.cs", model); // Build Database Layer Directory.CreateDirectory(Path.Combine(serverPath, "source", "Database", entityModel.Name)); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.dbirepository.t", $"{Path.Combine(serverPath, "source", "Database", $"{entityModel.Name}")}\\I{entityModel.Name}Repository.cs", model); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.dbrepository.t", $"{Path.Combine(serverPath, "source", "Database", $"{entityModel.Name}")}\\{entityModel.Name}Repository.cs", model); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.dbentityconfiguration.t", $"{Path.Combine(serverPath, "source", "Database", $"{entityModel.Name}")}\\{entityModel.Name}EntityConfiguration.cs", model); // Build Model Layer Directory.CreateDirectory(Path.Combine(serverPath, "source", "Model", "Models", $"{entityModel.Name}Model")); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.model.t", $"{Path.Combine(serverPath, "source", "Model", "Models", $"{entityModel.Name}Model")}\\{entityModel.Name}Model.cs", model); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.modelvalidator.t", $"{Path.Combine(serverPath, "source", "Model", "Models", $"{entityModel.Name}Model")}\\{entityModel.Name}ModelValidator.cs", model); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.addmodel.t", $"{Path.Combine(serverPath, "source", "Model", "Models", $"{entityModel.Name}Model")}\\Add{entityModel.Name}Model.cs", model); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.addmodelvalidator.t", $"{Path.Combine(serverPath, "source", "Model", "Models", $"{entityModel.Name}Model")}\\Add{entityModel.Name}ModelValidator.cs", model); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.updatemodel.t", $"{Path.Combine(serverPath, "source", "Model", "Models", $"{entityModel.Name}Model")}\\Update{entityModel.Name}Model.cs", model); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.updatemodelvalidator.t", $"{Path.Combine(serverPath, "source", "Model", "Models", $"{entityModel.Name}Model")}\\Update{entityModel.Name}ModelValidator.cs", model); // Build Application Layer Directory.CreateDirectory(Path.Combine(serverPath, "source", "Application", entityModel.Name)); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.iapplicationservice.t", $"{Path.Combine(serverPath, "source", "Application", entityModel.Name)}\\I{entityModel.Name}ApplicationService.cs", model); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.applicationservice.t", $"{Path.Combine(serverPath, "source", "Application", entityModel.Name)}\\{entityModel.Name}ApplicationService.cs", model); // Build Web Controller Layer ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcore.controller.t", $"{Path.Combine(serverPath, "source", "Web", "Controllers")}\\{entityModel.Name}Controller.cs", model); } } return(true); }
public void RegisterHelper(string helperName, HandlebarsBlockHelper helperFunction) { Configuration.BlockHelpers[helperName] = new DelegateBlockHelperDescriptor(helperName, helperFunction); }
public DelegateBlockHelperDescriptor(string name, HandlebarsBlockHelper helper) { _helper = helper; Name = name; }
public void RegisterHelper(string helperName, HandlebarsBlockHelper helperFunction) { Configuration.BlockHelpers.AddOrUpdate(helperName, n => helperFunction, (n, h) => helperFunction); }
public HandlebarHelperRegistration(string name, HandlebarsBlockHelper helper) { this.Name = name; this.BlockHelper = helper; }
public DelegateBlockHelperDescriptor(string name, HandlebarsBlockHelper helper) : base(name) => _helper = helper;
public static bool GenerateWebsite(Website website) { var assembly = typeof(Generators.CoreApiGenerator).GetTypeInfo().Assembly; var resources = assembly.GetManifestResourceNames(); using (ZipFile zipFile = ZipFile.Read(assembly.GetManifestResourceStream("site_generator_lib.Templates.dotnetcoreangular.WebApplication.zip"))) { FileInfo fileInfo = new FileInfo(assembly.Location); string serverPath = Path.Combine(fileInfo.DirectoryName, $"output{DateTime.Now.ToString("_ddMMyyyy_HHmmss")}", "server"); zipFile.ExtractAll(serverPath, ExtractExistingFileAction.OverwriteSilently); var fullModel = new { Entities = website.Entities }; ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcoreangular.startup.t", $"{Path.Combine(serverPath, "WebApplication")}\\Startup.cs", fullModel); //Cient Generation ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcoreangular.client.nav-menu.t", $"{Path.Combine(serverPath, "WebApplication", "ClientApp", "src", "app", "nav-menu")}\\nav-menu.component.html", fullModel); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcoreangular.client.module.t", $"{Path.Combine(serverPath, "WebApplication", "ClientApp", "src", "app")}\\app.module.ts", fullModel); foreach (var entityModel in website.Entities) { var model = new { Name = entityModel.Name, Fields = entityModel.Fields.Select(field => new { Type = Shared.TypeConverter.PostgresToCsharp(field.Type), Name = field.Name }).ToArray() }; HandlebarsBlockHelper _propertyList = (TextWriter output, HelperOptions options, dynamic context, object[] arguments) => { var enumList = arguments[0] as object[]; for (int i = 0; i < enumList.Length; i++) { string text = $"{((dynamic)(enumList[i])).Type} _{((dynamic)(enumList[i])).Name}"; if (i < enumList.Length - 1) { text += ","; } output.WriteLine(text); } }; Handlebars.RegisterHelper("prop_list", _propertyList); HandlebarsBlockHelper _addModelList = (TextWriter output, HelperOptions options, dynamic context, object[] arguments) => { var enumList = arguments[0] as object[]; for (int i = 0; i < enumList.Length; i++) { string text = $"addModel.{((dynamic)(enumList[i])).Name}"; if (i < enumList.Length - 1) { text += ","; } output.WriteLine(text); } }; Handlebars.RegisterHelper("addmodel_list", _addModelList); // Build Database Layer Directory.CreateDirectory(Path.Combine(serverPath, "WebApplication", "Database", entityModel.Name)); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcoreangular.dbentitycontext.t", $"{Path.Combine(serverPath, "WebApplication", "Database")}\\{entityModel.Name}DbContext.cs", model); // Build Model Layer Directory.CreateDirectory(Path.Combine(serverPath, "WebApplication", "Model", "Models", $"{entityModel.Name}Model")); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcoreangular.model.t", $"{Path.Combine(serverPath, "WebApplication", "Model", "Models")}\\{entityModel.Name}.cs", model); // Build Web Controller Layer ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcoreangular.controller.t", $"{Path.Combine(serverPath, "WebApplication", "Controllers")}\\{entityModel.Name}Controller.cs", model); // Client ////////////////// Directory.CreateDirectory(Path.Combine(serverPath, "WebApplication", "ClientApp", "src", "app", entityModel.Name)); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcoreangular.client.component-ts.t", $"{Path.Combine(serverPath, "WebApplication", "ClientApp", "src", "app", entityModel.Name)}\\{entityModel.Name}.component.ts", model); ParseGenerateFileTemplate("site_generator_lib.Templates.dotnetcoreangular.client.component-html.t", $"{Path.Combine(serverPath, "WebApplication", "ClientApp", "src", "app", entityModel.Name)}\\{entityModel.Name}.component.html", model); } } return(true); }
public TemplateEngine(IEnumerable <string> templates, IDictionary <string, string> templateArguments, ILogger logger, Models.ViewModel model) { this.logger = logger; this.model = model; this.engine = HandlebarsDotNet.Handlebars.Create(new HandlebarsConfiguration { ThrowOnUnresolvedBindingExpression = true, }); this.engine.RegisterHelper("concat", (writer, context, args) => { writer.WriteSafeString(string.Concat(args)); }); this.engine.RegisterHelper("pascalCase", (writer, context, args) => { writer.WriteSafeString(args[0].ToString().ToPascalCase()); }); this.engine.RegisterHelper("camelCase", (writer, context, args) => { writer.WriteSafeString(args[0].ToString().ToCamelCase()); }); this.engine.RegisterHelper("replace", (writer, context, args) => { string toReplace = args[1].ToString(); string toReplaceWith = args[2].ToString(); writer.WriteSafeString(args[0].ToString().Replace(toReplace, toReplaceWith)); }); this.engine.RegisterHelper("resolve", (writer, context, args) => { string target = args[0].ToString(); var currentDirectory = Path.GetDirectoryName(model.OutputPath); System.Uri uri1; if (target.StartsWith("~/")) { uri1 = new Uri(Path.GetFullPath(Path.Combine(model.RootPath, target.Substring(2)))); } else { uri1 = new Uri(target, UriKind.Relative); } System.Uri uri2 = new Uri(currentDirectory.TrimEnd() + "\\"); Uri relativeUri = uri2.MakeRelativeUri(uri1); var path = relativeUri.ToString(); if (path[0] != '.') { path = "./" + path; } writer.WriteSafeString(path); }); HandlebarsBlockHelper ifTemplateHelper = (output, options, context, args) => { var val = this.engine.Compile("{{> " + args[0].ToString() + "}}")((object)context)?.ToString()?.Trim(); var isSet = !string.IsNullOrWhiteSpace(val); if (args.Length > 1) { isSet = val.Equals(args[1]?.ToString()); } if (isSet) { options.Template(output, context); } else { options.Inverse(output, context); } }; this.engine.RegisterHelper("ifTemplate", ifTemplateHelper); //this.engine.RegisterHelper("ifTemplateSet", (writer, context, args) => //{ // var val = this.engine.Compile("{{> " + args[0].ToString() + "}}")(context); // writer.WriteSafeString(string.IsNullOrWhiteSpace(val) ? "" : args[1]); //}); this.engine.Configuration.TextEncoder = new NullEncoder(); //engine.RegisterHelper("render", (w, c, a) => //{ // if (c is Models.TypeViewModel typeVm) // { // // this is the typerefcontext // w.WriteSafeString("Type"); // return; // } // if (c is Models.TypeReferenceModel typeRef) // { // // this is the typerefcontext // w.WriteSafeString("TypeReference_" + typeRef.ScalerType.ToString()); // return; // } //}); foreach (string templatePath in templates) { string templateContents = LoadTemplate(templatePath); ProcessTemplate(templateContents); } foreach (var a in templateArguments) { this.engine.RegisterTemplate(a.Key, a.Value); } }
public void RegisterHelper(string helperName, HandlebarsBlockHelper helperFunction) { Configuration.BlockHelpers[helperName] = helperFunction; }