PebbleTemplates / pebble

Java Template Engine
https://pebbletemplates.io
BSD 3-Clause "New" or "Revised" License
1.1k stars 168 forks source link

Newline trimmed unexpectedly #613

Open crummy opened 2 years ago

crummy commented 2 years ago

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!)

joshua-paypay commented 2 years ago

+1 here

joshua-paypay commented 2 years ago

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

chingjustwe commented 1 month ago

Thanks @joshua-paypay, saved my day :-)