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

Smarty 2.x {if $variable} - Smarty 3.x{if isset($variable)}

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


Joined: 26 Apr 2013
Posts: 3

PostPosted: Tue Dec 30, 2014 5:29 pm    Post subject: Smarty 2.x {if $variable} - Smarty 3.x{if isset($variable)} Reply with quote

Hello everybody!
I'm just updating my website from php4 to php5 and I foud that also smarty has a new version for php5...so I was just changing the variables and things like that when I've noticed that in Smarty 3.x is not possible to check the variable exists using only {if $var} but every time I have to write {if isset($var)}... and also when a smarty variable in the tpl code is not assigned(smarty ->assign("var",$var)) gives me a notice telling me that the variable has problem in the evaluation and is not an object...
Is it possible to add few lines of code(also tell me where should I add it and what) or a plugin for changing this new logic?
Thaks
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue Dec 30, 2014 9:17 pm    Post subject: Reply with quote

You're doing it wrong to begin with.
If your template addressing unassigned variables, there's an issue with your template or your code, that calling it.
Properly written code should not raise any kind of errors unhandled.
If your tempate contains blocks, that should be rendered differently depending on specific criteria, separate them into subtemplates.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Tue Dec 30, 2014 10:21 pm    Post subject: Reply with quote

Smarty 2 did always turn E_NOTICE error messages off during display() and fetch() calls. You had no chance to detect unassigned variables by error messages.

Smarty 3 provides full control of the error_reporting() setting during template processing by setting the $smarty->error_reporting property,

For example with
Code:
$smarty->error_reporting = E_ALL & ~E_NOTICE;

you will have the same behaviour as in Smarty 2
Back to top
View user's profile Send private message
Neoprogrammer
Smarty n00b


Joined: 26 Apr 2013
Posts: 3

PostPosted: Sat Jan 03, 2015 1:29 pm    Post subject: Reply with quote

Ok...
But follow me during this train of thoughts...
I have
<div class="slogn">
{if $smarty.session.id}
<p>Benvenuto</p>
<div class="navigation2">

<div id="smooth1" class="ddsmooth">
<ul>
<li>
<a href="">{$smarty.session.nickname}</a>
<ul>
<li><a href="yaccount.php">Dati Personali</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</li>
</ul>
</div>
</div>
{else}<p>Benvenuto su Bandevent.org!</p>{/if}
</div>
isn't it too much to specify {if isset($smarty.session.id)}?...every time i'm not using this dam "isset" thing it gives me notice...the code will be more agile without specifing "isset" everytime...
Also variable in pho null are really problematic to set...
Any help?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Jan 03, 2015 4:44 pm    Post subject: Reply with quote

Simple solution - always fill session data.
With generic "Guest/none/none" or with actual data from authentication.
Also don't forget to include session data in nocache blocks.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Sat Jan 03, 2015 7:59 pm    Post subject: Reply with quote

As I said before; You dont need isset and can have null variables as in Smarty2 if you set this in your PHP script

Code:
$smarty->error_reporting = E_ALL & ~E_NOTICE;
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