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

Escape or replace on include

Hi all,

is there a way to escape during an include in Volt? I need to include a html snippet in a javascript variable context, so I need to escape all quotes or double quotes during include.

Thanks Gianluca

Correct me if I'm wrong but doesn't PHP have this built in? addslashes() https://php.net/manual/en/function.addslashes.php



20.4k

Hi Rewt0r,

thanks for the suggestion, can you please provide an example?

Regards

Can you provide your code and explain which bit you're trying to escape and I'll have a look...



20.4k
edited Aug '14

Ok, see below:

<script>
    function toggle(){
        $('#myContainer').html("{% include "inc.html" with ['var' : 'value'] %}");
    }

I need the result of the include to be escaped... so basically i need to buffer it to escape!

I hope to have been more clear. Regards

edited Aug '14

I'm not sure whether you can add filters on an include but have you tried adding |slashes?

For example:

$('#myContainer').html("{% include "inc.html" with ['var' : 'value']|slashes %}");



20.4k

Yes i tried everything but, as expected, none of these syntaxes worked:

{% include|slashes ".... 

{% include "........"|slashes 

{% (include "........")|slashes 

Your code is applying the filter to the input array intead of the buffer of the include. I think i need to implement a custom include volt function to accomplish this, using a stacked output buffer: essentially i've understood my request is beyond VOLT's limits since it's beyond PHP's, as there's no way to do what i need with the language include statement.

Yeah I hadn't tested, probably is best to add a function that's passed to volt that outputs the file contents with addslashes() applied.