johndoe33
johndoe33

Reputation: 454

Replace line break

In Fluid Template and tx_news, I need to replace line breaks with "\n" for passing into JavaScript function.

If a JavaScript string contains line break, console will print "Unexpected token."

<a onclick="doSomething('{newsItem.bodytext}');">Click me</a>

How can you replace line breaks with "\n" in this example?

Upvotes: 1

Views: 2672

Answers (2)

Bernd Wilke πφ
Bernd Wilke πφ

Reputation: 10791

AS urbantrout already wrote: you can write an own viewhelper in PHP.

But you also can use a TypoScript-Viewhelper:

<a onclick="doSomething('{newsItem.bodytext -> f:cObject(typoscriptObjectPath: \'lib.nlReplace\')}');">Click me</a>

(as you are in a string you need to escape the inner ')
and some TypoScript like

lib.nlReplace = TEXT
lib.nlReplace {
    current = 1
    stdWrap.replacement {
        1 {
            search = #\n#
            replace = \\n
            useRegExp = 1
        }
    }
}

Upvotes: 4

Harald Atteneder
Harald Atteneder

Reputation: 66

You could write your own ViewHelper and use it like this:

{namespace ns=Vendor\ExtensionName\ViewHelpers}

<a onclick="doSomething('{newsItem.bodytext -> ns:viewhelperName()}');">Click me</a>

More infos here: Developing a custom ViewHelper

Upvotes: 2

Related Questions