How To Concatenate & Interpolate Strings In Twig

Twig is arguably the most powerful, modern and easy to use templating engine in PHP.

When using twig, sooner or later, you will run into the situation where you need to join two strings (concatenation) or evaluate an expression in strings (interpolation). Please consider the following

Concatenate Strings In Twig

To join two strings in Twig, you can use the tilde ~ operator as follows

{{ "Welcome Mr " ~ name }}

This will give the output Welcome Mr JohnDoe if the value of the name variable is JohnDoe.

You can play with this exact example on the twig playground.

Interpolating Strings In Twig

Sometimes you will desire to evaluate an expression and embed the result in a string, this is called interpolation, consider the example below where we will like to display the total sales of two months and display it.

{{ "Jan & Feb Sales = #{jan_sales + feb_sales}"}}

This will give the output “Jan & Feb Sales = 51” if the values of jan_sales and feb_sales are 28 & 23 respectively.

You can play with this exact example on the twig playground.

Quick snippet – Selecting only certain columns for a model in laravel

With a model named (Person) that maps to a table named (persons) with a structure defined below

CREATE TABLE `persons` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `first_name` VARCHAR(255) NOT NULL,
  `last_name` VARCHAR(255)  NOT NULL,
  `email` VARCHAR(255) NOT NULL,
  `created_at` TIMESTAMP NULL DEFAULT NULL,
  `updated_at` TIMESTAMP NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `persons` (`email`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

Then, Consider the eloquent statement given below

$result = Person::find(1);

this is equivalent to,

SELECT * FROM Persons

The above can be wasteful if all you require is just a few columns such as the firstname and lastname, to fix this, we use the eloquent statement below

$result = Person::find(1, ['first_name', 'last_name']);

This is equivalent to,

SELECT first_name, last_name FROM persons

Hope, you have been able to learn something new today.

Quick primer- How to evaluate strings to boolean in PHP

Occasionally in PHP, you need to evaluate a string value to its boolean counterpart. One very common example of when this is needed is when you try to represent boolean in query string parameters. The jury is still out on the most appropriate way to represent a boolean value in Rest API or a query string, some of the

  1. true
  2. True
  3. TRUE
  4. false
  5. FALSE
  6. Yes
  7. No
  8. On
  9. Off
  10. 0
  11. 1

all of the values listed above are valid ways to represent a boolean value in your API or query string hence the need find a valid way to change the values correctly to their boolean value.

Evaluation using filter_var()

this filter_var method has been present in PHP since php5 and as the name implies, it filters a variable with a specified filter, for the purpose of this writeup, we are interested only in the boolean filter which is named FILTER_VALIDATE_BOOLEAN.

check the example below

filter_var("1", FILTER_VALIDATE_BOOLEAN)   // => bool(true)
filter_var("on", FILTER_VALIDATE_BOOLEAN)  // => bool(true)
filter_var("false", FILTER_VALIDATE_BOOLEAN) // => bool(false)

Value (int) BoolVal Boolean
1 1 TRUE
 0 0 FALSE
true 1 TRUE
false 0 FALSE
1 1 TRUE
0 0 FALSE
1 1 TRUE
0 0 FALSE
on 1 TRUE
off 0 FALSE
yes 1 TRUE
no 0 FALSE
True 1 TRUE
FalSe 0 FALSE