Condition Examples for Templex
The if/else feature allows you to conditionally render content based on variable values, comparisons, and boolean logic.
Basic Syntax
Section titled “Basic Syntax”<{ if( condition ) }> Content when true<{ endif }>
<{ if( condition ) }> Content when true<{ else }> Content when false<{ endif }>Examples
Section titled “Examples”Variable Self-Evaluation
Section titled “Variable Self-Evaluation”A variable on its own evaluates as truthy or falsy:
<{ if( $isLoggedIn ) }> <p>Welcome back!</p><{ endif }>Truthy values: true, 1, "true", any non-empty string or non-zero number.
Falsy values: false, 0, "false".
Boolean Literals
Section titled “Boolean Literals”<{ if( true ) }> This is always shown<{ endif }>
<{ if( false ) }> This is never shown<{ endif }>String Comparison
Section titled “String Comparison”<{ if( $name === 'Fred' ) }> <p>Hello Fred!</p><{ else }> <p>Hello stranger!</p><{ endif }>Numeric Comparison
Section titled “Numeric Comparison”<{ if( $age >= 18 ) }> <p>Adult content available</p><{ endif }>
<{ if( $score > 100 ) }> <p>High score!</p><{ else }> <p>Keep trying</p><{ endif }>Variable to Variable Comparison
Section titled “Variable to Variable Comparison”<{ if( $userRole === $requiredRole ) }> <p>Access granted</p><{ else }> <p>Access denied</p><{ endif }>Not Equal
Section titled “Not Equal”<{ if( $status != 'disabled' ) }> <p>Feature is available</p><{ endif }>
<{ if( $count !== 0 ) }> <p>Items found: <{ $count }></p><{ endif }>Nested Conditions
Section titled “Nested Conditions”Conditions can be nested within other control structures:
<{ foreach( $users as $user ) }> <{ if( $user === 'admin' ) }> <li class="admin"><{ $user }> (Admin)</li> <{ else }> <li><{ $user }></li> <{ endif }><{ endforeach }>Elseif Chains
Section titled “Elseif Chains”Chain multiple conditions without nesting:
<{ if( $status === "active" ) }> <span class="green">Active</span><{ elseif( $status === "pending" ) }> <span class="yellow">Pending</span><{ elseif( $status === "disabled" ) }> <span class="red">Disabled</span><{ else }> <span class="grey">Unknown</span><{ endif }>Multiple elseif branches are supported. The first matching condition wins, and the else branch is optional.
Conditions Inside For Loops
Section titled “Conditions Inside For Loops”<{ for( $i = 1; $i <= 10; $i++ ) }> <{ if( $i === $currentPage ) }> <span class="active"><{ $i }></span> <{ else }> <a href="?page=<{ $i }>"><{ $i }></a> <{ endif }><{ endfor }>Negation
Section titled “Negation”Negate any condition with !:
<{ if( !$banned ) }> <p>Welcome back.</p><{ endif }>
<{ if( !false ) }> Always shown<{ endif }>Negation works with logical operators:
<{ if( !$banned && $active ) }> Active and not banned<{ endif }>Logical Operators
Section titled “Logical Operators”Combine multiple conditions with && (and) and || (or):
<{ if( $isLoggedIn && $isAdmin ) }> <p>Welcome, administrator.</p><{ endif }>
<{ if( $role === "admin" || $role === "editor" ) }> <p>You can edit content.</p><{ endif }>Chain three or more conditions:
<{ if( $a && $b && $c ) }> All three are truthy<{ endif }>Logical operators work with comparisons, self-evaluation, and inside elseif branches:
<{ if( $role === "admin" ) }> Admin<{ elseif( $role === "editor" || $role === "author" ) }> Writer<{ else }> Guest<{ endif }>Supported Operators
Section titled “Supported Operators”| Operator | Description |
|---|---|
== |
Equal (loose) |
=== |
Equal (strict) |
!= |
Not equal (loose) |
!== |
Not equal (strict) |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal |
<= |
Less than or equal |
&& |
Logical AND |
|| |
Logical OR |
! |
Negation |
Supported Value Types
Section titled “Supported Value Types”- Variables:
$name,$count - String literals:
'hello',"world" - Numbers:
7,49 - Booleans:
true,false
Usage with PHP
Section titled “Usage with PHP”use Myerscode\Templex\Templex;
$templex = new Templex(__DIR__ . '/templates/');
$data = [ 'isLoggedIn' => true, 'name' => 'Fred', 'role' => 'admin', 'score' => 150, 'currentPage' => 3,];
$result = $templex->render('my-template.stub', $data);echo $result;