dc2bd6c630eff41bc09c970d7dd9434c2c6e7d59
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Format / CropViewHelper.php
1 <?php
2
3 /* *
4 * This script is part of the TYPO3 project - inspiring people to share! *
5 * *
6 * TYPO3 is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU General Public License version 2 as published by *
8 * the Free Software Foundation. *
9 * *
10 * This script is distributed in the hope that it will be useful, but *
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
12 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
13 * Public License for more details. *
14 * */
15
16 /**
17 * @package Fluid
18 * @subpackage ViewHelpers
19 * @version $Id$
20 */
21
22 /**
23 * Use this view helper to crop the text between its opening and closing tags.
24 *
25 * = Examples =
26 *
27 * <code title="Defaults">
28 * <f:format.crop maxCharacters="10">This is some very long text</f:format.crop>
29 * </code>
30 *
31 * Output:
32 * This is...
33 *
34 * <code title="Custom suffix">
35 * <f:format.crop maxCharacters="17" append="&nbsp;[more]">This is some very long text</f:format.crop>
36 * </code>
37 *
38 * Output:
39 * This is some&nbsp;[more]
40 *
41 * <code title="Don't respect word boundaries">
42 * <f:format.crop maxCharacters="10" respectWordBoundaries="false">This is some very long text</f:format.crop>
43 * </code>
44 *
45 * Output:
46 * This is so...
47 *
48 * WARNING: This tag does NOT handle tags currently.
49 * WARNING: This tag doesn't care about multibyte charsets currently.
50 *
51 * @package Fluid
52 * @subpackage ViewHelpers
53 * @version $Id$
54 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
55 * @scope prototype
56 */
57 class Tx_Fluid_ViewHelpers_Format_CropViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
58
59 /**
60 * @var tslib_cObj
61 */
62 protected $contentObject;
63
64 /**
65 * Constructor. Used to create an instance of tslib_cObj used by the render() method.
66 *
67 * @param tslib_cObj $contentObject injector for tslib_cObj (optional)
68 * @return void
69 */
70 public function __construct($contentObject = NULL) {
71 $this->contentObject = $contentObject !== NULL ? $contentObject : t3lib_div::makeInstance('tslib_cObj');
72 }
73
74 /**
75 * Render the cropped text
76 *
77 * @param integer $maxCharacters Place where to truncate the string
78 * @param string $append What to append, if truncation happened
79 * @param boolean $respectBoundaries If TRUE and division is in the middle of a word, the remains of that word is removed. This is currently ignored in backend mode!
80 * @return string cropped text
81 * @author Andreas Pattynama <andreas.pattynama@innocube.ch>
82 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
83 * @author Bastian Waidelich <bastian@typo3.org>
84 */
85 public function render($maxCharacters, $append = '...', $respectWordBoundaries = TRUE) {
86 $stringToTruncate = $this->renderChildren();
87 if (TYPO3_MODE === 'BE') {
88 if (strlen($stringToTruncate) > $maxCharacters) {
89 $stringToTruncate = substr($stringToTruncate, 0, ($maxCharacters - strlen($append))) . $append;
90 }
91 return $stringToTruncate;
92 } else {
93 return $this->contentObject->crop($stringToTruncate, $maxCharacters . '|' . $append . '|' . $respectWordBoundaries);
94 }
95 }
96 }
97
98
99 ?>