Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newline trimmed unexpectedly #613

Open
crummy opened this issue Jan 10, 2022 · 3 comments
Open

Newline trimmed unexpectedly #613

crummy opened this issue Jan 10, 2022 · 3 comments

Comments

@crummy
Copy link

crummy commented Jan 10, 2022

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
Copy link

+1 here

@joshua-paypay
Copy link

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

@chingjustwe
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants