1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Twig. |
5
|
|
|
* |
6
|
|
|
* (c) 2009 Fabien Potencier |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @author Henrik Bjornskov <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Twig_Extensions_Extension_Text extends Twig_Extension |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Returns a list of filters. |
17
|
|
|
* |
18
|
|
|
* @return array |
|
|
|
|
19
|
|
|
*/ |
20
|
1 |
|
public function getFilters () |
21
|
|
|
{ |
22
|
|
|
return array( |
23
|
1 |
|
new Twig_SimpleFilter('summary', 'twig_summary_filter'), |
24
|
1 |
|
new Twig_SimpleFilter('truncate', 'twig_truncate_filter', array('needs_environment' => true)), |
25
|
1 |
|
new Twig_SimpleFilter('wordwrap', 'twig_wordwrap_filter', array('needs_environment' => true)), |
26
|
1 |
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Name of this extension. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
1 |
|
public function getName () |
35
|
|
|
{ |
36
|
1 |
|
return 'Text'; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function twig_summary_filter ($value, $paragraphCount = 1) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
if (function_exists('simplexml_load_string')) |
43
|
|
|
{ |
44
|
|
|
$content = simplexml_load_string('<html>' . $value . '</html>'); |
45
|
|
|
$count = min($paragraphCount, $content->count()); |
46
|
|
|
$children = $content->children(); |
47
|
|
|
|
48
|
|
|
$summary = ""; |
49
|
|
|
|
50
|
|
|
for ($i = 0; $i < $count; $i++) |
51
|
|
|
{ |
52
|
|
|
$summary .= $children[$i]->asXml(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $summary; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
function twig_truncate_filter (Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...') |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if (mb_strlen($value, $env->getCharset()) > $length) |
64
|
|
|
{ |
65
|
|
|
if ($preserve) |
66
|
|
|
{ |
67
|
|
|
// If breakpoint is on the last word, return the value without separator. |
68
|
|
|
if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) |
69
|
|
|
{ |
70
|
|
|
return $value; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$length = $breakpoint; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return rtrim(mb_substr($value, 0, $length, $env->getCharset())).$separator; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
function twig_wordwrap_filter (Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$sentences = array(); |
85
|
|
|
|
86
|
|
|
$previous = mb_regex_encoding(); |
87
|
|
|
mb_regex_encoding($env->getCharset()); |
88
|
|
|
|
89
|
|
|
$pieces = mb_split($separator, $value); |
90
|
|
|
mb_regex_encoding($previous); |
91
|
|
|
|
92
|
|
|
foreach ($pieces as $piece) |
93
|
|
|
{ |
94
|
|
|
while (!$preserve && mb_strlen($piece, $env->getCharset()) > $length) |
95
|
|
|
{ |
96
|
|
|
$sentences[] = mb_substr($piece, 0, $length, $env->getCharset()); |
97
|
|
|
$piece = mb_substr($piece, $length, 2048, $env->getCharset()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$sentences[] = $piece; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return implode($separator, $sentences); |
104
|
|
|
} |
105
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.