user3165474
user3165474

Reputation: 164

Symfony 2 : How to reuse variable(username) inside template?

The error :

 Variable "username" does not exist in HomeHomepageBundle:Template:header.html.twig
 at line 27

DefaultController.php

public function indexAction(Request $request)
    {
        $session = $this->getRequest()->getSession();
        $em = $this->getDoctrine()->getManager();
        $repository = $em->getRepository('LoginLoginBundle:Users');

        if ($request->getMethod() == 'POST') {
            $session->clear();
            $username = $request->get('username');
            $password = sha1($request->get('password'));
            $user = $repository->findOneBy(array(
                'username' => $username,
                'password' => $password));

            if ($user) {

                return $this->render('HomeHomepageBundle:Content:Dashboard.html.twig', array(

                    'username' => $user->getUsername() ,
                    'password' => $user->getPassword()));

            } else {
                return $this->render('LoginLoginBundle:Default:index.html.twig', array('username' => 'Username or Password Incorrect'));
                $em->persist($user);
                $em->flush();
            }
        } else
        return $this->render('LoginLoginBundle:Default:index.html.twig');

header.html.twig

 <span data-brackets-id='1813' class="hidden-xs">{{ username }}</span>

I split the template into header.html.twig footer.html.twig leftpanel.html.twig and rightpanel.html.twig and include it inside default.html.twig

Index.html.twig

    {% include 'HomeHomepageBundle:Template:header.html.twig' %}
    {% include 'HomeHomepageBundle:Template:leftpanel.html.twig' %}
    </aside>
    <div data-bracets-id='1869' class="content-wrapper">
   {% block content  %}{% endblock %}
        <section data-brackets-id='1878' class="content">
        </section>
    </div>
    {% include 'HomeHomepageBundle:Template:footer.html.twig' %}
    {% include 'HomeHomepageBundle:Template:rightpanel.html.twig' %}

The Problem start when i try to change the {%block content %}. Any idea for best practice rendering username on header while change only the content?

Dashboard.html.twig

{% extends 'HomeHomepageBundle:Default:index.html.twig' %}
{% block content  %}
<section data-brackets-id='1870' class="content-header">
    <h1 data-brackets-id='1871'>
        Greetings! {{ username }}<br>
        <small data-brackets-id='1872'>Development in progress</small>
    </h1>
    <ol data-brackets-id='1873' class="breadcrumb">
        <li data-brackets-id='1874'><a data-brackets-id='1875' href="#"><i data-brackets-id='1876' class="fa fa-dashboard"></i>index</a></li>
        <li data-brackets-id='1877' class="active">Dashboard</li>
    </ol>
</section>
{% endblock %}
{% block javascript %}{% endblock %}

Upvotes: 0

Views: 364

Answers (3)

Alex
Alex

Reputation: 1183

You should pass 'username' variable into template by doing this:

$this->render('<Template>', ['username' => 'Alex' /* or $username */]);

Look carefully at the last line of the indexAction: you don't pass anything if it's get request.

You may want to pass 'username' var into partial template, which you include in index.html.twig. But until you didn't pass it into main template it won't appear in partial template. Last twig versions automatically pass all variables passed into main template to partial templates - so there is no need to do it. Once again: the last line of indexAction in controller says that you don't pass any variables into template when request method is GET.

Upvotes: 1

Joshua
Joshua

Reputation: 2982

First of all, Daniel's way is the way to go if you want to access the user's name in twig templates.

More generic; you can pass variables to included templates like this:

{% include 'template.html' with {'foo': 'bar'} %}

For more info on this topic, read the documentation: http://twig.sensiolabs.org/doc/tags/include.html

Upvotes: 1

Daniel
Daniel

Reputation: 8667

  • If user is logged in you can use {{ app.user.username }} in twig.
  • If you want to use variable username only if it is defined type: {% if username is defined %}{{ username }}{% endif %}.
  • If you want to use username always, define it in controller before first if statement.

Upvotes: 0

Related Questions