Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Standard if statement

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Game Over!
Smarty Rookie


Joined: 01 May 2019
Posts: 8

PostPosted: Wed Sep 04, 2019 11:38 am    Post subject: Standard if statement Reply with quote

Hello guys.

I just wanna know is there any difference between these two? If there is , so which one is better ? which one has more performance ?

Code:

<div class="container">
       <div class="row">
              <h1>Test</1>
              {if $Gender eq "male" }
                     <p>Hello Mr</p>
              {else}
                     <p>Hello Mrs</p>
              {/if}
       </div>
</div>


or

Code:

{if $Gender eq "male" }
<div class="container">
        <div class="row">
              <h1>Test</1>
              <p>Hello Mr</p>
        </div>
</div>
{else}
<div class="container">
       <div class="row">
              <h1>Test</1>
              <p>Hello Mrs</p>
       </div>
</div>
{/if}
Back to top
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 245
Location: West Coast

PostPosted: Wed Sep 04, 2019 8:52 pm    Post subject: Reply with quote

Two <div> blocks are going to be sent out anyway. The first example is more normalized than the second, but having:
Code:
{if $Gender eq "male"}Mr{else}Mrs{/if}
is even more normalized. That is, every bit of common code is outside anything that could change, and appears once -- easiest to make changes.
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1572

PostPosted: Sun Sep 08, 2019 5:15 pm    Post subject: Reply with quote

Only one block will be sent to the user, but which approach is better is a nontrivial question.
In this specific case, the first one I would prefer.
But YMMV.
Back to top
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 245
Location: West Coast

PostPosted: Sun Sep 08, 2019 7:13 pm    Post subject: Reply with quote

"Only one block will be sent"

The 'container' and the nested 'row'. Slapdash notes.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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