FORSMILE
JA
Smarty2020/06/18

[Smarty] Summary of Counting Arrays and Strings

For example, to get the number of elements in the array $loop, do the following:

Back to Blog

GETTING THE COUNT OF ELEMENTS IN AN ARRAY

For example, to get the number of elements in the array $loop, do the following:

php
{$loop|@count}
php
{ if count($loop) > 0 }
    $loopの要素数は1以上
{/if}

COUNTING STRINGS

javascript
{assign var="hoge" value="Hello World"}

{$hoge}
{$hoge|count_characters}
{$hoge|count_characters:true}  //White Space characterを含むかどうか

//Output data
Hello World
10
11

Besides strings, modifiers are also available for paragraphs, sentences, and words. (These might be difficult to use with Japanese text.)

text
count_paragraphs
count_sentences
count_words

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で探す →(アソシエイトリンク)