Field method html - misunderstanding

Hi, I’m a little bit confused about the html field method. If I output a title field with $field->html() and I want to use some html entities like   or ­ it renders them as text instead of html. Is that the intended behavior?
In my understanding it should render it as html and not text? Html tags are rendered correct with the html method.
But if I output the field just as $field without any method it seems to work, for entities aswell as tags.

What would be the correct way to use entities in text fields?

You can omit the html() method if you want to use HTML entities.

This is what the HTML method does:

  /**
   * Converts a string to a html-safe string
   *
   * @param  string  $string
   * @param  boolean $keepTags True: lets stuff inside html tags untouched.
   * @return string  The html string
   */
  public static function encode($string, $keepTags = true) {
    if($keepTags) {
      return stripslashes(implode('', preg_replace_callback('/^([^<].+[^>])$/', function($match) {
        return htmlentities($match[1], ENT_COMPAT, 'utf-8');
      }, preg_split('/(<.+?>)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE))));
    } else {
      return htmlentities($string, ENT_COMPAT, 'utf-8');
    }
  }

As you can see, it internally uses the PHP htmlentities()function.

If you don’t like this behavior, you could create your own field method and set the 4th parameter of the htmlentities() method (i.e. double_encode) to false.

Suppose you have a string like this:

$string = 'Some text with an HTML entity &nbsp;!';
<?= htmlentities($string, ENT_COMPAT, 'utf-8') ?>

will output

Some text with an HTML entity $nbsp;!

Whereas

<?= htmlentities($string, ENT_COMPAT, 'utf-8', false) ?>

will output

Some text with an HTML entity !