smarty-php / smarty

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.
Other
2.24k stars 705 forks source link

commented javascript code lines not working/not ignored since smarty 3.1.2x above #883

Closed topsetting closed 1 year ago

topsetting commented 1 year ago

hello, this is original codes on TPL files :

<script>
    //global
    $('#desc-sort-col-1').dataTable({
              "aaSorting": [[ 1, "desc" ]],
          "deferRender": true,
    });
</script>

the output on the browser is working on smarty 3.1.13 but not working on smarty 3.1.2x to smarty 4.1.x

i found the problem, the problem solved when commented text ( //global ) on the code lines above removed my question, why commented text ( //global ) ignored on smarty 3.1.13 but not on smarty 3.1.2x above ?

scottchiefbaker commented 1 year ago

Can you post a simplified template file that exhibits the error. We will attempt to recreate the issue.

scottchiefbaker commented 1 year ago

I am unable to recreate your error using Smarty v4.3.0. Using the above example and a _theme value of red/ I get the following output which is 100% what I would expect:

<script src="red/assets/plugins/chart.js/js/Chart.min.js"></script>
<script src="red/assets/dist/js/statistics.js"></script>

I suspect your error lies somewhere else. Probably in your assign.

topsetting commented 1 year ago

i found the problem, this is original codes on TPL files :

<script>
    //global
    $('#desc-sort-col-1').dataTable({
              "aaSorting": [[ 1, "desc" ]],
          "deferRender": true,
    });
</script>

the output on the browser is working on smarty 3.1.13 but not working on smarty 3.1.2x to smarty 4.1.x

the problem solved when commented text ( //global ) on the code lines above removed

my question, why commented text ( //global ) ignored on smarty 3.1.13 but not on smarty 3.1.2x above ?

wisskid commented 1 year ago

Im not sure. In any case: javascript comments shouldn't be ignored by Smarty. v3.1.20 was released almost a decade ago. Maybe a bug was fixed then? You could check the changelog to see if you find anything.

topsetting commented 1 year ago

for the above javascript code, maybe you can explain why my problem solved when commented text ( //global ), thank you.

topsetting commented 1 year ago

@wisskid , one more question. look at attachment file, this is output on compiled TPL file. its look different output because its started with php tag : <?php ... ?> where its doesnt happen on smarty 3.1.13

Screenshot_30

I think this is cause of the problem, because if i use smarty 3.1.13 it will not be started with php tag <?php ... ?> on the compiled file for the above example javascript code

wisskid commented 1 year ago

@topsetting I suspect you have another problem, since this is the way it is supposed to be. You don't have to worry about the compiled files, just look at the actual output, which in this case is exactly what is expected:

<script>
    //global
    $('#desc-sort-col-1').dataTable({
              "aaSorting": [[ 1, "desc" ]],
          "deferRender": true,
    });
</script>

tested at 3.1.46

Also, I advise you update to the latest Smarty version.

topsetting commented 1 year ago

@wisskid i have one more question, i hope you can help me because i am a newbie.

why <div> or <span> tag not started with php tag <?php ... ?> on compiled files

but based on my problem, every <script> tag on TPL file will be compiled and started with php tag <?php ... ?> on the compiled files

please explain and give me a clue so i can find why its happen. because since <script> tag compiled by smarty and started with php tag <?php ... ?> on the compiled file, the problem come....javascript codes after commented text not loaded

wisskid commented 1 year ago

@topsetting It has something to do with \Smarty_Internal_ParseTree_Template::to_smarty_php. But as I wrote above, the internals of the compiled files are of no interest to you and are invisible to the browser. If you can explain why the output (see this comment) is wrong, I might be able the help.

After some digging, I believe the behavior traces bag to this commit: https://github.com/smarty-php/smarty/commit/279bdbd3521cd717cae6a3ba48f1c3c6823f439d

with changelog:

===== 3.1.21-dev ===== (xx.xx.2014)
 17.10.2014
 - bugfix on $php_handling security and optimization of smarty_internal_parsetree (Thue Kristensen)

Is is unclear what bug is fixed and whether this is still relevant. The replacing of the script tag probably has something to do with <script language="php"></script> being a valid opening tag for PHP in PHP 5 and lower.

topsetting commented 1 year ago

thank you for the reply and i understand with the different on that section.

but i cannot find why javascript code not loaded if its contain a commented string like //global on the script below :

<script>
    //global
    $('#desc-sort-col-1').dataTable({
              "aaSorting": [[ 1, "desc" ]],
          "deferRender": true,
    });
</script>

and its can be solved if //global removed. so, i must remove any commented string after <script> tag if i want to use smarty 3.1.2x or later

but its make me confused, because its happen on smarty 3.1.2x later only, not on smarty 3.1.13 (my current version) and my php version is v7.4

wisskid commented 1 year ago

@topsetting your script should load fine if it is presented to the browser like this.

What could be happening is that you are using a method like {trim} to compact your HTML output. This might cause the //global line to run together with the next one, causing the entire line to be commented out.

I.e. if this is the output of your template:

<script>//global    $('#desc-sort-col-1').dataTable({              "aaSorting": [[ 1, "desc" ]],          "deferRender": true,  });</script>

it will be ignored by the browser.

topsetting commented 1 year ago

ok i have try to disable my function to minify the html code on the browser and you right, its ok now. but its still make me confused, because everything is good when i use smarty 3.1.13

topsetting commented 1 year ago

@wisskid last question, what is maximum php version supported by smarty 3.1.x ?

wisskid commented 1 year ago

Smarty v3.1 has support for PHP up to v7.4. For PHP8, please upgrade to Smarty v4.3.

topsetting commented 1 year ago

ok @wisskid , thank you for all of your answer and response