if

How to get the URI of the current node in a Drupal 8 Twig template theme file

As a quick note, I was just able to get the URI of the current page (node) in a Drupal 8 Twig template theme file using this code:

{% set uri = path('entity.node.canonical', {'node': node.id}) %}

As an example, I’m rendering some different content based on the URI of the current node, so I first use that code to set the uri field, then I have a little Twig if/then/else condition like this:

{% if uri starts with '/foo' %}
    <div>Option Foo here ...</div>
{% elseif uri starts with '/bar' %}
    <div>Option Bar here ...</div>
{% else %}
    <div>Option Baz here ...</div>
{% endif %}

In summary, if you wanted to see how to get the URI of the current page/node when using a Drupal 8 Twig theme template file, I hope this example is helpful.

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:

How to use a Scala ‘for’ loop with embedded ‘if’ statements (guards)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.3, “How to use a 'for' loop with embedded 'if' statements (guards).”

Problem

You want to add one or more conditional clauses to a for loop, typically to filter out some elements in a collection while working on the others.

Solution

Add an if statement after your generator, like this:

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 for loop syntax examples (including yield and guards)

Scala FAQ: Can you share some examples of the Scala for loop syntax?

Sure. I'm going to start with a comparison to Java for loops, because that's what I was just thinking about.

In Java you might write a for loop with a counter like this:

for (int i = 0; i < 10; i++) {
  System.out.println(i);
}

The equivalent for loop in Scala looks like this:

for (i <- 1 to 10) {
  println(i)
}

(The use of parentheses isn’t necessary in either example, but most for loops will be longer.)

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: