I want one character function
Permalink 1 user found helpful
I want use simple one character function in custom template like Codeigniter.
ex:
to
define:
Is it already exists?
ex:
echo htmlentities ( $string , ENT_QUOTES , APP_CHARSET );
to
h($string);
define:
function h($string,$echo == true){ $string = htmlentities ( $string , ENT_QUOTES , APP_CHAR_SET ); if($echo == true){ echo $string; } return $string; }
Is it already exists?
Does not exist in C5. The way to do this would be by adding it to one of the C5 helper classes (the "text" helper seems to be where this function would belong in C5 -- there's already an "entities" function there).
You can do this by creating a new file in your site's "helpers" directory called "text.php". In that file put this:
Then in the places you want to use this, you need to do this:
So... not as succinct as it could be, but better than before.
In response to 1stWebDesign's question -- normally I agree that a more descriptive name is better, but in this case it is such a widely-used function that it makes sense to keep it short (and has become somewhat of a standard -- I know it's used in CodeIgniter and Ruby on Rails, both of which are fairly popular to say the least).
You can do this by creating a new file in your site's "helpers" directory called "text.php". In that file put this:
class SiteTextHelper extends TextHelper { function h($text) { echo htmlentities($text, ENT_QUOTES, APP_CHARSET); } }
Then in the places you want to use this, you need to do this:
$th = Loader::helper('text'); //only needs to be loaded once per file $th->h('whatever');
So... not as succinct as it could be, but better than before.
In response to 1stWebDesign's question -- normally I agree that a more descriptive name is better, but in this case it is such a widely-used function that it makes sense to keep it short (and has become somewhat of a standard -- I know it's used in CodeIgniter and Ruby on Rails, both of which are fairly popular to say the least).
Isn't that function exactly what TextHelper::entities() already does?
print Loader::helper('text')->entities('My text');
print Loader::helper('text')->entities('My text');
Yes, and it's also the same as what htmlentities does. The OP's question was specifically about using a very short function name ("h").
Thanks.
I know text helper. but It's too long and troublesome.
I want to type more shorter.
concrete5 do not like it?
I know text helper. but It's too long and troublesome.
I want to type more shorter.
concrete5 do not like it?
No, there is nothing like that in concrete5.
No, not built-in. If you have global functions like this that you want to define, I'd probably just create a file named "config/site_post.php" and put them in there
Something like that. Putting it in config/site_post.php will ensure that it will be loaded after Loader and some other base classes.
<?php function h($text, $echo = false) { $text = Loader::helper('text')->entities($text); if ($echo) { print $text; } }
Something like that. Putting it in config/site_post.php will ensure that it will be loaded after Loader and some other base classes.
Wow, I learn something new every day (config/site_post.php) -- this is a very handy tip, thanks.
Thank you!
I wanted this way.
I wanted this way.
I'm not familiar with Codeignitor, but wouldn't it be better to have a more meaningful function name? I have created my own function called specialchars() for the same purpose as you.