smarty-php / smarty-php/smarty

Cannot use escape on output of smarty defined function

Open
#847 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
PHP
Stars
2.3k
Forks
709
PR merge metrics
No merged PRs in 30d

Description

There seems to be a big difference between how smarty handles filtering of smarty defined functions. Which breaks on newer versions of smarty.

It seems that smarty no longer escapes javascript properly, if function is called.

Workaround: Assign output to variable (by capture for example) and escape:'javascript' works properly again.

Disadvantages of behavior:

  • Need to introduce another variable
  • Backward compatibility

Example code

{function name=testfunction index=0 el=''}
    ---
    Line1 "das" asd as ' asddass' " '' "
    Line2
    ---
{/function}
Call
{testfunction}
Call:escape
{testfunction|escape}
Call:escape:'javascript'
{testfunction|escape:'javascript'}

Output on older smarty: const SMARTY_VERSION = 'Smarty-3.1.19';

Call
    ---
    Line1 "das" asd as ' asddass' " '' "
    Line2
    ---

Call:escape
    ---
    Line1 "das" asd as ' asddass' " '' "
    Line2
    ---
Call:escape:'javascript'
    ---\n    Line1 \"das\" asd as \' asddass\' \" \'\' \"\n    Line2\n    ---\n

Output on new smarty
v3.1.30 - raw installed smarty
and 3.1.43 - bundled with Prestashop 1.7

---
    Line1
    Line2
---

Call:escape

---
    Line1
    Line2
---

Call:escape:'javascript'

---
    Line1
    Line2
---

Example workaround

{capture name=myvarname}
    {testfunction}
{/capture}
{$smarty.capture.myvarname|escape:'javascript'}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Smarty defined-function example and compare how {testfunction|escape} and {testfunction|escape:'javascript'} are processed in the reported older and newer versions. Reproduce the output difference, then trace the defined-function and escaping behavior. Done means direct escaping matches the expected escaped output without requiring capture.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.