@materlelik

Как отобразить определенное колличество слов в Twig?

Друзья, есть функция показа определенного колличества слов

function first_n_words($text, $number_of_words) {
   // Where excerpts are concerned, HTML tends to behave
   // like the proverbial ogre in the china shop, so best to strip that
   $text = strip_tags($text);

   // \w[\w'-]* allows for any word character (a-zA-Z0-9_) and also contractions
   // and hyphenated words like 'range-finder' or "it's"
   // the /s flags means that . matches \n, so this can match multiple lines
   $text = preg_replace("/^\W*((\w[\W'-]*\b\W*){1,$number_of_words}).*/ms", '\\1', $text);

   // strip out newline characters from our excerpt
   return str_replace("\n", "", $text);
}

// excerpt plus link if shortened
function truncate_to_n_words($text, $number_of_words, $url, $readmore = 'Read More') {
   $text = strip_tags($text);
   $excerpt = first_n_words($text, $number_of_words);
   // we can't just look at the length or try == because we strip carriage returns
   if( str_word_count($text) !== str_word_count($excerpt) ) {
      $excerpt .= '... <br><a href="'.$url.'">'.$readmore.'</a>';
   }
   return $excerpt;
}


Как эту функцию прикрутить к Twig {{article.description}} ?
  • Вопрос задан
  • 2408 просмотров
Пригласить эксперта
Ответы на вопрос 2
t-alexashka
@t-alexashka
Сразу пишу legacy код
А я бы не советовал прикручивать какие-то ф-ии, гораздо проще мне кажется воспользоваться такой конструкцией:

{{ description|split(' ')[0:5]|join(' ') }}

в массиве 0:5 указываете сколько слов вывести, и все.

Удачной разработки.
Ответ написан
bboytiwst
@bboytiwst
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы