What is the `HtmlString` used in Laravel?

This class: HtmlString

<? php

namespace Illuminate \ Support;

use Illuminate \ Contracts \ Support \ Htmlable;

class HtmlString implements Htmlable
{
    / **
     * The HTML string.
     *
     * @var string
     * /
    protected $ html;

    / **
     * Create a new HTML string instance.
     *
     * @param string $ html
     * @return void
     * /
    public function __construct ($ html)
    {
        $ this-> html = $ html;
    }

    / **
     * Get the HTML string.
     *
     * @return string
     * /
    public function toHtml ()
    {
        return $ this-> html;
    }

    / **
     * Get the HTML string.
     *
     * @return string
     * /
    public function __toString ()
    {
        return $ this-> toHtml ();
    }
}

using:

    function csrf_field ()
    {
        return new HtmlString('<input type="hidden" name="_token" value="'.csrf_token().'">');
    }

, "" !

- ? :)

+4
2

(Htmlable), , , HTML .

, , , Illuminate/Support/helpers.php:519:

if (! function_exists('e')) {
    /**
     * Escape HTML special characters in a string.
     *
     * @param  \Illuminate\Contracts\Support\Htmlable|string  $value
     * @return string
     */
    function e($value)
    {
        if ($value instanceof Htmlable) {
            return $value->toHtml();
        }

        return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
    }
}

, $value Htmlable, . .

+2

, .blade.php?

{{csrf_field()}}
0

Source: https://habr.com/ru/post/1670186/


All Articles