JavaStudent
JavaStudent

Reputation: 69

Twig Blocks not working

I have several twig files that should reflect a different navbar depending on which page is displayed. It was working fine with if statements in my base.html until I changed some of the routes to include a user id. For example it should be /user/id/details. The base html does not know what user is so I tried to move the navbar into a different twig that does know what user is, but it does not show. Could anyone offer any direction? I am pretty new with Symfony. Thank you!

Here is the code... Base.html

    {% block heading %}
        <div class="main-heading" id="main-heading">
            <div class="main-logo">
                <img src="{{ asset('images/Dashboard-Logo.png') }}">
            </div>
        {% if '/admin/' in app.request.uri %}
            <nav class="navbar navbar-default">
                <!-- /navbar-header -->
                <div class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li><a href="#">Manage Users <span class="sr-only">(current)</span></a></li>
                        <li><a href="{{ path('manage_groups') }}">Manage Groups</a></li>
                        <li><a href="{{ path('export_test_data') }}">Export</a></li>
                    </ul>
                </div><!-- /.navbar-collapse -->
            </nav>
        {% elseif app.request.get('_route') == 'homepage' %}
            <nav class="navbar navbar-default">
                <!-- /navbar-header -->
                <div class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li><a href="#login">User Login <span class="sr-only">(current)</span></a></li>
                        <li><a href="#about">About</a></li>
                        <li><a href="#team">The Team</a></li>
                    </ul>
                </div><!-- /.navbar-collapse -->
            </nav>
    </div><!-- /main-heading -->
        {% else %}
        <nav class="navbar navbar-default">
            <!-- /navbar-header -->
            <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li><a href="#">Dashboard<span class="sr-only">(current)</span></a></li>
                    <li><a href="#">Resources</a></li>
                    <li><a href="#">Profile</a></li>
                </ul>
            </div><!-- /.navbar-collapse -->
        </nav>
        {% endif %}
{% endblock %}

and the other twig template

{% block header %}
<nav class="navbar navbar-default">
    <!-- /navbar-header -->
    <div class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
            <li><a href="{{ path('dashboard', {'id': user.id}) }}">Dashboard <span class="sr-only">(current)</span></a></li>
            <li><a href="{{ path('resources', {'id': user.id}) }}">Resources</a></li>
            <li><a href="{{ path('user_profile', {'id': user.id})}}">Profile</a></li>
        </ul>
    </div><!-- /.navbar-collapse -->
</nav>`enter code here`
{% endblock %}

Here is the controller

namespace AppBundle\Controller;

use AppBundle\Entity\User;
use AppBundle\Entity\UserGroup;
use AppBundle\Entity\BehaviorData;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use AppBundle\Form\UserProfileCreateType;
use AppBundle\Form\BehaviorDataType;
use AppBundle\Form\CreateGoalType;
use AppBundle\Form\InvitationType;

/**
* @Route("/user")
*/    /**
      * @Route("/{id}/profile", name="user_profile")
     */
    public function userProfileCreateAction(EntityManagerInterface $em ,Request $request, User $user)

I won't post the entire controller as it is pretty long and everything else works. I pass the User entity in when I call this function which allows it to show specific user details in the twig.

Upvotes: 2

Views: 911

Answers (1)

JavaStudent
JavaStudent

Reputation: 69

I found what I needed. All I had to do was look in the uri for the user id and pass that into the twig like this:

<li><a href="{{ path('resources', { 'id': app.request.get('id') }) }}">Resources</a></li>

Upvotes: 1

Related Questions