Skip to content

Instantly share code, notes, and snippets.

@pafnuty
Created April 24, 2016 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pafnuty/19f913ba0ff1d78e15fa98ccd7c339b8 to your computer and use it in GitHub Desktop.
Save pafnuty/19f913ba0ff1d78e15fa98ccd7c339b8 to your computer and use it in GitHub Desktop.
Пример демонстрирует работу модификатора ematch_all в модуле BlockPro
{*
Пример демонстрирует работу модификатора ematch_all в модуле BlockPro
показано как можно посчитать количество картинок в новости.
*}
{foreach $list as $key => $el}
{* Счётчик гифок *}
{set $totalGifs = 0}
{* Счётчик остальных картинок *}
{set $otherImages = 0}
{* Получаем картинки *}
{set $images = $el.short_story|ematch_all:'/<img(?:\\s[^<>]*?)?\\bsrc\\s*=\\s*(?|"([^"]*)"|\'([^\']*)\'|([^<>\'"\\s]*))[^<>]*>/i'}
{* Для вывода результатов работы модификатора можно использовать конструкцию: *}
{* <pre>{$images|dump}</pre> *}
{* Если нашлась хоть одна картинка — работаем *}
{if $images[1]|length}
{* Пробегаем по картинкам *}
{foreach $images[1] as $image}
{* Если спойлер или смайлик — пропускаем *}
{if ('dleimages' in $image) || ('engine/data/emoticons' in $image)}
{continue}
{/if}
{set $imgInfo = $image|pathinfo}
{if $imgInfo.extension == 'gif'}
{* Если гифка — добавим счётчик гифок *}
{set $totalGifs = $totalGifs + 1}
{else}
{* Если дугая картинка — добавим счётчик других картинок *}
{set $otherImages = $otherImages + 1}
{/if}
{/foreach}
{/if}
{* Выводим информацию *}
<p>
В новости <b><a href="{$el.url}">{$el.title}</a></b>
{$totalGifs} {$totalGifs|declination:'гиф|ка|ки|ок'},
а так же
{$otherImages} {$otherImages|declination:'|другая картинка|других картинки|других картинок'}
</p>
{/foreach}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment