Search code examples
if-statementconditional-statementsfat-free-framework

Fat-Free Framework Template Conditional Rendering Issue on Template


I'm using the Fat-Free Framework (F3) and trying to implement conditional rendering in attribute part of a HTML tag on the template. Specifically, I want to set the selected attribute for a option based on a user's access level.

<select name="access_level">
   <option value="Simple User" {{ @User['access_level'] === "Simple User" ? "selected" }}>
        Simple User
   </option>
   <option value="Administrator" {{ @User['access_level'] === "Administrator" ? "selected" }}>
        Administrator
    </option>
</select>

However, I’m getting a

Internal Server Error syntax error, unexpected token ")" [C:Users\user\Documents\Development\Tutorials\AuthF3Mapper\tmp\1rhmuqyegwrps.2qdvx1lj1r40s.php:60]

when trying to use the ternary operator inside the template. I believe F3 doesn’t recognize this syntax.

I understand that the condition should print the selected attribute if the condition is true and nothing if it’s false. The issue is in the template engine itself not recognizing the ternary operator at on attribute part HTML tags .

What is the correct syntax for my case, or is there another way to achieve this?


Solution

  • You're just missing the second part of the ternary. For example:

    {{ @User['access_level'] === "Simple User" ? "selected" : "" }}
    

    The F3 template is basically just raw PHP. And in raw PHP you can't leave off the :