拉拉维尔图像提交按钮



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

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

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

最好的办法是使用按钮。

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

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

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

最新更新