Laravel图像提交按钮

时间:2013-09-06 01:10:06

标签: php twitter-bootstrap laravel laravel-3

我想知道是否有办法在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))); }}

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

2 个答案:

答案 0 :(得分:3)

您无法使用HTML类以这种方式生成链接,并且HTML)已从L4中删除,这是最佳做法,如果您使用它会更容易虽然在HTML中存在类似(bootstrapper,但我没有尝试过)的替代方法,但在(IMO)中却是压倒性的。L3标记。 请检查此forum link

或者您可以使用自定义宏,只需在app\libraries中创建一个新文件(myMacros.php),它应该是app\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>');

答案 1 :(得分:2)

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

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

<button type="submit"><i class="icon-trash"></i></button>