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

define {function} in global scope by another template

 
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 -> General
View previous topic :: View next topic  
Author Message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Thu Mar 12, 2015 11:22 am    Post subject: define {function} in global scope by another template Reply with quote

hi

how to define {function} in global scope by another template?

main.tpl($smarty->display('main.tpl')):
Code:
{require 'functions.tpl'}
{call name=func1 nocache}


functions.tpl:
Code:
{function name=func1}
{nocache}loaded{/nocache}
{/function}


note :

if $smarty cache enabled
example above not work
------------
{require} plugin :
http://www.smarty.net/forums/viewtopic.php?t=25425&postdays=0&postorder=asc&start=15&sid=1c97dd344ddd38c84f9efdc4f96d111e
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Thu Mar 12, 2015 3:42 pm    Post subject: Reply with quote

Write smarty plugin instead.
Back to top
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Thu Mar 12, 2015 4:52 pm    Post subject: Reply with quote

AnrDaemon one question ???

when cache is false, smarty function define in global scope and you can access to smarty function in any scope but when cache is set to true this action does not work correctly.

why ?

you can test above example in post 1 by two way:

one) $smarty->caching = false;//worked
two) $smarty->caching = true;//does not work correctly
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sat Mar 14, 2015 5:09 am    Post subject: Reply with quote

Do you run the latest Smarty version 3.1.21?
{call ,,,, nocache} did have some problems in earlier versions.
Back to top
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Sat Mar 14, 2015 7:39 am    Post subject: Reply with quote

U.Tews wrote:
Do you run the latest Smarty version 3.1.21?
{call ,,,, nocache} did have some problems in earlier versions.


smarty version : @version 3.1-DEV(in smarty.class.php)

possibe problem of require plugin:
http://www.smarty.net/forums/viewtopic.php?t=25425&postdays=0&postorder=asc&start=15&

--------------------------
smarty behavior

%main% :
Code:
{require 'test'}
{call name=func1 nocache}


test :
Code:
{function name=func1}
{nocache}loaded{/nocache}
{/function}


first run (no exists problem):

smarty/compile/ and smarty/cache/

http://wikisend.com/download/591432/data1.zip

output :
Code:
loaded


second run(with error)


smarty/compile/ and smarty/cache/

http://wikisend.com/download/828004/data2.zip

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


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

PostPosted: Sat Mar 14, 2015 11:21 am    Post subject: Reply with quote

One general note:
Template functions are always globally defined, normally regard less if the definition is in the main or a sub template.

Your problem was caused again by your modification of your special {require} tag for variable template names.
Smarty was not able to resolve nocache calls of template function which are defined in subtemplate with their own cache file.

The fix is on github and will later be included in 3.1.22
Back to top
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Sat Mar 14, 2015 1:09 pm    Post subject: Reply with quote

U.Tews wrote:
One general note:
Template functions are always globally defined, normally regard less if the definition is in the main or a sub template.

Your problem was caused again by your modification of your special {require} tag for variable template names.
Smarty was not able to resolve nocache calls of template function which are defined in subtemplate with their own cache file.

The fix is on github and will later be included in 3.1.22


thanks.

but yet this problem is exists in another type :

last bug is fix ok. but see example :

before test this example remove any compile or cache file in smarty

test :
Code:
{$a = 'ok' nocache}


%main% :
Code:
{require 'test'}
{$a nocache}


output : ok

no problem up here.

but now replace above template with (no remove any compile file or cache file)

test :
Code:
{function name=func}
{nocache}loaded{/nocache}
{/function}


%main% :
Code:
{require 'test'}
{call name=func nocache}


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


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

PostPosted: Sat Mar 14, 2015 1:51 pm    Post subject: Reply with quote

What is your compile_check setting?

Does mysql have correct timestamp corresponding to the timestamps of the file system?
Did you make your changes with the correct user_ID ß

iT DOES WORK HERE:
Back to top
View user's profile Send private message
sm@rty
Smarty Regular


Joined: 01 Oct 2014
Posts: 65

PostPosted: Sat Mar 14, 2015 7:04 pm    Post subject: Reply with quote

Quote:
What is your compile_check setting?


is default setting.

Quote:
Does mysql have correct timestamp corresponding to the timestamps of the file system?

i always using php timestamp and not using mysql server timestamp.
In addition, timestamp in php and mysql is same.

Quote:
Did you make your changes with the correct user_ID ß

yes

in above example :

in first run status of compile and cache

http://wikisend.com/download/609390/run 1.zip

in second run(when run give error Unable to find template function 'func')

http://wikisend.com/download/429150/run 2.zip

in third run (does work) status of compile and cache

http://wikisend.com/download/491700/run 3.zip

run == refresh page

if you think problem is in timestamp please see the example below :

%main%
Code:
{require 'test'}
{$a nocache}


test
Code:
{$a = 1 nocache}


output : 1

now i updating template test to :
Code:
{$a = 2 nocache}


output : 2

so no problem in timestamp.
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 1:11 am    Post subject: Reply with quote

Smarty did internally restore the compile_check status too late during update of the first cache file.

This fixed now.
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 -> General All times are GMT
Page 1 of 1

 
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