category

Twig で変数と文字列を連結して変数にセットする

2018-06-30

BUNさんが以下で書かれてる Title の出し分けとかをためしていて。

Craft CMS のテンプレートについて理解しよう(エントリの詳細用テンプレート編) | BUN:Log
https://bunlog.d-s-b.jp/learn-about-craftcms-templates-part4

検索結果の検索文字列とかどう渡すんだろうな??と。

https://yyuuiikk.org/entry/350
で書き方が書いてあったので試してみたら問題なさそうだった。

{% set query = craft.request.getParam('q') %}
{% set entries = craft.entries.search(query).order('score') %}

{% set contentTitle = "「#{ query }」の検索結果" %}

こんなかんじでいけた。

set - Documentation - Twig - The flexible, fast, and secure PHP template engine
https://twig.symfony.com/doc/2.x/tags/set.html


set - Combine variables with text to make variable - Craft CMS Stack Exchange
https://craftcms.stackexchange.com/questions/11858/combine-variables-with-text-to-make-variable

を見る限りは ~ でも行けそうなので試した感じだと、以下の書き方でもいけた。

{% set contentTitle = "「" ~ query ~ "」の検索結果" %}

なるほどなるほど。
どっちがいいんだろうな。