Everybody needs to manage some templates from time to time in their application. Templator provides a database agnostic solution to the problem, optionally providing a configurable set of API endpoints to allow user to manage these from any interface.
Default template sources can be hosted remotely on github for example and reloaded/reset at will.
let templator = try req.make(Templates<ApiCoreDatabase>.self)
let htmlFuture = templator.get(EmailTemplateInvitationHTML.self, data: templateModel, on: req)
return htmlFuture.flatMap(to: View.self) { htmlTemplate in
/// Use template
}
Creating templates
To create a new template, conform your struct to a Source<Database>
/// Basic invitation template
public class EmailTemplateInvitationHTML: Source {
public typealias Database = PostgreSQLDatabase
/// Name of the template
public static var name: String = "email.invitation.html"
public static var link: String = "https://raw.githubusercontent.com/LiveUI/ApiCore/master/Resources/Templates/email.invitation.html.leaf"
public static var deletable: Bool = false
}
A little trick if you want to avoid defining the Database for each template is to create a protocol wrapper
public protocol TemplateSource: Source where Self.Database == ApiCoreDatabase { }
public class EmailTemplateInvitationPlain: TemplateSource {
/// Name of the template
public static var name: String = "email.invitation.plain"
public static var link: String = "https://raw.githubusercontent.com/LiveUI/ApiCore/master/Resources/Templates/email.invitation.plain.leaf"
public static var deletable: Bool = false
}
Templator
API for template management for Vapor 3
Everybody needs to manage some templates from time to time in their application. Templator provides a database agnostic solution to the problem, optionally providing a configurable set of API endpoints to allow user to manage these from any interface.
Default template sources can be hosted remotely on github for example and reloaded/reset at will.
Usage
Configuration
To setup API routes do (more on routes here)
Next you need to add the Templator table / model to your migrations
optionally, you can also setup an additional endpoint authentication through a closure as follows
To register required services, run
Apart from registering Leaf as a templating system,
Templates<PostgreSQLDatabase>
is registered to be used internally in your application.API routes
Routes available are as follows:
templates
- list templatestemplates
- create a templatetemplates/:id
- get a single templatetemplates/:id
- modify a templatetemplates/:id
- delete a templateModel for modification or creation of a template is:
Retrieving templates in your app is super simple
Creating templates
To create a new template, conform your struct to a
Source<Database>
A little trick if you want to avoid defining the Database for each template is to create a protocol wrapper