|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
chu_man_fu Smarty n00b
Joined: 13 Sep 2018 Posts: 3
|
Posted: Thu Sep 13, 2018 8:24 pm Post subject: stripcslashes and parse_double_quoted_string causing issues |
|
|
I am currently in the process of migrating from Smarty 2 to Smarty 3.
Everything was going great until I walked into an issue with \n characters breaking my Javascript.
I have a language config var which contains a \n character.
When then config is loaded and I consume the variable in my template to write the Javscript the \n character is honoured. On the face of it this is correct.
But why am I getting this issue now? Why didn't I get it in Smarty 2?
I have traced it down the the Smarty 3 function parse_double_quoted_string. This calls stripcslashes which seems sensible...but why?
When the config file is read, PHP will double slash all \n characters.
This means that \n becomes \\n. parse_double_quoted_string will then strip out the first slash.
In Smarty 2 there was no call to a function like parse_double_quoted_string or stripcslashes. This means that \\n was passed to the template and the javascript didn't break because a line feed escaped and thus was never honoured.
Can anyone think of a way that I can get round this problem without modifying my config vars?
I want to be able to duck punch parse_double_quoted_string but thats not possible.
Modify the Smarty code is a no go since it is being installed via composer.
Many thanks |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Sep 13, 2018 11:03 pm Post subject: |
|
|
Can you provide an example template that demonstates your issue?
So far, it seems you're doing something you should have not done. |
|
Back to top |
|
chu_man_fu Smarty n00b
Joined: 13 Sep 2018 Posts: 3
|
Posted: Fri Sep 14, 2018 7:35 am Post subject: |
|
|
Sure
test.conf:
Code: |
testvar = "FirstLine\nSecondLine"
|
test.tpl:
Code: |
{config_load file="test.conf"}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<script type="text/javascript">
{literal}
alert("{/literal}{#testvar#}{literal}");
{/literal}
</script>
</body>
</html>
|
Smarty V2 Output raw:
You can see on the alert function that the \n is \\n.
Code: |
\n<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n</head>\n<body>\n<script type="text/javascript">\n\t\n\talert("FirstLine\\nSecondLine");\n\t\n</script>\n</body>\n</html>
|
Smarty V2 Output to browser:
Code: | <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<script type="text/javascript">
alert("FirstLine\nSecondLine");
</script>
</body>
</html> |
Smarty V3 Output raw:
You can see on the alert function that the \n is still \n.
Code: |
\n<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n</head>\n<body>\n<script type="text/javascript">\n\t\n\talert("FirstLine\nSecondLine");\n\t\n</script>\n</body>\n</html>
|
Smarty V3 Output to browser:
Code: | <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<script type="text/javascript">
alert("FirstLine
SecondLine");
</script>
</body>
</html> |
If you perform a simple file_get_contents on test.conf you get the following:
Code: | testvar = "FirstLine\\nSecondLine" |
I hope this sheds some light on my issue.
By the way; I am not saying there is a bug in Smarty v3. We are relying on the bug in Smarty v2 and are wondering what we can do to make Smarty v3 act like v2. |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Fri Sep 14, 2018 1:35 pm Post subject: |
|
|
If you want compatible behavior in Smarty2 and Smarty3, try to enclose the config line in single quotes.
If you just want it to work in Smarty3, double the escape slash.
https://github.com/AnrDaemon/test-001/tree/forum-27426 |
|
Back to top |
|
chu_man_fu Smarty n00b
Joined: 13 Sep 2018 Posts: 3
|
Posted: Mon Sep 17, 2018 2:23 pm Post subject: |
|
|
Thanks for your reply and sample code.
We did have this idea as a backup but we didn't know if there was a way of stopping the slashes stripping. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|