We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Volt template is acting strange - comments within JS is commenting out the complete function when rendering

Hi,

When I add any comments in my js using volt, the next block of js code is also commented out and I get javascript errors.

So for example the following .volt file

<html>

<head>
  <script>
    //this function alert to the screen hello world
    function hello() {
      alert("hello world!");
    }
  </script>
</head>

<body>
  <button onclick="hello();">Click Me</button>
</body>

</html>

Is rendered as:

<html>

<head>
  <script>
    //this function alert to the screen hello world
   // function hello() {
   //   alert("hello world!");
   // }
  </script>
</head>

<body>
  <button onclick="hello();">Click Me</button>
</body>

</html>

Any tips on how to resolve would be appreciated. (As a workaround I can remove the comments for now, but when I integrate any 3rd party plugins eg. paypal / stripe, I am going to have problems).

Thanks

Put js code into file?



6.5k

Hi,

I have tried that peviously and gives samwe result.

If by chance you used Devtools to start a project, the public/index.php sometimes has some "compression" techniques on the getContent() output...



6.5k

@Trent thanks for pointing me in the right direction,

echo str_replace(["\n","\r","\t"], '', $application->handle()->getContent());

the \n is causing the problem, what would be the impact of changing the code to just :

echo $application->handle()->getContent();

Thanks

nothing will impact.. for some reason that was added to condense the html output, but any JS on the page breaks.

Starting off with website development is a great challenge as many things act strange for no reason. A website https://prodissertation.com/ helped me a lot in my journey to become a web developer. I am sure it will be a helper for you too.