【问题标题】:Toastr to use Laravel session variables as alert messagesToastr 使用 Laravel 会话变量作为警报消息
【发布时间】:2015-07-05 03:38:44
【问题描述】:

我想从 Bootstrap 警报切换到 Toastr 警报。我目前的工作设置是:

@if (Session::has('flash_notification.message'))

     <div class="alert alert-{{ Session::get('flash_notification.level') }}">
         <button type="button" class="close" data-dismiss="alert" 
             aria-hidden="true">&times;</button>
         {{ Session::get('flash_notification.message') }}
     </div>

 @endif

但我现在正在努力在 JS 中访问 Laravel 的会话变量。

实际的 JS 工作示例是:

$(document).ready(function() {

    toastr.info('Page Loaded!');

    });

});

但是,我想使用 Laravel 的会话变量来包含不同的消息和警告框:

@if (Session::has('flash_notification.message'))

    <script>
        $(document).ready(function() {

            toastr.options.timeOut = 4000;
            toastr.{{ Session::get('flash_notification.level') }}('{{ Session::get('flash_notification.message) }}');

        });
    </script>

@endif

我遇到了各种错误,例如unexpected ;。任何帮助将不胜感激。非常感谢。

【问题讨论】:

    标签: javascript twitter-bootstrap laravel laravel-5 toastr


    【解决方案1】:

    由于您在刀片模板中工作,您可以使用{{ var/function() }} 输出变量/函数的内容。如果你想要非转义输出,你可以使用{!! var/function() !!}

    所以你的问题的解决方案是,你需要用{{ }}标签包围你的php代码:

    @if (Session::has('flash_notification.message'))
    
        <script>
            $(document).ready(function() {
    
            toastr.{{ Session::get('flash_notification.level') }}
            ('{{ Session::get('flash_notification.message') }}');
    
            });
        </script>
    
    @endif
    

    【讨论】:

    • 感谢您的回复。不幸的是,它返回错误unexpected ;
    • 因为它在我的 template.blade.php 中,所以错误信息很差 - FatalErrorException in 97a837778d688883db99bfdf8c10c794 line 66: syntax error, unexpected ';'。第 66 行是:toastr.{{ Session::get('flash_notification.level') }}('{{ Session::get('flash_notification.message) }}');
    • Session::get('flash_notification.message) - 您在flash_notification.message 之后缺少第二个'
    • 非常真实!我自己也注意到了——谢谢。我现在就去看看。
    • 这一切看起来都是正确的,但现在错误是:Uncaught ReferenceError: $ is not defined。在页脚中调用了 jQuery,但是 document.ready 应该停止这个错误吗?
    猜你喜欢
    • 2012-11-05
    • 2017-06-10
    • 2019-10-19
    • 1970-01-01
    • 2021-02-08
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    相关资源
    最近更新 更多