This article introduces how to combine variables and strings within a Twig template.
Although I've actually introduced this topic in the section on operators available in Twig, I'm writing it as a separate entry so I can easily recall it with a more descriptive title when I actually use it myself.
CONCATENATING VARIABLES AND STRINGS USING "~"
css
{% set name = 'tester' %}
{{ "Hello " ~ name ~ "!" }}
// result
Hello tester!COMBINING VARIABLES AND STRINGS USING "#{expression}"
css
{{ "foo #{1 + 2} baz" }}
// result
foo 3 bazIt's used for calculations, such as expressions, but you can, of course, also use variables.
css
{% set name = 'tester' %}
{{ "foo #{name} baz" }}
// result
foo tester bazIt's like this. I feel that combining variables and strings is surprisingly common.
RECOMMENDED BOOKS FOR TWIG
I haven't read any books exclusively dedicated to Twig, but I found some on Amazon Kindle.
📦Amazon で関連書籍・ツールを検索
PHP Twig template engine
Amazonで探す →(アソシエイトリンク)
