modifier.wrap.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2018 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 
12 /**
13  * Wrap a value inside a html tag, if the string is not empty
14  *
15  * Example:
16  * @code
17  * {$text|wrap:'<h1 class="display-1">'}
18  * @endcode
19  *
20  * @param $string The string to wrap
21  * @param $tag The opening tag
22  * @return String
23  */
24 function smarty_modifier_wrap($string, $tag) {
25  $matches = [];
26  if (preg_match('/<([a-zA-Z0-9]+)(\s.*?>|>)/', trim($tag), $matches)) {
27  $element = $matches[1];
28  return strlen($string) > 0 ? $tag.$string.'</'.$element.'>' : "";
29  }
30  return $string;
31 }
32 ?>