On this article I will inform you all about content material filters and present you how you can construct your personal one utilizing hooks features and Vapor.
Vapor
The anatomy of a content material filter
If you create a weblog publish or a static web page in Feather you should use the markdown filter to render the ultimate illustration of the saved content material. It’s also doable to focus on Swift code snippets although one other filter. These content material filters are altering the underlying information in dynamic approach at runtime, Feather solely saves the unique information within the persistent srorage utilizing Fluent. 💪
This strategy permits us to rework numerous textual content values utilizing manually chosen filters for every particular person frontend associated content material. For instance you possibly can allow the markdown filter for publish A, however in case you want to make use of HTML for publish B you possibly can disable the markdown filter and write your article utilizing the nice previous HyperText Markup Language.
Content material filters can be utilized to create your personal shortcodes. In WordPress shortcode is a small piece of code, indicated by brackets, that may carry out a devoted perform. Through the use of Feather you do not have to place shortcodes into brackets, however you possibly can change something you need based mostly by yourself guidelines. Curse phrases? No downside. Print one thing utilizing Swift? Why not.
The one factor that you need to remember that content material filters are operating in a synchronous approach. Be as quick as doable, since they’ll block the execution thread. In a lot of the circumstances this isn’t a giant deal, however I simply wished to let you already know that you just will not be capable of return a future this time. 🚀
How you can create a content material filter for Feather?
Content material filters are supplied by modules, which means that you need to write a Feather CMS module first. Don’t be concerned an excessive amount of, a module might be fairly easy, in our case it is simply going to be one Swift file. We’re going to write a filter that is going to switch the fuck phrase with a duck emoji. 🦆
import Vapor
import Fluent
import ViperKit
last class DuckFilterModule: ViperModule
static var identify: String = "duck-filter"
func invokeSync(identify: String, req: Request, params: [String: Any]) -> Any?
swap identify
case "content-filter":
return [DuckFilter()]
default:
return nil
Simply place the code from above into a brand new file known as DuckFilterModule.swift
contained in the Modules
listing. We now have to present the module a reputation, that is going to be duck-filter
after all, and now we have to implement the invokeSync
hook perform that ought to return a filter sort for the content-filter
key. You may even return a number of filters per module if wanted.
Hook features are fairly important in Feather CMS, modules can work together through dynamic hooks with out forming a dependency. This strategy may be very versatile and highly effective, you possibly can construct and invoke your personal hooks by means of the ViperKit framework. If you wish to study extra about this modular structure, you may also grab a copy of my Practical Server Side Swift book, it is written for Vapor 4 and you may discover ways to write a modular weblog engine utilizing Swift (similiar to Feather).
Anyway, again to the subject, we simply need to implement the DuckFilter object:
import Vapor
import ViperKit
struct DuckFilter: ContentFilter
var key: String "duck-filter"
var label: String "Duck"
func filter(_ enter: String) -> String
enter.replacingOccurrences(of: "fuck", with: "🦆")
Principally that is it. You simply have to change the configure.swift
and append a DuckFilterModule()
occasion to the module checklist. Now in case you run Feather with this module you possibly can allow the Duck filter in your contents below the content material editor admin interface. Oh by the best way it can save you this filter below a ContentFilters/DuckFilter.swift
listing subsequent to your module file.
How you can invoke out there filters?
Let’s check out our newly created filter. Go to the admin and create a brand new static web page with the “I do not give a fuck” content material, nicely… it may be something that incorporates the f phrase, simply be artistic. 🤔

Save the web page and preview it utilizing the attention icon. It is best to see your unique textual content. Now in case you click on on the feather icon you possibly can choose which filters must be enabled for this explicit content material. Verify the Duck, save the content material particulars and reload the preview web page. It is best to see the duckling. 🐥
These content material filters are programmatically out there for you. By calling the filter methodology on any FrontendContentModel
sort you possibly can invoke the enabled filters on that content material, this may help you rework an related mannequin worth utilizing filters.
let filteredValue = frontendContentModel.filter(myModel.contentToBeFiltered, req: req)
The one catch is that your Fluent mannequin must have an related content material sort since filters are tied to FrontendContentModel
objects. In my previous article I discussed how you can create a content material relation, however perhaps subsequent time I will create an extended publish in regards to the existence of this particular object in Feather CMS. You probably have points, feedbacks or concepts, please use GitHub or Twitter.