[ad_1]
On this article I am going to let you know all about content material filters and present you the right way to construct your individual one utilizing hooks features and Vapor.
Vapor
The anatomy of a content material filter
While you create a weblog put up or a static web page in Feather you should utilize the markdown filter to render the ultimate illustration of the saved content material. It’s also potential to focus on Swift code snippets although one other filter. These content material filters are altering the underlying knowledge in dynamic means at runtime, Feather solely saves the unique knowledge within the persistent srorage utilizing Fluent. 💪
This strategy permits us to remodel varied textual content values utilizing manually chosen filters for every particular person frontend associated content material. For instance you may allow the markdown filter for put up A, however for those who desire to make use of HTML for put up B you may disable the markdown filter and write your article utilizing the nice previous HyperText Markup Language.
Content material filters can be utilized to create your individual 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 may change something you need based mostly by yourself guidelines. Curse phrases? No drawback. Print one thing utilizing Swift? Why not.
The one factor that you need to remember that content material filters are working in a synchronous means. Be as quick as potential, since they may block the execution thread. In many of the instances this isn’t a giant deal, however I simply wished to let that you simply will not be capable of return a future this time. 🚀
How one can create a content material filter for Feather?
Content material filters are supplied by modules, because of this 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 exchange 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? {
change 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’ve to provide the module a reputation, that is going to be duck-filter
after all, and we’ve to implement the invokeSync
hook perform that ought to return a filter sort for the content-filter
key. You’ll be able to even return a number of filters per module if wanted.
Hook features are fairly important in Feather CMS, modules can work collectively by dynamic hooks with out forming a dependency. This strategy may be very versatile and highly effective, you may construct and invoke your individual hooks by the ViperKit framework. If you wish to study extra about this modular structure, you can too seize a duplicate of my Sensible Server Aspect Swift e book, it is written for Vapor 4 and you will 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 listing. Now for those who run Feather with this module you may allow the Duck filter to your contents beneath the content material editor admin interface. Oh by the way in which it can save you this filter beneath a ContentFilters/DuckFilter.swift
listing subsequent to your module file.
How one can invoke accessible 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, properly… it may be something that incorporates the f phrase, simply be inventive. 🤔

Save the web page and preview it utilizing the attention icon. It’s best to see your authentic textual content. Now for those who click on on the feather icon you may choose which filters needs to be enabled for this specific content material. Verify the Duck, save the content material particulars and reload the preview web page. It’s best to see the duckling. 🐥
These content material filters are programmatically accessible for you. By calling the filter methodology on any FrontendContentModel
sort you may invoke the enabled filters on that content material, this may mean you can 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 earlier article I discussed the right way to create a content material relation, however possibly subsequent time I am going to create an extended put up concerning the existence of this particular object in Feather CMS. In case you have points, feedbacks or concepts, please use GitHub or Twitter.
[ad_2]