Open Closed

Create Text Template By User at Runtime #9195


User avatar
0
htavukcu created

Hi,

I didn't see any endpoints about creating a Text Template by the user at runtime. I don't think there is a section about this in the documentation. What is the right way to achieve this?

Thanks


4 Answer(s)
  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    Hi,

    ITextTemplateDefinitionManager is implemented here: https://github.com/abpframework/abp/blob/605482916e6f87b75d5a23a76ba2299fb72c9306/framework/src/Volo.Abp.TextTemplating.Core/Volo/Abp/TextTemplating/TemplateDefinitionManager.cs#L9

    And it uses IStaticTemplateDefinitionStore and IDynamicTemplateDefinitionStore.

    And Dynamic one doesn't have any real implementation. The NullIDynamicTemplateDefinitionStore was resolved when injected:

    https://github.com/abpframework/abp/blob/605482916e6f87b75d5a23a76ba2299fb72c9306/framework/src/Volo.Abp.TextTemplating.Core/Volo/Abp/TextTemplating/NullIDynamicTemplateDefinitionStore.cs#L9

    You may implement your own DynamicTemplateDefinitionStore class by implementing IDynamicTemplateDefinitionStore interface that writes/read from a database table.

    Something like that:

    public class DatabaseTemplateDefinitionStore : IDynamicTemplateDefinitionStore, ITransientDependency
    {
        protected IRepository<TemplateDefinitionEntity, Guid> TemplateDefinitionRepository { get; }
    
        public DatabaseTemplateDefinitionStore(IRepository<TemplateDefinition, Guid> templateDefinitionRepository)
        {
            TemplateDefinitionRepository = templateDefinitionRepository;
        }
    
        public async Task<TemplateDefinition> GetAsync(string name)
        {
            var entity = await TemplateDefinitionRepository.GetAsync(name);
            return MapToTemplateDefinitionAsync(entity); // Map to TemplateDefinition in your own way
        }
    
        public async Task<IReadOnlyList<TemplateDefinition>> GetAllAsync()
        {
            var list = await TemplateDefinitionRepository.GetListAsync();
            return list.Select(MapToTemplateDefinitionAsync).ToImmutableList(); // Map to TemplateDefinition in your own way
        }
    
        public async Task<TemplateDefinition?> GetOrNullAsync(string name)
        {
            var entity = await TemplateDefinitionRepository.FindAsync(name);
    
            return entity == null ? null : MapToTemplateDefinitionAsync(entity); // Map to TemplateDefinition in your own way
        }
    
        private TemplateDefinition MapToTemplateDefinitionAsync(TemplateDefinitionEntity entity)
        {
            // Implement your mapping logic here
            return new TemplateDefinition(entity.Name, entity.LocalizationResourceName, entity.DisplayName, entity.IsLayout, entity.Layout, entity.RenderEngine, entity.Properties);
        }
    }
    

    And create another AppService that creates it at runtime:

    
    public class DynamicTemplateDefinitionAppService : IDynamicTemplateDefinitionStore, ITransientDependency
    {
        protected IRepository<TemplateDefinitionEntity, Guid> TemplateDefinitionRepository { get; }
    
        public DynamicTemplateDefinitionAppService(IRepository<TemplateDefinition, Guid> templateDefinitionRepository)
        {
            TemplateDefinitionRepository = templateDefinitionRepository;
        }
    
        public async Task CreateAsync(TemplateDefinitionDto input)
        {
            var entity = new TemplateDefinitionEntity(input.Name, input.LocalizationResourceName, input.DisplayName, input.IsLayout, input.Layout, input.RenderEngine, input.Properties);
            await TemplateDefinitionRepository.InsertAsync(entity);
        }
    
        // all other CRUD methods can be implemented similarly
    }
    
  • User Avatar
    0
    htavukcu created

    Thanks for reply, I think it's done.

  • User Avatar
    0
    htavukcu created

    Thanks for reply, I think it's done.

    Hi,

    I have completed the implementation, how can I add content to it?

  • User Avatar
    0
    enisn created
    Support Team .NET Developer

    You should be able to see them in the table on Text Templates page if you properly implemented IDynamicTemplateDefinitionStore. The module gets them from dynamic store and merges with static store.

    You can also put a breakpoint to DynamicTemplateDefinitionAppService's GetAllAsync method to make sure it's executed. If not, check if it's located in the correct layer and make sure it's registered to dependency injection well. You may consider using [ExposeServices] attribute to make sure it's registered properly like this:

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(IDynamicTemplateDefinitionStore))]
    public class DatabaseTemplateDefinitionStore : IDynamicTemplateDefinitionStore, ITransientDependency
    {
        // ...
    }
    
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v9.3.0-preview. Updated on May 15, 2025, 10:28