簡體   English   中英

Symfony 4 - base.html.twig 中的“變量不存在”

[英]Symfony 4 - "variable does not exist" in base.html.twig

我有一個博客,有兩條路由重定向到我的個人資料頁面和我的“更新個人資料”頁面

@Route("/profil/{slug}", name="profile")

@Route("/profil/{slug}/infos", name="profile_update")

這些路線在我的導航欄中有兩個鏈接,這是我的 base.html.twig 模板的一部分

所以在我的 base.html.twig 我做了這樣的事情

 <a href="{{ path('profile',  {'slug' : membre.slug }) }}" class="dropdown-item">Account</a>
 <a href="{{ path('profile_update',  {'slug' : membre.slug }) }}" class="dropdown-item">Update profile</a>

但它說“變量成員不存在”:(

它只適用於我的個人資料路由,因為在它的模板中,我在頁面內也有這些鏈接,所以在控制器中我做了:

    /**
     * @Route("/profil/{slug}", name="profile")
     */
    public function profile(Membre $membre){

        return $this->render('blog/profile.html.twig', [
            'membre' => $membre
        ]);
    }

它可以工作,但是 base.html.twig沒有 CONTROLLER它只是一個“基本”模板,所以我該如何告訴它變量來自哪里?

謝謝

問題是所有控制器操作都使用base.html.twig ,無論用戶是否設置為模板變量。 一種解決方案是僅在設置成員變量時顯示這些鏈接:

{% if membre is defined %}
    <a href="{{ path('profile',  {'slug' : membre.slug }) }}" class="dropdown-item">Account</a>
    <a href="{{ path('profile_update',  {'slug' : membre.slug }) }}" class="dropdown-item">Update profile</a>
{% endif %}

唯一的其他選擇是始終將該變量傳遞給模板,這將很困難,因為它需要一些額外的輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM