public TemplateMetadataEntity ParseTemplate(string template) { var templateMetadataEntity = new TemplateMetadataEntity(); List <string> templateTokens = GetTokensFromTemplate(template); foreach (string viewName in GetAllViewsFromTokens(templateTokens)) { var tokenMapEntity = new TokenMapEntity { ViewName = viewName }; foreach (string token in templateTokens.Where(s => s.StartsWith("{{" + viewName))) { string fieldName = null; string tokenKey = token.ToString().Remove("{").Remove("}"); if (tokenMapEntity.TokenEntities.Any(te => te.TokenKey == tokenKey)) { continue; } if (tokenKey != null && tokenKey.Contains(_delimeter)) { fieldName = tokenKey.SubstringAfter(_delimeter); } tokenMapEntity.TokenEntities.Add( new TokenEntity { TokenRaw = token, TokenKey = tokenKey, FieldName = fieldName }); } templateMetadataEntity.TokenMapEntities.Add(tokenMapEntity); } return(templateMetadataEntity); }
public async Task <TemplateToPdfCommandResult> Handle(TemplateToPdfCommand request, CancellationToken cancellationToken) { string template = _templateProvider.GetTemplate(request); TemplateMetadataEntity templateMetadataEntity = _parser.ParseTemplate(template); foreach (TokenMapEntity tokenMapEntity in templateMetadataEntity.TokenMapEntities) { DataTable dataTable = _saleRepository.QueryView(tokenMapEntity.ViewName, request.Id); Dictionary <string, string> data = _parser.CreateDynamicMap(tokenMapEntity, dataTable.Rows[0]); template = _templateCompiler.Compile(template, data); } Stream stream = _pdfRenderer.RenderHtml(template); return(new TemplateToPdfCommandResult { Stream = stream }); }