pebble icon indicating copy to clipboard operation
pebble copied to clipboard

Newline trimmed unexpectedly

Open crummy opened this issue 3 years ago • 3 comments

Hi, during my migration from jtwig to pebble I've noticed some odd behaviour: sometimes newlines are trimmed. Here is a test case to reproduce the issue:

import com.mitchellbosecke.pebble.PebbleEngine
import org.junit.jupiter.api.Test
import java.io.StringWriter
import java.io.Writer


class PebbleTest {
    val template = """
        <!DOCTYPE html>
        <html>
            <script>
                const foo = {{ foo | default("null") }}
                const bar = {{ bar | default("null") }};
            </script>
        </html>
    """.trimIndent()

    @Test
    fun `no newline`() {
        val pebble = PebbleEngine.Builder().build()
        val template = pebble.getLiteralTemplate(template)
        val writer: Writer = StringWriter()
        template.evaluate(writer)
        println(writer.toString())
    }
}

What I expect is:

<!DOCTYPE html>
<html>
    <script>
        const foo = null
        const bar = null;
    </script>
</html>

But what I get is:

<!DOCTYPE html>
<html>
    <script>
        const foo = null        const bar = null;
    </script>
</html>

It seems that the presence of the ; after the const bar definition means a semicolon will be inserted correctly. If I add one to the end of the const foo line the program behaves correctly.

(It's probably invalid JS to skip the semicolons... my fix was just to insert them. But the behaviour seemed unexpected to me!)

crummy avatar Jan 10 '22 23:01 crummy

+1 here

joshua-paypay avatar Feb 01 '22 06:02 joshua-paypay

Hello, You can use newLineTrimming(false) in builder https://pebbletemplates.io/com/mitchellbosecke/pebble/PebbleEngine/Builder/

joshua-paypay avatar Feb 01 '22 06:02 joshua-paypay