Automatic Text Excerpt / Truncated Texts with Twig

Today I had to create a grid of cards for a search page. To ensure a consistent layout I wanted the excerpt texts to be cut off, if necessary, at certain length.

This can be done easily when using the truncate filter in the Twig template engine for PHP:

toLongText|truncate(14)

The number 14 stands for the number of words after which the text will be cut off and an ellipsis is added.

HTML5 Video Autoplay in Mobile Safari

For autoplay to work on Mobile Safari with a native HTML video element you need to also set the “playsinline” attribute. Otherwise the video wouldn’t start to play automatically:

<video muted loop autoplay playsinline preload="auto">
    <source src="video.mp4" type="video/mp4">
    Your browser does not support playing this video.
</video>

The playsinline attribute is actually a standard HTML attribute, I learned today. I did not encounter any problems with autoplay in other browsers besides within Mobile Safari though.