FORSMILE
JA
Smarty2016/02/11

[Smarty] Displaying Variables and Arrays

For your information, while the PHP side configures what values are passed to the template, this article focuses solely on how Smarty is handled within the template from a front-end perspective.

Back to Blog

For your information, while the PHP side configures what values are passed to the template, this article focuses solely on how Smarty is handled within the template from a front-end perspective.

DISPLAYING VARIABLES PASSED TO THE TEMPLATE

If you have a variable named `arena_rank`, you just need to prefix it with `$` and enclose it in curly braces `{}` as shown below. This will display the value in the template:

php
ランキング {$arena_rank} 位

DISPLAYING ARRAYS PASSED TO THE TEMPLATE

First, inspect the data using what's equivalent to PHP's `var_dump`.

For more details, please refer to [Smarty] Investigating Template Variables and Arrays.

javascript
<div style="text-align:left;">{$rank_list|@debug_print_var}</div>

If we check the contents using `debug_print_var`, which we introduced recently, let's assume the `$rank_list` variable contained the following:

text
0
 "山田"
1
 "田中"
2
 "中山"

In the example above, if you want to display, for instance, the very first item "Yamada", you would do it as follows:

php
{$rank_list[0]}
{$rank_list.0}
text
name
 "山田"
mail
 "aaa@aaa.com"
tel
 "000-0000-0000"
php
{$user.mail}
php
{$rank_list.$arena_rank}

The displayed name will change depending on the value of `arena_rank`.

Recommended Smarty Books

While Smarty only has older books, and only Kindle editions were available, the following is recommended for beginners.

📦
Amazon で関連書籍・ツールを検索
PHP template engine development
Amazonで探す →(アソシエイトリンク)