[英]laravel 4: How insert raw HTML to label?
有一些簡單的方法可以將原始HTML標簽標記嗎? 我有這個:
{{ Form::label('firstName', 'First name <em>*</em>', array('class' => 'input_tag')) }}
它產生:
<label class="input_tag" for="firstName">First Name <em>*</em></label>
但是未正確解釋BUT標簽EM。 我想要的是:
<label class="input_tag" for="firstName">First Name <em>*</em></label>
使用HTML :: decode():
http://forums.laravel.io/viewtopic.php?id=1772
{{ HTML::decode(Form::label('firstName', 'First name <em>*</em>', array('class' => 'input_tag'))) }}
在macro
使用sprintf
比重新編碼要快得多:
Form::macro('rawLabel', function($name, $value = null, $options = array())
{
$label = Form::label($name, '%s', $options);
return sprintf($label, $value);
});
您可以創建一個輔助函數(宏),如下所示:
HTML::macro('raw', function($htmlbuilder) {
return htmlspecialchars_decode($htmlbuilder);
});
在您看來:
{{ HTML::raw(Form::label('input_firstname', 'Prénom <sup>*</sup>')) }}
我經常將原始html用於表單輸入和標簽,因為我發現它更易於閱讀和使用html5屬性,例如required和placeholder。 此表單是如何將原始html與Input :: old一起使用的一個很好的示例,它使您可以捕獲舊輸入。 https://github.com/Zizaco/confide/blob/master/src/views/login.blade.php
對於必需的輸入,我沒有嘗試將HTML添加到標簽中,而是添加了一個類“ required-input”(或其他內容)。
然后我有以下CSS規則
label.required-input:before {
content: ' * ';
color: #f00;
}
除非您需要用於屏幕閱讀器的<em> * </ em>,否則這將起作用。 但是您仍然可以在輸入中添加必需的標志。
知道了,它使用的是e()幫助函數,該函數使用htmlentities()格式化標簽,並將標簽轉換為&lt;em&gt;*&lt;/em&gt;
。
快速且(非常)骯臟的修復程序是在首次調用Helpers.php之前將其添加到start.php或其他位置:
function e($value) { return $value; }
但這遠非理想。
我相信Form :: label($ name,$ value,$ attributes,$ escape_html)帶有第四個參數,告訴它是否不轉義html。 默認為true。 因此,如果您的預期結果是斜體*,則將false作為第四個參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.