[IPB] HTML logic guide

.Chris

Adherent
Joined
Jun 9, 2004
Messages
300
.Chris submitted a new Article:

[IPB] HTML logic guide

Invision Power Board (IPB) comes with a feature allowing you to carry out basic conditional statements in the templates. This means you can carry out simple actions without the need to edit any files. You can not however use HTML logic in the board header & footer wrappers, it can only be used in the template bits. The HTML logic syntax was overhauled in 2.2.

If you are new to PHP or programming in general I suggest you check out the PHP language reference guide.

General syntax

Here is an example of a simple if statement:

Code:
<if="$this->ipsclass->member['id'] == 0">
Hello, guest
</if>
Notice the similarity with PHP's syntax, if we were to do the same thing in a PHP file it would look like:

Code:
if($this->ipsclass->member['id'] == 0)
{
echo 'Hello, guest';
}
If statements

You can carry out an if statement by doing:

Code:
<if="$variable == test">
Variable equals test
</if>
Unlike PHP, you don't need to put quotes around strings. You can if you want though. However, make sure not to nest the same type of quote marks.

You can use all the PHP comparison operators in HTML logic. For example:

Code:
<if="$a > $b">
$a is greater than $b
</if>
As well as comparing variables, you can also use PHP functions such as count() and is_array() in <if> statements. So, if we wanted to check if $variable was an array we can do:

Code:
<if="is_array($variable)">
$variable is an array!
</if>
From IPB 2.2 onwards you can nest if statements. This means you can have an if statement inside another. For example:

Code:
<!--1st if-->
<if="$this->ipsclass->member['id'] > 0">
This will be displayed if you are a member
       <!--2nd if-->
       <if="$this->ipsclass->member['id'] == 1">
       This will be displayed if your member id is 1
        </if>
</if>
However, IPB does not allow you to use an <else> statement after the 2nd if.

Else statements

else statements allow you to output something if a condition has not been met in an if statement. For example:

Code:
<if="$a > $b">
$a is greater than $b
</if>
<else />
$a is not greater than $b
</if>
An example where this would be useful is for member & guest links:

Code:
<if="$this->ipsclass->member['id'] == 0">
Welcome Guest! Click here to...etc
</if>
<else />
Welcome...

Read more about this article here...
 
Last edited by a moderator:

mity

Neophyte
Joined
Oct 31, 2007
Messages
3
Your post has helped me a lot, I just have 1 more question:

If I'm using
<!--exec.url="http://www.mydomain.com/script.php"-->

and this is working to activate the contents of the php file.

But then in the php file I want to print something on the screen if the site visitor is a guest but not a member what do I put in the file?

I have tried:
if($this->ipsclass->member['id'] == 0)
{
echo 'Hello, guest';
}

But this gives me an error because of the $this

Any help appreciated - the conents are couing in the footer which I guess is a wrapper
 

RubberToe

Neophyte
Joined
Aug 9, 2010
Messages
2
Sorry if I shouldn't bring up an old post, but I have a question.

Is there a way I can test an Array with in If statement for the amount of characters, or the amount of lines within the Array.

ie:
<if test="$post > 100">DO</if>

Where 100 is the amount of characters (or better yet if I could put 3, for the amount of lines) that is within the Array.

-Thanks
 

thermal

Aspirant
Joined
Jul 6, 2011
Messages
11
The "struts-logic" tag library contains tags that are useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management.
 
Last edited:
Top