Issue #49 new

What do you think about append "addslashes" util function ?

Stéphane Klein
created an issue

What do you think about append "addslashes" util function (something named "webhelpers.util.addslashes") ?

Code like : {{{

!python

def addslashes(s): d = {'"':'\"', "'":"\'", "\0":"\\0", "\":"\\"} return ''.join(d.get(c, c) for c in s) }}}

Use case :

{{{

!javascript

<script type="text/javascript"> print("${h.addslashes(one_template_variable)}") </script> }}}

If you think it's a good idea, I can you send a patch with code and documentation.

Regards, Stephane

Comments (4)

  1. Stéphane Klein reporter

    If I have :

    one_template_variable = "I don't know"
    

    and I execute :

    <script type="text/javascript">
    print('${one_template_variable}')
    </script>
    

    The result is :

    <script type="text/javascript">
    print('I don't know') // <= here there are an error
    </script>
    

    It's more explicit ?

  2. Mike Orr

    I think this needs a more general solution, some kind of 'qoute' function that's told what the surrounding quotes are.

    It could also make the surrounding quotes itself, but that would only work if the helper expression is the entire quoted string, not interpolated within it.

  3. Log in to comment