Laravel字符串中使用

Laravel字符串中使用

本文介绍了在PHP Laravel字符串中使用@(符号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中包含以下代码.问题是@(符号处).当它在那里时,我得到语法高亮显示和错误,好像字符串没有结束.删除它后,该页面可以正常工作(减去不存在的操作).我试过转义@,这是行不通的.我也尝试过使用双引号,但这也不起作用.我怎样才能逃脱@?

I have the below code in my app. The problem is the @ (at sign). When it's there, I get syntax highlighting and errors as if the string doesn't end. When remove it, the page works fine (minus the action not existing). I've tried escaping the @ and that doesn't work. I've also tried double quotes, but that doesn't work either. How can I escape @?

我可以使用route函数并完全避免使用@,但是我觉得该动作函数的作用要清楚得多,所以我宁愿不使用route.

I could use the route function and avoid the @ entirely but I feel that the action function is far more clear in terms of what it's doing so I'd rather not use route.

@extends('layouts.default') <?php
$url = URL::action('UsersController@index'); ?>

@section('header')
    @include('partials.components.searchHeader', array('title' => "Users", 'results' => $users->getTotal(), 'total' => $total, 'url' => URL::route( 'users.index' ), 'type' => 'user'))
@stop

<?php
if(Auth::user()->isAdmin()) $publishedFellows = Fellow::published()->get();
if(!Auth::user()->isFellow()) $publishedOpportunities = Opportunity::select('opportunities.*')->published()->sortedByCompany()->get(); ?>

@section('content')
    @if(Auth::user()->isAdmin())
        @include('partials.components.add-button', array('url' => '/users/create', 'name' => 'Add User'))
    @endif
    <?php $partialsList = [
        'listItems' => $users,
        'search' => $search,
        'url' => URL::route('users.index'),
        'pills' => $pills,
        'indexView' => 'users.single',
        'type' => 'user',
        'total' => $total,
    ]; ?>
    @include('partials.list')
@stop

推荐答案

要在刀片中转义@符号-只需使用双@@.

To escape @ symbols in blade - you just use a double @@.

所以这个:

@@example

将打印

@example

但是,您的代码非常混乱,这很可能导致您遇到问题.最大的问题是您正在使用extend-但是随后您将代码放在各节之间,这没有正确调用.此外,您甚至没有在任何地方调用您在各节之间放置的用于保存变量的代码!

However your code is very messy, and that is probably causing you problems. The biggest issue is you are using extend - but then you are putting code inbetween the sections, which is not called correctly. Further - the code you are putting inbetween the sections to save variables is not even been called anywhere!

您应该将视图重构为类似这样的格式以解决此问题:

You should refactor your view to something like this to fix the issue:

@extends('layouts.default')

@section('header')
    @include('partials.components.searchHeader', ['title' => "Users", 'results' => $users->getTotal(), 'total' => $total, 'url' => URL::route( 'users.index' ), 'type' => 'user'])
@stop


@section('content')
    @if(Auth::user()->isAdmin())
        @include('partials.components.add-button', ['url' => '/users/create', 'name' => 'Add User'])
    @endif
    @include('partials.list', ['listItems' => $users, 'search' => $search, 'url' => URL::route('users.index'), 'pills' => $pills, 'indexView' => 'users.single', 'type' => 'user', 'total' => $total])
@stop

这篇关于在PHP Laravel字符串中使用@(符号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:32