|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
cYbercOsmOnauT Smarty Rookie
Joined: 26 Aug 2012 Posts: 10 Location: Göttingen, Germany
|
Posted: Sun Aug 26, 2012 11:08 pm Post subject: Modifier strip_tags |
|
|
I created a version without using the crappy PHP function strip_tags or the used RegEx which kills everything between a < and a > for a customer of mine. Is there any place where I can provide this enchanced version to the Smarty community? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon Aug 27, 2012 4:53 pm Post subject: |
|
|
You post it just here. |
|
Back to top |
|
cYbercOsmOnauT Smarty Rookie
Joined: 26 Aug 2012 Posts: 10 Location: Göttingen, Germany
|
Posted: Mon Aug 27, 2012 5:30 pm Post subject: |
|
|
Ah okay.. my version is for Smarty 2.x. I will make it work with 3.x too and post. |
|
Back to top |
|
cYbercOsmOnauT Smarty Rookie
Joined: 26 Aug 2012 Posts: 10 Location: Göttingen, Germany
|
Posted: Wed Aug 29, 2012 10:54 pm Post subject: |
|
|
For Smarty 2.x
Code: | <?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty strip_tags modifier plugin
*
* Type: modifier<br>
* Name: strip_tags<br>
* Purpose: strip html tags from text
* @link http://smarty.php.net/manual/en/language.modifier.strip.tags.php
* strip_tags (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com>
* @author Jordon Mears <jordoncm at gmail dot com>
* @author Tekin Birdüzen <t.birduezen at web-coding dot eu>
*
* @version 3.0
*
* @param string
* @param boolean optional
* @param string optional
* @return string
*/
function smarty_modifier_strip_tags($string)
{
// HTML5 selfclosing tags
$selfclosingTags = array('area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr');
/**
* Find out how many arguments we have and
* initialize the needed variables
*/
switch (func_num_args()) {
case 1:
$replace_with_space = true;
break;
case 2:
$arg = func_get_arg(1);
if ($arg === 1 || $arg === true || $arg === '1' || $arg === 'true') {
// for full legacy support || $arg === 'false' should be included
$replace_with_space = ' ';
$allowed_tags = '';
} elseif ($arg === 0 || $arg === false || $arg === '0' || $arg === 'false') {
// for full legacy support || $arg === 'false' should be removed
$replace_with_space = '';
$allowed_tags = '';
} else {
$replace_with_space = ' ';
$allowed_tags = $arg;
}
break;
case 3:
$replace_with_space = func_get_arg(1) ? ' ' : '';
$allowed_tags = func_get_arg(2);
break;
}
if (strlen($allowed_tags)) {
// Allowed tags are set
$allowed_tags = str_replace(array(' />', '/>', '>'), '', $allowed_tags);
$allowed_tags = substr(str_replace('<', '|', $allowed_tags), 1);
// This is to delete the allowed selfclosing tags from the list
$tagArray = explode('|', $allowed_tags);
$selfclosingTags = array_diff($selfclosingTags, $tagArray);
unset ($tagArray);
}
// Let's get rid of the selfclosing tags first
if (count($selfclosingTags)) {
$string = preg_replace('/<(' . implode('|', $selfclosingTags) . ')\s?[^>]*?\/>/is', $replace_with_space, $string);
}
// And now the other tags
if (strlen($allowed_tags)) {
while (preg_match("/<(?!({$allowed_tags}))([a-z1-5]+)\s?[^>]*?>([^<]*?)<\/\\2>/is", $string))
$string = preg_replace("/<(?!({$allowed_tags}))([a-z1-5]+)\s?[^>]*?>([^<]*?)<\/\\2>/is", '$3' . $replace_with_space, $string);
}
else {
// Absolutely no tags allowed
while (preg_match("/<([a-z1-5]+)\s?[^>]*?>([^<]*?)<\/\\1>/is", $string))
$string = preg_replace("/<([a-z1-5]+)\s?[^>]*?>([^<]*?)<\/\\1>/is", '$2' . $replace_with_space, $string);
}
return $string;
}
/* vim: set expandtab: */
?> |
The 3.x version will follow soon.
Edit: Hmm.. did I understand it right? When you delete modifiercompiler.strip_tags.php and upload the above code as modifier.strip_tags.php then there is nothing to change for 3.x? |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Wed Aug 29, 2012 11:08 pm Post subject: |
|
|
Normally Smarty2 modifier do run also under Smarty3.
Yes, in your case you must remove the modifiercompiler.strip_tags.php file because of the search order for modifier. |
|
Back to top |
|
cYbercOsmOnauT Smarty Rookie
Joined: 26 Aug 2012 Posts: 10 Location: Göttingen, Germany
|
Posted: Wed Aug 29, 2012 11:43 pm Post subject: |
|
|
btw.. the usage is the same as the version of Jordon Mears.
- {$string|strip_tags} strips all tags and replaces them with a space
- {$string|strip_tags:false} strips all tags without replacing them with a space
- {$string|strip_tags:'<b><br>'} strips all tags except b and br tags and replaces them with a space
- {$string|strip_tags:false:'<b><br>'} strips all tags except b and br tags without replacing them with a space
It doesn't matter if you write the allowed tags like <br> or <br /> or even <br/>. All of those are recognized.
I use Smarty almost in every project I do and this shall be my thank for it. I hope my version will become official because using strip_tags PHP is really crappy. I learned to hate it |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Aug 30, 2012 12:16 am Post subject: |
|
|
Thanks for your input. I think we will include it in the upcomming major 3.2 release. |
|
Back to top |
|
cYbercOsmOnauT Smarty Rookie
Joined: 26 Aug 2012 Posts: 10 Location: Göttingen, Germany
|
Posted: Thu Aug 30, 2012 10:16 pm Post subject: |
|
|
Gern geschehen. Euch einen Dank für das wunderbare Templatesystem. |
|
Back to top |
|
cYbercOsmOnauT Smarty Rookie
Joined: 26 Aug 2012 Posts: 10 Location: Göttingen, Germany
|
Posted: Tue Apr 02, 2013 12:22 pm Post subject: |
|
|
There was a small problem with the selfclosing tags. The fixed version:
Code: | <?php
/**
* Smarty plugin
* @package Smarty
* @subpackage plugins
*/
/**
* Smarty strip_tags modifier plugin
*
* Type: modifier<br>
* Name: strip_tags<br>
* Purpose: strip html tags from text
* @link http://smarty.php.net/manual/en/language.modifier.strip.tags.php
* strip_tags (Smarty online manual)
* @author Monte Ohrt <monte at ohrt dot com>
* @author Jordon Mears <jordoncm at gmail dot com>
* @author Tekin Birdüzen <t.birduezen at web-coding dot eu>
*
* @version 3.0
*
* @param string
* @param boolean optional
* @param string optional
* @return string
*/
function smarty_modifier_strip_tags($string)
{
// HTML5 selfclosing tags
$selfclosingTags = array('area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr');
/**
* Find out how many arguments we have and
* initialize the needed variables
*/
switch (func_num_args()) {
case 1:
$replace_with_space = true;
break;
case 2:
$arg = func_get_arg(1);
if ($arg === 1 || $arg === true || $arg === '1' || $arg === 'true') {
// for full legacy support || $arg === 'false' should be included
$replace_with_space = ' ';
$allowed_tags = '';
} elseif ($arg === 0 || $arg === false || $arg === '0' || $arg === 'false') {
// for full legacy support || $arg === 'false' should be removed
$replace_with_space = '';
$allowed_tags = '';
} else {
$replace_with_space = ' ';
$allowed_tags = $arg;
}
break;
case 3:
$replace_with_space = func_get_arg(1) ? ' ' : '';
$allowed_tags = func_get_arg(2);
break;
}
if (strlen($allowed_tags)) {
// Allowed tags are set
$allowed_tags = str_replace(array(' />', '/>', '>'), '', $allowed_tags);
// This is to delete the allowed selfclosing tags from the list
$tagArray = explode('<', substr($allowed_tags, 1));
$selfClosing = array_intersect($tagArray, $selfclosingTags);
$selfclosingTags = array_diff($selfclosingTags, $tagArray);
$allowed_tags = implode('|', array_diff($tagArray, $selfClosing));
unset ($tagArray, $selfClosing);
}
// Let's get rid of the selfclosing tags first
if (count($selfclosingTags)) {
$string = preg_replace('/<(' . implode('|', $selfclosingTags) . ')\s?[^>]*?\/?>/is', $replace_with_space, $string);
}
// And now the other tags
if (strlen($allowed_tags)) {
while (preg_match("/<(?!({$allowed_tags}))([a-z][a-z1-5]+)\s?[^>]*?>(.*?)<\/\\2>/is", $string))
$string = preg_replace("/<(?!({$allowed_tags}))([a-z][a-z1-5]+)\s?[^>]*?>(.*?)<\/\\2>/is", '$3' . $replace_with_space, $string);
}
else {
// Absolutely no tags allowed
while (preg_match("/<([a-z][a-z1-5]+)\s?[^>]*?>(.*?)<\/\\1>/is", $string))
$string = preg_replace("/<([a-z][a-z1-5]+)\s?[^>]*?>(.*?)<\/\\1>/is", '$2' . $replace_with_space, $string);
}
return $string;
}
/* vim: set expandtab: */
?> |
|
|
Back to top |
|
ildar Smarty Rookie
Joined: 14 May 2015 Posts: 14
|
Posted: Thu May 14, 2015 10:24 am Post subject: overwrite the default plugins/modifiercompiler.default.php |
|
|
U.Tews wrote: | Normally Smarty2 modifier do run also under Smarty3.
Yes, in your case you must remove the modifiercompiler.strip_tags.php file because of the search order for modifier. |
I would like to overwrite the default plugins/modifiercompiler.default.php behavior
with my modifier.default.php implementation
Code: | return empty($value) ? $default : $value; |
As I understand the only one solution is to remove the default modifiercompiler.default.php file
Am I right?
Smarty-3.1.21 |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu May 14, 2015 10:56 pm Post subject: |
|
|
Yes, the search order for plugins does first look for compiler versions of the plugins.
So you must remove it, or you can replace it's code with
Code: | $output = '(empty(' . $output . ') ? ' . $param . ' : '. $output . ')'; |
|
|
Back to top |
|
ildar Smarty Rookie
Joined: 14 May 2015 Posts: 14
|
Posted: Fri May 15, 2015 5:35 am Post subject: |
|
|
U.Tews wrote: | Yes, the search order for plugins does first look for compiler versions of the plugins.
So you must remove it, or you can replace it's code with
Code: | $output = '(empty(' . $output . ') ? ' . $param . ' : '. $output . ')'; |
|
Thank you for your quick response. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Mon May 18, 2015 6:28 am Post subject: |
|
|
Just a note
Your default modifier will not work on tags like
Code: | {tag|default:"DOES NOT WORK"}
|
|
|
Back to top |
|
ildar Smarty Rookie
Joined: 14 May 2015 Posts: 14
|
Posted: Mon May 18, 2015 6:32 am Post subject: |
|
|
U.Tews wrote: | Just a note
Your default modifier will not work on tags like
Code: | {tag|default:"DOES NOT WORK"}
|
|
I have changed the default
include/lib/smarty3/plugins/modifiercompiler.default.php
Code: | diff --git a/include/lib/smarty3/plugins/modifiercompiler.default.php b/include/lib/smarty3/plugins/modifiercompiler.default.php
index fe77762..c07641f 100644
--- a/include/lib/smarty3/plugins/modifiercompiler.default.php
+++ b/include/lib/smarty3/plugins/modifiercompiler.default.php
@@ -28,7 +28,7 @@ function smarty_modifiercompiler_default($params)
array_shift($params);
foreach ($params as $param) {
- $output = '(($tmp = @' . $output . ')===null||$tmp===\'\' ? ' . $param . ' : $tmp)';
+ $output = '(($tmp = @' . $output . ')===null||empty($tmp) ? ' . $param . ' : $tmp)';
}
return $output;
|
|
|
Back to top |
|
ildar Smarty Rookie
Joined: 14 May 2015 Posts: 14
|
Posted: Tue Sep 22, 2015 9:41 am Post subject: |
|
|
U.Tews wrote: | Yes, the search order for plugins does first look for compiler versions of the plugins.
So you must remove it, or you can replace it's code with
Code: | $output = '(empty(' . $output . ') ? ' . $param . ' : '. $output . ')'; |
|
As I can see I cannot overwrite the system modifier
libs/plugins/modifier.escape.php with my own one
custom_dir/plugins/modifier.escape.php (loaded via addPluginsDir())
for version
const SMARTY_VERSION = '3.1.28-dev/63';
Please, confirm.
Thank you. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|