這個類:HtmlString在Laravel中使用了什麼`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(); } }
使用:
function csrf_field() { return new HtmlString('<input type="hidden" name="_token" value="'.csrf_token().'">'); }
它什麼都不做,而是 「建設」 的字符串,返回字符串本身!
任何人都可以解釋它嗎?非常感謝:)
當然,它就像你說的XD一樣使用,但是我很好奇它下面的HtmlString類,謝謝你的時間。 – zjuwujunchao
注意'csrf_field()'返回'HtmlString',所以在刀片中你可以使用'{{...}}'而不是'{! '!!}'。不需要記住打印原始數據並確保它不會被轉義 – morph