网络营销电子商务研究中心

网络营销电子商务研究中心 (https://www.0058.net/index.php)
-   vBulletin技术交流 (https://www.0058.net/forumdisplay.php?f=6)
-   -   Template Conditionals IF (https://www.0058.net/showthread.php?t=4530)

topvip 2013-05-06 07:52 AM

Template Conditionals IF
 
Template Conditionals
Template Conditionals are a powerful tool for controlling the XHTML output from your templates. They allow you to create simple if/else branches within your templates, in order to display different content depending on the condition you specify.

For example you may want to show a different welcome message on the front page of your board to registered users and to guests. The way to know whether or not the person visiting a page is a guest, or a logged-in user is to check the value of $bbuserinfo[userid]. If the value is 0, the visitor is a guest (or not logged-in), otherwise the visitor is a registered member.

This is a simple conditional to show a welcome message to guests only.
Code:

<vb:if condition="$bbuserinfo['userid'] == 0">
    <p>Welcome to the forum!<br />
    If this is your first visit, we hope you enjoy your stay!</p>
</vb:if>

The previous example used a simple 'if' condition. We can extend that to include an 'else' condition, which will be used if the 'if' condition is not fulfilled.

This example extends the previous conditional to show a different message to registered members from that shown to guests.

Code:

<vb:if condition="$bbuserinfo['userid'] == 0">
    <p>Welcome to the forum!<br />
    If this is your first visit, we hope you enjoy your stay!</p>
<vb:else />
    <p>Welcome back, $bbuserinfo[username].<br />
    <a href="search.php?do=getnew">Click here to view new posts</a>.
</vb:if>

Starting in vBulletin 4.0 template conditionals natively support 'else if' as well.

Code:

<vb:if condition="$my_variable == 1">
    <p>My variable is equal to one.</p>
<vb:elseif condition="$my_variable == 2" />
    <p>My variable is equal to two.</p>
<vb:else />
    <p>My variable is equal to neither one nor two.</p>
</vb:if>

Sample:

<vb:if condition="THIS_SCRIPT == 'index'">
<p>My variable is equal to one.</p>
<vb:else />
<p>My variable is not equal to one.</p>
</vb:if>


<if condition="THIS_SCRIPT == 'index'">

</if>


All times are GMT +8. The time now is 10:54 AM.

Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.