I want one character function

Permalink 1 user found helpful
I want use simple one character function in custom template like Codeigniter.

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?

Tao
 
1stWebDesigns replied on at Permalink Reply
1stWebDesigns
Not sure what the question is? The function "h" is already defined?

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.
jordanlev replied on at Permalink Reply
jordanlev
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:
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).
andrew replied on at Permalink Reply
andrew
Isn't that function exactly what TextHelper::entities() already does?

print Loader::helper('text')->entities('My text');
jordanlev replied on at Permalink Reply
jordanlev
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").
Tao replied on at Permalink Reply
Tao
Thanks.

I know text helper. but It's too long and troublesome.

I want to type more shorter.

concrete5 do not like it?
jordanlev replied on at Permalink Reply
jordanlev
No, there is nothing like that in concrete5.
andrew replied on at Permalink Best Answer Reply
andrew
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

<?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.
jordanlev replied on at Permalink Reply
jordanlev
Wow, I learn something new every day (config/site_post.php) -- this is a very handy tip, thanks.
Tao replied on at Permalink Reply
Tao
Thank you!
I wanted this way.