Saturday, October 14, 2023
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 how one can construct your individual one utilizing hooks features and Vapor.

Vapor

The anatomy of a content material filter

If you create a weblog put up or a static web page in Feather you need to use the markdown filter to render the ultimate illustration of the saved content material. It’s also potential 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 method permits us to remodel varied 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 put up A, however should you desire to make use of HTML for put up 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 operate. By utilizing Feather you do not have to place shortcodes into brackets, however you’ll be able to 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 must remember that content material filters are operating in a synchronous approach. Be as quick as potential, since they’ll block the execution thread. In many of the circumstances this isn’t a giant deal, however I simply wished to let you understand that you just will not have the ability to return a future this time. 🚀



The way to create a content material filter for Feather?

Content material filters are offered by modules, because of this you must write a Feather CMS module first. Don’t be concerned an excessive amount of, a module could 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 interchange the fuck phrase with a duck emoji. 🦆

import Vapor
import Fluent
import ViperKit

last class DuckFilterModule: ViperModule {

    static var title: String = "duck-filter"

    func invokeSync(title: String, req: Request, params: [String: Any]) -> Any? {
        swap title {
        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 present the module a reputation, that is going to be duck-filter in fact, and we’ve got to implement the invokeSync hook operate that ought to return a filter sort for the content-filter key. You possibly can 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 method could be very versatile and highly effective, you’ll be able to construct and invoke your individual hooks by the ViperKit framework. If you wish to be taught extra about this modular structure, you too can seize a duplicate of my Sensible Server Facet Swift ebook, 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 should you run Feather with this module you’ll be able to 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.



The way to 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, nicely… it may be something that incorporates the f phrase, simply be inventive. 🤔





Save the web page and preview it utilizing the attention icon. It is best to see your unique textual content. Now should you click on on the feather icon you’ll be able to choose which filters needs to be enabled for this explicit content material. Test 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 accessible for you. By calling the filter methodology on any FrontendContentModel sort you’ll be able to invoke the enabled filters on that content material, it will mean you can remodel 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 how one can create a content material relation, however possibly subsequent time I am going to create an extended put up in regards to the existence of this particular object in Feather CMS. You probably have points, feedbacks or concepts, please use GitHub or Twitter.






Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments