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

Have problem template variable inside {php}...{/php}

 
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
saiful
Smarty n00b


Joined: 19 Jun 2014
Posts: 3

PostPosted: Fri Jun 20, 2014 3:04 am    Post subject: Have problem template variable inside {php}...{/php} Reply with quote

Hello every one, hope all of you passing nice days. I need a small help.
I am very new in smapty. I am using smarty with codeigniter first time. Now i have a problem to find the difference between two date. Join date and resign comes from a for loop. I attach my code block here.
----------------------------------------------------------------------------------
{foreach $data.exp_info as $exp}
{assign var="ex_join_year" value="{$exp->ex_join_year}"}
{assign var="ex_resign_year" value="{$exp->ex_resign_year}"}
{assign var="ex_join_month" value="{$exp->ex_join_month}"}
{assign var="ex_resign_month" value="{$exp->ex_resign_month}"}

{php}

$date1 = $smarty->get_template_vars('ex_join_year');
$date2 = $smarty->get_template_vars('ex_resign_year');

//$date1 = "2007-Jan"; sample date
//$date2 = "2007-feb"; sample date

$diff = abs(strtotime($date2) - strtotime($date1));

$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf("%d years, %d months\n", $years, $months);
{/php}
{/foreach}

---------------------------------------------------------------------------------
ex_join_year, ex_resign_year, ex_join_month, ex_resign_month
they are smarty template variable, so i cant use them directly under {php}.......{/php}. But i need some run time process using those variables to calculate difference between two days.
it shows error when i assign those template variables inside {php}......{/php}
it generates these errors

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: smarty
Filename: plugins/block.php.php(23) : eval()'d code

Line Number: 3
Fatal error: Call to a member function get_template_vars() on a non-object in C:\wamp\www\hms\application\libraries\smarty\libs\plugins\block.php.php(23) : eval()'d code on line 3

Thanks every one
Back to top
View user's profile Send private message Send e-mail
U.Tews
Administrator


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

PostPosted: Fri Jun 20, 2014 12:17 pm    Post subject: Reply with quote

Inside compiled templates the Smarty or Template object must be accessed by an internal special name $_smarty_tpl

like
Code:
$_smarty_tpl->get_template_vars('ex_join_year');
Back to top
View user's profile Send private message
saiful
Smarty n00b


Joined: 19 Jun 2014
Posts: 3

PostPosted: Thu Jul 17, 2014 5:46 am    Post subject: Reply with quote

Hello U.Tews

I tried this,
$_smarty_tpl->get_template_vars('ex_join_year');

But my problem not solved.

i coded like this: $date1 = $_smarty_tpl->get_template_vars('ex_join_year');

and it shows the following errors.

------------------------------------------------------------------------
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: _smarty_tpl

Filename: plugins/block.php.php(23) : eval()'d code

Line Number: 8
-----------------------------------------------------------------------
Back to top
View user's profile Send private message Send e-mail
U.Tews
Administrator


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

PostPosted: Thu Jul 17, 2014 2:35 pm    Post subject: Reply with quote

Sorry, I was wrong at my last reply.

{php} {/php} is implemented in SmartyBC like a block plugin.

Within that the template object is accessable with $template and the Smarty object with $template->smarty.

So use
Code:
$date1 = $template->get_template_vars('ex_join_year');
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