HomeiOS DevelopmentContent material filters in Feather CMS

Content material filters in Feather CMS


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

Whenever you create a weblog publish or a static web page in Feather you need to use the markdown filter to render the ultimate illustration of the saved content material. Additionally it is doable to spotlight 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 remodel numerous textual content values utilizing manually chosen filters for every particular person frontend associated content material. For instance you’ll be able to allow the markdown filter for publish A, however for those who want to make use of HTML for publish B you’ll be able to 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. By utilizing Feather you do not have to place shortcodes into brackets, however you’ll be able to exchange something you need primarily based by yourself guidelines. Curse phrases? No drawback. Print one thing utilizing Swift? Why not.

The one factor that you need to bear in mind that content material filters are working in a synchronous approach. Be as quick as doable, since they are going to block the execution thread. In many of the instances this isn’t an enormous deal, however I simply wished to let that you just will not be capable to return a future this time. 🚀



Tips on how to 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 fret an excessive amount of, a module may 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

closing 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 referred to as DuckFilterModule.swift contained in the Modules listing. We’ve got 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 may even return a number of filters per module if wanted.

Hook features are fairly important in Feather CMS, modules can work collectively by way of dynamic hooks with out forming a dependency. This strategy could be very versatile and highly effective, you’ll be able to construct and invoke your individual hooks by way of the ViperKit framework. If you wish to study extra about this modular structure, you can too seize a replica 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: "🦆")
    }
}

Mainly that is it. You simply have to change the configure.swift and append a DuckFilterModule() occasion to the module record. Now for those who run Feather with this module you’ll be able to allow the Duck filter to 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.



Tips on how to invoke obtainable 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 accommodates the f phrase, simply be artistic. 🤔





Save the web page and preview it utilizing the attention icon. It’s best to see your unique textual content. Now for those who click on on the feather icon you’ll be able to choose which filters must 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 obtainable for you. By calling the filter technique on any FrontendContentModel sort you’ll be able to invoke the enabled filters on that content material, it will 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 publish concerning the existence of this particular object in Feather CMS. If in case you have points, feedbacks or concepts, please use GitHub or Twitter.




RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments