4

我想知道是否有办法在Laravel 3中自定义提交按钮的外观(改为图像) 。

目前,我的提交按钮代码如下所示:

{{ Form::open('project/delete', 'DELETE') }}
{{ Form::hidden('id', $project->id) }}
{{ Form::submit('Delete project', array('class'=>'btn')); }}
{{ Form::close() }}

它正在正确地完成他的工作。但是我看不到如何自定义提交按钮并将其作为引导图标,例如使用 ;<i class="icon-trash"></i>

我尝试使用:

{{ HTML::decode(HTML::link_to_route('project_delete', '<i class="icon-trash"></i>', array($project->id))); }}

但是后来我的路由/函数调用有问题。

4

2 回答 2

3

您不能将 HTML 用于input. 如果您尝试过<input type="submit" value='<i class="icon-trash"></i>'>,您会发现它不起作用。此外,使用像第二种方法这样的链接将不起作用,因为它实际上并没有提交表单。

您最好的选择是使用按钮。

<button type="submit"><i class="icon-trash"></i></button>
于 2013-09-06T01:26:32.183 回答
3

您不能使用HTML类以这种方式生成链接,并且已将其 ( HTML)L4作为最佳实践删除,如果您HTML为此使用原始标记会更容易,尽管还有其他方法,例如 ( bootstrapper,我没有尝试过),L3但在(IMO)中它是压倒性的。检查这个论坛链接

或者,您可以使用自定义宏,只需在 中创建一个新文件(myMacros.php)app\libraries,它应该是 asapp\libraries\myMacros.php并将以下代码放入此文件中

HTML::macro('link_nested', function($route, $title = null, $attributes = array(), $secure = null, $nested = null, $params = array()) 
{
    $url = URL::to_route($route, $params, $secure);
    $title = $title ?: $url;
    if (empty($attributes)) {
        $attributes = null;
    }
    return '<a href="'.$url.'"'.HTML::attributes($attributes).'>'.$nested.''.HTML::entities($title).'</a>';
});

然后,将其包含在您的start.php喜欢中

require path('app').'/libraries/myMacros.php';

最后,在你的模板中使用它

HTML::link_nested('user.accountview', 'Delete', array('class'=>'btn'), '', '<i class="icon-trash"></i>', array($project->id));

对于一个submit按钮,将其添加到您的myMacros.php

HTML::macro('submit_nested', function($title = null, $attributes = array(), $nested = null) 
{
    $title = $title ?: 'Submit';
    if (empty($attributes)) {
        $attributes = null;
    }
    return '<button type="submit" ' . HTML::attributes($attributes).'>' . $nested  .' '. HTML::entities($title).'</button>';
});

最后,像这样使用它

HTML::submit_nested('Search', array('class'=>'someClass', 'name' => 'submit'), '<i class="icon-trash"></i>');
于 2013-09-06T02:29:21.597 回答