Slim Twig 没有渲染出 id

Slim Twig not rendering out id's

我有

{% if auth.renderFriendShip("{{auth.id}}", "{{user.id}}", 'isThereFriendship') == 0 %}

在我的视图文件中。它应该改变 {{auth.id}} 和 {{user.id}},但事实并非如此。当我 运行 在我的 php 模型(函数指向的地方)的 {{user.id}} 上 "var_dump" 时...它只是输出 {{user.id}}.

这是我的视图文件中的相关代码...

{% if auth.renderFriendShip("{{auth.id}}", "{{user.id}}", 'isThereFriendship') == 0 %}
    <button class="friendBtn add" data-uid="{{user.id}}" data-type="addFriend">Friend</button>
    <button class="request_pending hidden" disabled>Request Pending</button>
{% else %}
    <button class="friendBtn unfriend" data-uid="{{user.id}}" data-type="unfriend">Unfriend</button>
    <button class="friendBtn add hidden" data-uid="{{user.id}}" data-type="addFriend">Add as friend</button>                        
    <button class="request_pending hidden" disabled>Request Pending</button>
{% endif %}

和我的用户模型...

public function renderFriendShip($user_one, $user_two, $type) {
    if(!empty($user_one) || !empty($user_two) || !empty($type)) {
        switch($type) {
            case 'isThereRequestPending':
                $matchThese = ['user_one' => "$user_one", 'user_two' => "$user_two", 'friendship_official' => '0'];
                $orMatchThese = ['user_one' => "$user_two", 'user_two' => "$user_one", 'friendship_official' => '0'];

                $query = $this->friendsList()
                    ->where($matchThese)
                    ->orWhere($orMatchThese)
                    ->get()
                    ->count();

                return $query;
                break;
            case 'isThereFriendship':
                $q = $this->friendsList()
                    ->where(function($query) use ($user_one, $user_two) {
                        return $query->where(['user_one' => $user_one, 'user_two' => $user_two, 'friendship_official' => '1']);
                    })
                    ->orWhere(function($query) use ($user_one, $user_two) {
                        return $query->where(['user_one' => $user_two, 'user_two' => $user_one, 'friendship_official' => '1']);
                    })
                    ->get()
                    ->count();

                break;
        }
        return var_dump($user_two);
    } else {
        return "Failed";
    }
}

感谢您给我任何可能的建议。我已经搜索了几个小时,但还没有找到解决方案。

这里的问题很简单:在您的 twig 文件中,您使用大括号引用 auth.iduser.id。尝试使用:

{% if auth.renderFriendShip(auth.id, user.id, 'isThereFriendship') == 0 %}

来自documentation

It's important to know that the curly braces are not part of the variable but the print statement. When accessing variables inside tags, don't put the braces around them.