<p>I am trying to create a seamless user login interaction with a modal form using ajax. Let's say I have a navigation template <code>_navigation.html</code></p>

<pre><code>&lt;div&gt;
    {% if currentUser %}
    {{ currentUser }}
    — &lt;a href="{{ url('shop/customer') }}"&gt;{{ "My Orders"|t }}&lt;/a&gt;
    — &lt;a href="{{ url('shop/customer/addresses') }}"&gt;{{ "My Addresses"|t }}&lt;/a&gt;
    — &lt;a href="{{ url('shop/customer/cards') }}"&gt;{{ "My Cards"|t }}&lt;/a&gt;
    — &lt;a href="{{ logoutUrl }}"&gt;Logout&lt;/a&gt;
    {% else %}
      &lt;a href="" data-toggle="modal" data-target="#loginModal"&gt;{{ "Login"|t }}&lt;/a&gt;
    — &lt;a href="" data-toggle="modal" data-target="#registerModal"&gt;{{ "Register"|t }}&lt;/a&gt;
    {% endif %}
&lt;/div&gt;
</code></pre>

<p>After successful login I would like to refresh the view by loading the template again (using <a href="http://api.jquery.com/load/" rel="nofollow noreferrer">jquery.load()</a>). Since the template is "hidden" I can't access it. If I make the template "visible" all works, but naturally I would like the template to stay "hidden".</p>

<p>Is there any way to achieve this?</p>

CC BY-SA 4.0
2018-05-20T12:03:26.597Z
I would suggest you to create a custom controller that returns the required html together with the login response. Otherwise you would have to do a dirty work around via 2 ajax requests
Robin Schambach
CC BY-SA 4.0
2018-05-20T12:17:59.443Z
Cool, thanks. I will look into custom controllers. What do you mean with 2 ajax requests?
krizajb
CC BY-SA 4.0
2018-05-20T12:27:44.600Z
Some people prefer to now use PHP at all. So a (pretty bad) work around would be to create a second ajax request to your template and insert the result in your container
Robin Schambach
CC BY-SA 4.0
2018-05-20T12:31:01.490Z
Do you think "hidden" template would be accessible via ajax?
krizajb
CC BY-SA 4.0
2018-05-20T12:32:58.110Z
When you create a custom route yes
Robin Schambach
CC BY-SA 4.0
2018-05-20T12:35:43.007Z
But does it only make it accessible via ajax or does it become public? I would assume making a second ajax request is a lot slower then returning html with custom controller in first step?
krizajb
CC BY-SA 4.0
2018-05-20T12:38:18.933Z
It will be public... That's why I always prefer a controller
Robin Schambach
CC BY-SA 4.0
2018-05-20T12:45:24.610Z
OK, thank you. I would recommend you making an answer out of this conversation. If not, I will answer it myself once I'm done with the custom controller and make it work :)
krizajb
CC BY-SA 4.0
2018-05-21T08:21:29.367Z
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/77784/discussion-between-krizajb-and-robin-schambach).
krizajb
<templating><ajax><login>
Refresh "hidden" template after login with ajax
# Refresh "hidden" template after login with ajax

(Asked by: username_0 on 2018-05-20)

I am trying to create a seamless user login interaction with a modal form using ajax. Let's say I have a navigation template _navigation.html

    {% if currentUser %}
    {{ currentUser }}
    — {{ "My Orders"|t }}
    — {{ "My Addresses"|t }}
    — {{ "My Cards"|t }}
    — Logout
    {% else %}
      {{ "Login"|t }}
    — {{ "Register"|t }}
    {% endif %}


After successful login I would like to refresh the view by loading the template again (using jquery.load()). Since the template is "hidden" I can't access it. If I make the template "visible" all works, but naturally I would like the template to stay "hidden".
Is there any way to achieve this?

**Tags:** templating, ajax, login
> **Comments:**
> **username_1**: I would suggest you to create a custom controller that returns the required html together with the login response. Otherwise you would have to do a dirty work around via 2 ajax requests  
> **username_0**: Cool, thanks. I will look into custom controllers. What do you mean with 2 ajax requests?  
> **username_1**: Some people prefer to now use PHP at all. So a (pretty bad) work around would be to create a second ajax request to your template and insert the result in your container  
> **username_0**: Do you think "hidden" template would be accessible via ajax?  
> **username_1**: When you create a custom route yes  
> **username_0**: But does it only make it accessible via ajax or does it become public? I would assume making a second ajax request is a lot slower then returning html with custom controller in first step?  
> **username_1**: It will be public... That's why I always prefer a controller  
> **username_0**: OK, thank you. I would recommend you making an answer out of this conversation. If not, I will answer it myself once I'm done with the custom controller and make it work :)  
> **username_0**: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/77784/discussion-between-krizajb-and-robin-schambach).  

---

## 0 answers

