Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

bug in smarty (function and subtemplate)
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Sun Mar 15, 2015 4:28 pm    Post subject: bug in smarty (function and subtemplate) Reply with quote

hi

please run this example :

%main% :
Code:
{require 'test'}


test :
Code:
{require 'test2'}


test2 :
Code:
{require 'test_func'}
{require 'test3'}
{require 'test_t'}


test3 :
Code:
{function name=func}
{call name=test_func1 nocache}
{nocache}loaded{/nocache}
{/function}
{$a = 'func' nocache}


test_t :
Code:
{call name=$a nocache}


test_func :
Code:
{function name="test_func1"}
{nocache}test_func1{/nocache}
{/function}


please run above example.

now update test_func. only change timestamp. no any change in template.

now run.

ok no problem exists !

now run.

problem is here.

error : Unable to find template function 'test_func1'
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sun Mar 15, 2015 11:20 pm    Post subject: Reply with quote

This is not a Smarty bug.

This and some other of your previous problems are related to the change you made to the {require} compiler to support variable template names.
The modification was made in a way Smarty thought that all subtemplate names are variable causing separate cache files for each subtemplate.
Smarty has some limitations when calling template functions in nocache mode like {calll .... nocache} when the function is defined in a subtemplate which is assumed to be variable.

See corrected code of the require compiler below which does test if the template name is variable or not.


Code:
class Smarty_Compiler_require extends Smarty_Internal_Compile_Include
{
    public $required_attributes = array('file');
    public $shorttag_order = array('file');
    public $option_flags = array('nocache', 'inline', 'caching');
    public $optional_attributes = array('compile_id', 'scope');

    public function compile($args, Smarty_Internal_SmartyTemplateCompiler $compiler, $parameter = null)
{
    $_attr = $this->getAttributes($compiler, $args);
    $newArgs = array();
    if (false === strpos($_attr['file'], '$')) {
        $file = trim($_attr['file'], "'\"");
        $newArgs[0]['file'] = "'mysql:{$file}'";
    } else {
        $newArgs[0]['file'] = "'mysql:'.".$_attr['file'];
    }
    $newArgs[]['compile_id'] = "'{$GLOBALS['user_id']}'";
    $newArgs[]['scope'] = "'parent'";
    return parent::compile($newArgs, $compiler, $parameter);
}
}
Back to top
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Mon Mar 16, 2015 8:00 am    Post subject: Reply with quote

my problem was solved.
thank you very much.
Back to top
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Mon Mar 16, 2015 3:43 pm    Post subject: Reply with quote

no no no seems problem is exists.

[removed]


Last edited by sm@rty on Mon Mar 16, 2015 4:24 pm; edited 1 time in total
Back to top
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Mon Mar 16, 2015 4:21 pm    Post subject: Reply with quote

I'm confused !!!!!!!!!!!!!!!!!!!!!

please test examples :

first example :

%main% :

Code:

{function name=func1}
   <title>loaded</title>
{/function}
{$events.header['func1'] = [] nocache}
{require 'test'}


test :
Code:

{foreach $events.header as $func=>$p nocache}
{call name=$func p=$p nocache}
{/foreach}


only once run.

problem : cache file for test not create in cache dir. but %main% is made.
---------------------------------------------------------------------------------------------
second example:

%main% :

Code:

{$config.tpl.template = 'test' nocache}
{function name=func1}
   <title>loaded</title>
{/function}
{$events.header['func1'] = [] nocache}
{require $config.tpl.template}


test :

Code:

{foreach $events.header as $func=>$p nocache}
{call name=$func p=$p nocache}
{/foreach}


before test this example clear all cache and compile file.

now run.
result : compile and cache file is made(for both).

now update timestamp of template %main% to new timestamp.

now run.
result : no problem.

now run.

result : Unable to find template function 'func1'
Back to top
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Mon Mar 16, 2015 5:36 pm    Post subject: Reply with quote

and find new problem.

{"\n"} in sub template not working.

example :

%main%
Code:
{require 'test'}


test
Code:
{"\n" nocache}


output : n
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Mar 17, 2015 12:25 am    Post subject: Reply with quote

sm@rty wrote:
I'm confused !!!!!!!!!!!!!!!!!!!!!

please test examples :

first example :

%main% :

Code:

{function name=func1}
   <title>loaded</title>
{/function}
{$events.header['func1'] = [] nocache}
{require 'test'}


test :
Code:

{foreach $events.header as $func=>$p nocache}
{call name=$func p=$p nocache}
{/foreach}


only once run.

problem : cache file for test not create in cache dir. but %main% is made.

Note that {require} does only create a separate cache file if the template name is variable. Your example did work without problems in my place.


sm@rty wrote:
second example:

%main% :

Code:

{$config.tpl.template = 'test' nocache}
{function name=func1}
<title>loaded</title>
{/function}
{$events.header['func1'] = [] nocache}
{require $config.tpl.template}


test :

Code:

{foreach $events.header as $func=>$p nocache}
{call name=$func p=$p nocache}
{/foreach}


before test this example clear all cache and compile file.

now run.
result : compile and cache file is made(for both).

now update timestamp of template %main% to new timestamp.

now run.
result : no problem.

now run.

result : Unable to find template function 'func1'


Smarty was not really prepared for this very special use case where template function definitions and it's calls are in different individually cached subtemplates because of calling these subtemplates by a variable name,

I uploaded now a fix which does cover hopefully now all use cases.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Tue Mar 17, 2015 12:33 am    Post subject: Reply with quote

sm@rty wrote:
and find new problem.

{"\n"} in sub template not working.

example :

%main%
Code:
{require 'test'}


test
Code:
{"\n" nocache}


output : n



I can't reproduce this problem.
Check your mysql content. I suspect that the "\n" was escaped when it was stored in the database,
Back to top
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Tue Mar 17, 2015 6:53 am    Post subject: Reply with quote

Quote:
uploaded now a fix which does cover hopefully now all use cases.


thank you very much.

Quote:
I can't reproduce this problem.
Check your mysql content. I suspect that the "\n" was escaped when it was stored in the database,


yes. this is bug in my project and fix.

Quote:
Your example did work without problems in my place.

so download this project.

http://wikisend.com/download/341668/smarty.zip

this project like my project and occur bug.(smarty is update).

Edit :

seems bug is exists yet.

please download above project and retest examples http://www.smarty.net/forums/viewtopic.php?p=88481#88471
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Mar 18, 2015 4:15 am    Post subject: Reply with quote

What is wrong with it? I can't see a bug.
Back to top
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Wed Mar 18, 2015 6:58 am    Post subject: Reply with quote

ok ok.

step by step

step 1
download this project
http://wikisend.com/download/341668/smarty.zip

step 2
import db smarty.sql(in zip archive)

step 3

now run index.php
no problem.

status of cache and compile in dir smarty/data/compile && smarty/data/cache

compile :



cache



step 4

now update timestamp(1 second)



step 5

now run index.php

step 6

now run index.php

output :

Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Wed Mar 18, 2015 6:03 pm    Post subject: Reply with quote

It looks like that it is caused by your manual change of the modified date to a number which is not related to the current date.
Manually messing around with it does create problems.

See the mysql resource in the demo folder where the modified field is CURRENT TIMESTAMP.
Back to top
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Wed Mar 18, 2015 7:05 pm    Post subject: Reply with quote

no problem in mysql.
Quote:

`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,


CURRENT_TIMESTAMP is string date time.

and when fetch from database convert to timestamp by strtotime

using strtotime is mistake. beacuse depend on timezone !

Quote:
$mtime = strtotime($row['modified']);

this way is mistake !

please see example :

Code:
date_default_timezone_set('UTC');
echo strtotime('2015-03-18 22:12:37').'<br>';
date_default_timezone_set('Asia/Tehran');
echo strtotime('2015-03-18 22:12:37').'<br>';


output :

1426716757
1426704157

and using CURRENT_TIMESTAMP is mistake. because some database server is in another server and possible time not config.

i always using time() in php and never using current time in mysql( but in mysql event possible required).

But I've reviewed example.

please add this line after line 6 in index.php
Code:
if(isset($_GET['update']))
   mysqli_query($con,"UPDATE `templates` SET `modified` = '".time()."' WHERE `name` = '%main%'");


now claer all cache and compile file.

goto index.php?update

and

goto index.php?update

now goto index.php and you see

Fatal error: Uncaught --> Smarty: Unable to find template function 'func1' <-- thrown in ... smarty\smarty\sysplugins\smarty_internal_template.php on line 575
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Wed Mar 18, 2015 8:29 pm    Post subject: Reply with quote

sm@rty wrote:
no problem in mysql.
Quote:

`modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,


CURRENT_TIMESTAMP is string date time.

CURRENT_TIMESTAMP is a timestamp, not a string.
You can use it in any datetime context directly.

Quote:
and when fetch from database convert to timestamp by strtotime

using strtotime is mistake.

On this, I agree.

Quote:
and using CURRENT_TIMESTAMP is mistake. because some database server is in another server and possible time not config.

The time MUST be configured correctly. If it is not, pick a baseball bat and pay a visit to the server owner.
In either case, this is not a PHP question, not even MySQL.

Quote:
i always using time() in php and never using current time in mysql( but in mysql event possible required).

Then you're jumping on one leg where you could run on two.
Stupid choices lead to stupid code.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Mar 20, 2015 12:36 am    Post subject: Reply with quote

AnrDaemon wrote:
CURRENT_TIMESTAMP is a timestamp, not a string.
You can use it in any datetime context directly

This is wrong. MYSQL does store CURRENT_TIMESTAMP as a string.

I agree that CURRENT_TIMESTAMP can be a problem if MYSQL does run on a different server. Not only different a timezone will cause problems also the the clocks of the servers are out of sync.

Anyway I finally found the original problem.
The fix is on github
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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
Protected by Anti-Spam ACP