syntax

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:

Drupal 8 Twig templates comments syntax

As a quick note, the Drupal 8 Twig template comment syntax looks like this:

{# this is a comment #}

The Twig multiline comment syntax uses the same characters:

{#
   This is a comment.
   More comments ...
   Even more comments ...
#}

How to use a Maven repository library with SBT

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.17, “Using a Maven Repository Library with SBT.”

Problem

When working on a Scala project built with SBT, you want to use a Java library that’s in a Maven repository, but the library doesn’t include information about how to use it with Scala and SBT.

How to Write Scala Functions That Take Functions as Input Parameters alvin May 28, 2017 - 6:51pm

“Haskell functions can take functions as parameters and return functions as return values. A function that does either of those is called a higher order function. Higher order functions aren’t just a part of the Haskell experience, they pretty much are the Haskell experience.”

From Learn You a Haskell for Great Good

How to create a Scala ArrayBuffer

As a quick note, this is how you create a Scala ArrayBuffer:

import scala.collection.mutable.ArrayBuffer

var fruits = ArrayBuffer[String]()
var ints = ArrayBuffer[Int]()

The key there is that the keyword new is not required before the ArrayBuffer.

While I’m in the neighborhood, here are some other ways you can work with ArrayBuffer:

Scala for-loop examples and syntax

Table of Contents1 - Example data structures2 - Basic for-loop examples3 - Generators in for-loops4 - for-loop generators with guards5 - Scala for/yield examples (for-expressions)6 - Scala for loop counters (and zip, zipWithIndex)7 - Using a for loop with a Map8 - Multiple futures in a for loop9 - foreach examples10 - Summary

Besides having a bad memory, I haven’t been able to work with Scala much recently, so I’ve been putting together this list of for loop examples.

This page is currently a work in progress, and as of tonight I haven’t tested some of the examples, but ... if you’re looking for some Scala for loop examples — technically called a for comprehension or for expression — I hope these examples are helpful.