2016-10-12 06:25
浏览 80


I want to use Handlebars data (i.e. {{ id }}) as a parameter in Laravel helper function on .blade template view.

I know about escaping Handlebars brackets when using .blade.php extension like @{{#each users}} or @{{ name }} and that works perfectly.

The problem comes when I want to use @{{ id}} within Laravel's helper function because of nested double curly brackets:

<a href="#" class="btn btn-xs btn-warning" data-route="{{ route('admin.membership.toggle-show-hide', ['id' => @{{ id }}]) }}">

I know this can't work because helper function route doesn't expect curly bracket in parameters array but I still need to be able to pass data from Handlebars contex into Laravel's helper function on blade template view somehow.

I tried different ways to achieve this, but without success.

Does anyone knows some workaround or has any idea for this? Thank you all in advance.


The alternative (which I currently use) is to write a Handlebars helper which will accept needed parameter and generate route for some action:

Handlebars.registerHelper("mmbrshpToggleShowHideRoute", function(id) {
    return APP_URL + '/admin/membership/' + id + '/toggleShowHide';

Using this, I can generate route string in Handlebars template (with .blade.php extension) for each item in Handlebars JSON context retrieved from Laravel's controller:

<a href="#" class="btn btn-xs btn-success" data-route="{{ mmbrshpToggleShowHideRoute id }}">

I don't like this approach because I'm actually hardcoding the route URL in Handlebars helper function. This works and provides reusabillity throughout the views and templates, but it would be nicer if there was possibility to generate route using Laravel's route helper function and just pass an id parameter from Handlebars context.

图片转代码服务由CSDN问答提供 功能建议

我想使用Handlebars数据(即 {{id}} )作为参数 在.blade模板视图中的Laravel帮助函数。

我知道在使用.blade.php扩展名时转义句柄括号,例如 @ {{#each users}} @ {{name}} ,并且完美无缺。

当我想在Laravel的帮助函数中使用 @ {{id}} 时会出现问题,因为嵌套的双卷曲 括号:

 &lt; a href =“#”class =“btn btn-xs btn-warning”data-route =“{{route('admin.membership.toggle  -show-hide',['id'=&gt; @ {{id}}])}}“&gt; 
&lt; / a&gt; 
 <  p>我知道这不起作用,因为辅助函数 route 不期望参数数组中的花括号,但我仍然需要能够将数据从Handlebars上下文传递到刀片模板视图上的Laravel帮助函数 不知怎的。 


有没有人知道某些解决方法或对此有任何想法? 谢谢 你们都提前了。


替代( 我目前使用的)是写一个Handlebars帮助器,它将接受所需的参数并为某些动作生成路径:

返回APP_URL +'/ admin / membership /'+ id +'/ toggleShowHide'; 

使用此功能,我可以生成路由字符串 在Handlebars模板(扩展名为.blade.php)中,从Laravel控制器中检索到的Handlebars JSON上下文中的每个项目:

 &lt; a href =“#”class =“btn btn  -xs btn-success“data-route =”{{mmbrshpToggleShowHideRoute id}}“&gt; 
&lt; / a&gt; 

我不知道 喜欢这种方法,因为我实际上是在Handlebars帮助函数中硬编码路由URL。 这样可以在整个视图和模板中提供可重用性,但如果有可能使用Laravel的 route 辅助函数生成路径并且只是从Handlebars传递 id 参数,那就更好了。 上下文。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douduan5086 2016-10-12 09:46

    If you really need "bulletproof" solution for changing routes on frontend than most convenient solution is to make an accessor for route in your model. Something like this:

    public function getRouteAttribute()
        return route('admin.membership.toggle-show-hide', ['id' => $this->id]);

    Then you need to make sure that accessor is always available (even if you return JSON response) in your object instances using $appends property like this:

    protected $appends = ['route'];

    That should be it.

    打赏 评论
  • dou426098 2016-10-12 07:16

    In fact you can't use both JS and PHP variables/functions at the same time as JS variables may change over time during the PHP session.

    When JS is involved I don't use routes, I use plain string routes like data-route="'/admin/membership/toggle/' + id"

    打赏 评论
  • dpztth71739 2016-10-12 07:30

    You can use this sample code.

    <a href=".URL::route('admin.membership.toggle-show-hide',[$id])." class="btn-blue">Hide</a>
    打赏 评论

相关推荐 更多相似问题