Skip to content

Implement nl2br filter #698

@DenuxPlays

Description

@DenuxPlays

Hey,

would you be interested in adding the nl2br filter?

I would make a pr if you want.

My current implementation is:

class Nl2BrFilter : Filter {
    private val newlineRegex = Regex("(\r\n|\r|\n)")

    override fun apply(
        input: Any?,
        args: Map<String?, Any?>?,
        self: PebbleTemplate?,
        context: EvaluationContext?,
        lineNumber: Int
    ): Any? {
        if (input == null) {
            return null
        }
        if (input !is String) {
            throw IllegalArgumentException("nl2br filter only supports String input")
        }

        return input.replace(newlineRegex, "<br>")
    }

    override fun getArgumentNames(): List<String?>? {
        return null
    }
}

This is not what I would use I just did it because the implementation is easy.
In pebble I would not use regexs and loop through each char of the string.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions