laravel 4:如何将原始 HTML 插入标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16613110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:47:43  来源:igfitidea点击:

laravel 4: How insert raw HTML to label?

htmllabellaravel

提问by Lajdák Marek

Is there some easy way how put raw HTML tag to label? I have this:

是否有一些简单的方法可以将原始 HTML 标签放置到标签上?我有这个:

{{ Form::label('firstName', 'First name <em>*</em>', array('class' => 'input_tag')) }}

and it produces:

它产生:

<label class="input_tag" for="firstName">First Name &lt;em&gt;*&lt;/em&gt;</label>

BUT tag EM is not interpreted as it should be. What I want is:

但是标签 EM 没有按应有的方式解释。我想要的是:

<label class="input_tag" for="firstName">First Name <em>*</em></label>

回答by Aureliano Far Suau

use HTML::decode():

使用 HTML::decode():

http://forums.laravel.io/viewtopic.php?id=1772

http://forums.laravel.io/viewtopic.php?id=1772

{{ HTML::decode(Form::label('firstName', 'First name <em>*</em>', array('class' => 'input_tag'))) }}

回答by Adam Rivers

Using sprintfin a macrois much faster than redecoding:

sprintf在 a 中使用macro比重新解码要快得多:

Form::macro('rawLabel', function($name, $value = null, $options = array())
{
    $label = Form::label($name, '%s', $options);

    return sprintf($label, $value);
});

回答by Youssef Lahssini

You can create a helper function (macro) just like:

您可以创建一个辅助函数(宏),就像:

HTML::macro('raw', function($htmlbuilder) {
   return htmlspecialchars_decode($htmlbuilder);
});

in your view:

在您看来:

{{ HTML::raw(Form::label('input_firstname', 'Prénom <sup>*</sup>')) }}

回答by erj1

For required inputs, instead of trying to add HTML into the label, I add a class, 'required-input' (or something).

对于必需的输入,我没有尝试将 HTML 添加到标签中,而是添加了一个类“必需输入”(或其他东西)。

Then I have the following CSS rule

然后我有以下 CSS 规则

label.required-input:before {
    content: ' * ';
    color: #f00;
}

This would work unless you are needing to have the <em>*</em> for screen readers. But you can still add the required flag on the input.

除非您需要 <em>*</em> 用于屏幕阅读器,否则这会起作用。但是您仍然可以在输入上添加所需的标志。

回答by Thomas Clarkson

I often use raw html for form input and labels as I find it easier to read and use html5 attributes such as required and placeholder. This form is a good example of how to use raw html with Input::old which allows you to capture old input. https://github.com/Zizaco/confide/blob/master/src/views/login.blade.php

我经常将原始 html 用于表单输入和标签,因为我发现它更易于阅读和使用 html5 属性,例如 required 和 placeholder。这个表单是一个很好的例子,说明如何将原始 html 与 Input::old 一起使用,它允许您捕获旧输入。https://github.com/Zizaco/confide/blob/master/src/views/login.blade.php

回答by Antonio Carlos Ribeiro

Got it, it's using the e() helper function that uses htmlentities() to format your labels and this is converting your tag to &amp;lt;em&amp;gt;*&amp;lt;/em&amp;gt;.

明白了,它使用 e() 辅助函数,该函数使用 htmlentities() 来格式化您的标签,这会将您的标签转换为&amp;lt;em&amp;gt;*&amp;lt;/em&amp;gt;.

The quick and (very) dirty fix is to add this to your start.php or some other place before the first call to Helpers.php:

快速且(非常)肮脏的修复是在第一次调用 Helpers.php 之前将其添加到 start.php 或其他地方:

function e($value) { return $value; }

But this far from ideal.

但这远非理想。

回答by ipixelsmediaworks

I believe the Form::label($name, $value, $attributes, $escape_html) takes a fourth parameter which tells it whether to not to escape html. It defaults to true. So if your expected result is an italic *, pass false as the fourth parameter.

我相信 Form::label($name, $value, $attributes, $escape_html) 接受第四个参数,告诉它是否不转义 html。它默认为真。因此,如果您的预期结果是斜体 *,请将 false 作为第四个参数传递。