then

How to write if/then/elseif/else in Drupal 8 Twig templates

As a quick note Drupal programming note, here’s an example of how to write if/then/elseif/else in Drupal 8 Twig templates:

{% if node.getType == 'photo' %}
...
{% elseif node.getType in ['book', 'page'] %}
...
{% else %}
...
{% endif %}

While I’m in the neighborhood, here are a few more if conditions I’ve written recently:

Scala: How to add ‘if’ expressions (guards) to match/case expressions

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.13, “How to add 'if' expressions (guards) to case statements.”

Problem

You want to add qualifying logic to a case statement in a match expression, such as allowing a range of numbers, or matching a pattern, but only if that pattern matches some additional criteria.

Solution

Add an if guard to your case statement. Use it to match a range of numbers:

How to use a Scala if/then statement like a ternary operator

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.6, “How to use a Scala if/then statement like a ternary operator.”

Problem

You’d like to use a Scala if expression like a ternary operator to solve a problem in a concise, expressive way.

Solution

This is a bit of a trick problem, because unlike Java, in Scala there is no special ternary operator; just use an if/else expression:

Scala control structure examples (if/then, match/case, for, while, try/catch)

This post contains a collection of Scala control structures examples. I initially created most of these in the process of writing the Scala Cookbook. Unlike the Cookbook, I don’t describe them much here, I just show the examples, mostly as a reference for myself (and anyone else that can benefit from them).

if/then control structures:

Here are some examples of the Scala if/then control structure:

Scala if then else syntax (and returning a value from an if statement)

Scala FAQ: Can you share some examples of the Scala if/then/else syntax? Also, can you show a function that returns a value from an if/then/else statement?

In its most basic use, the Scala if/then/else syntax is very similar to Java:

A better test for detecting Unix operating systems in an Ant build script

In several previous tutorials (see my references below) about testing for operating systems within Ant build scripts, and then conditionally executing targets based on the results of those tests, I noted that Mac OS X operating systems respond to both Mac and Unix test conditions based on the Ant "os family" test. I mentioned that I thought this behavior was probably correct, because Mac OS X is built an a Unix base (BSD, to be specific).

Perl ‘equals’ FAQ: What is true and false in Perl?

Perl true/false FAQ: What is true in Perl? What is false in Perl?

The Perl programming language is a little unusual in not having true and false boolean operators. Because of this, I can never seem to remember what equates to true and false in Perl, so I decided to create this page.

What is true/false in Perl

In short, the following elements evalue to false in Perl: