DEV Community

dgloriaweb
dgloriaweb

Posted on

Php Smarty template ternary operator to reduce code in html tags

Hi Sweet People!
I've googled this, and found a solution with no explanation so I tested and came up with this example.

<head>
{$bloke = true}
{$sex = ($bloke) ? 'Male' : 'Female'}
</head>
Enter fullscreen mode Exit fullscreen mode

After entering {debug} mode, I can see this returning 'Male' all right.
Image description

Top comments (3)

Collapse
 
andersbjorkland profile image
Anders Björkland

How do you like Smarty? I tried checking our their page but I'm currently on mobile and the site wasn't very mobile friendly - so I have no clue.

Collapse
 
dgloriaweb profile image
dgloriaweb

Hi, I have no choice, it's the decision of the company I work for. So I better master it. ;)

Collapse
 
sharakpl profile image
Tom

Thanks. I couldn't make it work in my template. Turned out parentheses are required in smarty: (condition).