From bd44ec5e01a4b8ab47987f1eccfc7021db0a3823 Mon Sep 17 00:00:00 2001 From: Tobias Weichart Date: Tue, 5 Apr 2016 12:12:52 +0200 Subject: [PATCH] only truncate if limit is reached * added check for ellipsis to only be concatenated if string is bigger than the limit * added test case --- src/Handlebars/Helpers.php | 2 +- tests/Handlebars/HandlebarsTest.php | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Handlebars/Helpers.php b/src/Handlebars/Helpers.php index 369b0a4..d3bee14 100755 --- a/src/Handlebars/Helpers.php +++ b/src/Handlebars/Helpers.php @@ -561,7 +561,7 @@ public function helperTruncate($template, $context, $args, $source) $limit = $m[2]; $ellipsis = $m[3]; $value = substr($context->get($keyname), 0, $limit); - if ($ellipsis) { + if ($ellipsis && strlen($context->get($keyname)) > $limit) { $value .= $ellipsis; } return $value; diff --git a/tests/Handlebars/HandlebarsTest.php b/tests/Handlebars/HandlebarsTest.php index 3602f4f..495af36 100644 --- a/tests/Handlebars/HandlebarsTest.php +++ b/tests/Handlebars/HandlebarsTest.php @@ -190,6 +190,11 @@ public function internalHelpersdataProvider() "{{#truncate data 8 '...'}}", ["data" => "Hello World! How are you?"], 'Hello Wo...' + ], + [ + "{{#truncate data 8 '...'}}", + ["data" => "Hello"], + 'Hello' ], [ "{{#raw}}I'm raw {{data}}{{/raw}}",