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

Possible bugs with template functions

 
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 -> Smarty 3
View previous topic :: View next topic  
Author Message
artvolk
Smarty Regular


Joined: 09 Dec 2009
Posts: 45

PostPosted: Wed Dec 09, 2009 7:07 am    Post subject: Possible bugs with template functions Reply with quote

I have the following test case for beta5 and SVN versions:

Parent template:
Code:

{block name='content'}
{/block}


Inherited template:
Code:

{extends file='parent.tpl'}
{block name='content'}

{function name='just_to_test'}
   Title we passed to the function: {$title}
{/function}

{* test case 1: The function is called only once after file change if $force_compile=false if function is defined in template with {extends} *}
   {just_to_test title='test'}

{* test case 2: {t} is my custom block for gettext in templates registered using register_block() *}

   {* This works: *}
   {t}Passing value through Smarty-registered function using register_block(){/t}

   {* This doesn't work: the value without processing is passed to template function*}
   {just_to_test title="{t}Passing value through Smarty-registered function using register_block(){/t}"}

{/block}


If you a complete test case (with php/templates) will help you (save your tile) just let me know.

Thanks!
Back to top
View user's profile Send private message
artvolk
Smarty Regular


Joined: 09 Dec 2009
Posts: 45

PostPosted: Wed Dec 09, 2009 7:24 am    Post subject: Reply with quote

And a little bonus Smile

I've tried to place

Code:

{function name='just_to_test'}
   Title we passed to the function: {$title}
{/function}


to separate file and include it:

Code:

{include file='test.tpl'}
{just_to_test title='test'} {* Got Syntax Error in template, unknown tag *}
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Dec 09, 2009 5:12 pm    Post subject: Reply with quote

Quote:
{just_to_test title="{t}Passing value through Smarty-registered function using register_block(){/t}"}


Here the problem is that block tags do not work inside double quoted strings and I'm not sure that they ever will. The compiler should drop an error message, but currently it does just generate invalid code. I will fix that in the near future.

The solution would be to add an assign attribute to your block function. Then the following would work:
Code:
{t assign=foo}Passing value through Smarty-registered function using register_block(){/t}
{just_to_test title=$foo}


Quote:
{include file='test.tpl'}
{just_to_test title='test'}


Template functions defined with the {function} tag in a subtemplate can't be accessed by the parent template. The problem is that when the parent template gets compiled it has no knowledge of the content of any subtemplate. So when you call {just_to_test} it's an unknow tag. I see the point that it would be nice to be able to load a library of template functions, but currently have no solution for that with reasonable overhead.

Currently you could fake the compiler with a dummy definition of the function which gets overloaded at run time with the real one.

Code:
{function name='just_to_test'}{/function}
{include file='test.tpl'}
{just_to_test title='test'}
Back to top
View user's profile Send private message
artvolk
Smarty Regular


Joined: 09 Dec 2009
Posts: 45

PostPosted: Wed Dec 09, 2009 6:41 pm    Post subject: Reply with quote

First of all thank you for the quick reply!

First problem: Block tags do not work inside double quotes
Quote:

Here the problem is that block tags do not work inside double quoted strings and I'm not sure that they ever will. The compiler should drop an error message, but currently it does just generate invalid code. I will fix that in the near future.

The solution would be to add an assign attribute to your block function. Then the following would work:

The problem is that we are striving not to use {capture} or {assign} for such task, this will simplify templates a lot. My hope was that in
Smarty 3 we could use block functions in such context. May be there is some shorter (in syntax terms) workaround?

Second problem: Functions if placed in child template work only once.
I can reproduce the bug only if I place function inside {block} tag in child template, like this:

parent.tpl:
Code:

{block name='content'}
{/block}


child.tpl:
Code:

{extends file='parent.tpl'}
{block name='content'}

{function name='just_to_test'}
   Title we passed to the function: {$title}
{/function}

{* test case 1: The function is called only once after file change if $force_compile=false if function is defined in template with {extends} *}
   {just_to_test title='test'}
{/block}


If I set force $force_compile = true, it works as expected

Third problem: Includes do not work in child template. Please note, that I use function in child template, not parent one.

This works (with problem #2)

Code:

{extends file='parent.tpl'}
{block name='content'}

{function name='just_to_test'}
   Title we passed to the function: {$title}
{/function}

   {just_to_test title='test'}
{/block}


but this doesn't:

Code:

{extends file='parent.tpl'}
{block name='content'}
   {include file='file_with_my_function.tpl'}
   {just_to_test title='test'}
{/block}


The only change is that I place function to separate file. I've tried to place include in parent template -- the same result, may be I'm missing something?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Dec 09, 2009 8:25 pm    Post subject: Reply with quote

Quote:
Second problem: Functions if placed in child template work only once.

This is a bug in beta-5. It was fixed Nov 30 in the SVN.

Quote:
Third problem: Includes do not work in child template.

My wording was not the best in my last reply. Functions defined in included templates can not be accessed from the outerlevel templates. That has nothing to do with template inheritance.
Back to top
View user's profile Send private message
artvolk
Smarty Regular


Joined: 09 Dec 2009
Posts: 45

PostPosted: Wed Dec 09, 2009 8:29 pm    Post subject: Reply with quote

U.Tews wrote:
Quote:
Second problem: Functions if placed in child template work only once.

This is a bug in beta-5. It was fixed Nov 30 in the SVN.

I'm using this SVN version via SVN externals:
http://smarty-php.googlecode.com/svn/branches/Smarty3Dev/distribution/libs
it seems the bug is still here.

Quote:

Quote:
Third problem: Includes do not work in child template.

My wording was not the best in my last reply. Functions defined in included templates can not be accessed from the outerlevel templates. That has nothing to do with template inheritance.

Ok, so in other words functions are accessible only inside template file where they are defined. Right?
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Dec 09, 2009 8:48 pm    Post subject: Reply with quote

Quote:
Second problem: Functions if placed in child template work only once.


I just retested the SVN version with your sample code. It does work.

Quote:
Ok, so in other words functions are accessible only inside template file where they are defined. Right?


And any template which is included by the defining template. But not the other way around.
Back to top
View user's profile Send private message
artvolk
Smarty Regular


Joined: 09 Dec 2009
Posts: 45

PostPosted: Wed Dec 09, 2009 9:11 pm    Post subject: Reply with quote

First of all thank you for all you replies, I really appriciate you attention to my stupid questions Smile

U.Tews wrote:
Quote:
Second problem: Functions if placed in child template work only once.


I just retested the SVN version with your sample code. It does work.


I use the SVN revision 3392 (5 December 2009, - removed unneeded lexer code)

Here is the minimal set of files to reproduce:

Code:

{block name='content'}
{/block}


Code:

{extends file='parent.tpl'}
{block name='content'}

{function name='just_to_test'}
   Title we passed to the function: {$title}
{/function}

{just_to_test title='test'}

{/block}


index.php
Code:

<?php
include('smarty/Smarty.class.php');
$smarty = new Smarty();
$smarty->display('child.tpl');
?>


First refresh of the page will run the function, second -- not.

U.Tews wrote:

Quote:
Ok, so in other words functions are accessible only inside template file where they are defined. Right?


And any template which is included by the defining template. But not the other way around.

Thanks! Your trick with redefining empty functions works, I'll use it!
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed Dec 09, 2009 10:29 pm    Post subject: Reply with quote

Quote:
Second problem: Functions if placed in child template work only once.


Strange: Somehow the SVN got out of sync. I'll try to fix this ASAP.
Back to top
View user's profile Send private message
artvolk
Smarty Regular


Joined: 09 Dec 2009
Posts: 45

PostPosted: Thu Dec 10, 2009 9:21 am    Post subject: Reply with quote

U.Tews wrote:
Quote:
Second problem: Functions if placed in child template work only once.


Strange: Somehow the SVN got out of sync. I'll try to fix this ASAP.

Thank you for feedback, just drop me a line in this thread and I'll retest it.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Thu Dec 10, 2009 5:21 pm    Post subject: Reply with quote

The fix is in the SVN now
Back to top
View user's profile Send private message
artvolk
Smarty Regular


Joined: 09 Dec 2009
Posts: 45

PostPosted: Thu Dec 10, 2009 6:54 pm    Post subject: Reply with quote

Thanks! It seems to work 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 -> Smarty 3 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