FBML contains many conditional tests that can help you cut down on implementing this code in your application. At the heart of these conditionals is the <fb:if> tag:.
<fb:if value="true">
<p>Hi</p>
</fb:if>
At first glance, this isn’t that useful because the value attribute will always be true. This is where your programming language comes into play. To actually make this do something useful, you need to be able to dynamically set this value. Let’s write a short test to see whether the logged-in user has a user ID of 12345 and show a customized message:
<?php
$user = $facebook->require_login();
$test = false; // you may also use 1/0 for true/false
if($user == 0000001){
$test = 1;
}
?>
<fb:if value="<?php echo($test)?>">
<p>This is the secret message.</p>
<fb:else>
<p>No secret message for you!</p>
</fb:if>
This is a nonsense example, but it shows how you can you use the <fb:if>/<fb:else> construct to display custom messages to your users. You will find that through your application development process you will start constructing more complex <fb:if>/<fb:else> statements. Fortunately, the developers of the Facebook platform anticipated this and have a set of tags that will do many of the most common types of conditional checking.
As I stated earlier, Facebook tags act differently in different sections of the web pages. These conditional checks can occur only on the canvas page of your application:
  • <fb:is-in-network> displays content if a UID is in the specified network.
  • <fb:if-can-see> displays content if the logged-in user can view the specified content. This is often used to implement your own privacy features in your applications.
  • <fb:if-can-see-photo> displays content if the user is logged on and has permissions to view the photo specified.
  • <fb:if-is-app-user> displays content if the specified user has accepted the terms of service for the application.
  • <fb:if-is-friends-with-viewer> displays content if the user specified is friends with the logged-in user.
  • <fb:if-is-group-member> displays content if the user is a member of the specified group.
  • <fb:if-is-own-profile> displays content if the viewer is the profile owner
  • <fb:if-is-user> displays content if the viewer is one of the specified users.
  • <fb:if-user-has-added-app> displays content if the specified user has added the application to their account.

Unfortunately, there isn’t an FBML construct for else if logic. If you need to perform multiple conditional checks, you will need to properly nest your if statements. Alternatively, you can use the FBML’s switch construct.

The FBML <fb:switch> tag acts a bit differently than many programming languages that implement the construct. In FBML, the <fb:switch> tag evaluates a set of FBML tags and returns the tag information from the first tag that returns something other than an empty string:

<fb:switch>

<fb:user uid="0000001" />

<fb:default>This is the default statement</fb:default>

</fb:switch>


This code will display the contents of the <fb:default> tag since there’s no user with a UID of 0000001. You may at some point need something a bit more complex for your tests. You are able to nest <fb:if> and <fb:switch> statements within an <fb:switch> tag for these more advanced conditional analyses in your code:

<?php

$user = $facebook->require_login();

$test = false;

if($user == 0000001){

// Boolean true = 1

$test = 1;

}

?>

<fb:switch>

<fb:if value="<?php echo($test)?>">

<fb:switch>

<fb:profile-pic uid="<?php echo($user)?>" />

<fb:default>Inner default</fb:default>

</fb:switch>

</fb:if>

<fb:default>Outer Default</fb:default>

</fb:switch>

As you’ve probably noticed, there’s no case statements with breaks that you normally see in other programming languages. If you’re familiar with the switch statements having case and break statements, just think of each tag as a distinct case with no need for a break statement. There are times where this could require more complex nesting of statements, but if you find your conditional statements getting too complicated, it’s probably a good idea to take a step back from what you’re doing and see whether you can find an alternative method to perform the same check. Also, as a programming note, the switch statement essentially has a break after the first true statement in the switch statement. If you place the <fb:default> tag at the top of your code block (which will always return true), the rest of your switch statement will not get evaluated.

0 comments