Dominykas55
Dominykas55

Reputation: 1231

Symfony2 is refreshing a twigs block with ajax possible?

Lets say I have a block in my layout:

{% block sidebar %} {% render url( 'sidebar' ) %} {% endblock %}

Is it possible to refresh the block with ajax without making a div around it? In my example I cant make a div, because it crashes my whole template so I need to know is that even possible?

For example I can refresh a div like this(.test is the class of the table):

$('.test').load(" .test");

Can I make something like this to refresh the block?

$('sidebar').load(" sidebar");

Any ideas?

Upvotes: 0

Views: 813

Answers (2)

valepu
valepu

Reputation: 3315

Symfony works server side so it can't know what happens in your DOM once the page has been rendered. Also jquery can't know about twig blocks since those are parsed server side.

I'm gona give you a "stupid" (and probably i'm even going against good practices, depending on the content you are rendering) answer but maybe it can be of help: have you tried putting a "span" around it instead of a div?

{% block sidebar %} <span class="test">{% render url( 'sidebar' ) %}</span> {% endblock %}

EDIT: I think an explanation is due:
This answer is correct assuming there aren't divs inside your sidebar, otherwise it's just a "cheap trick" and might cause other issues (if not now, maybe in the future) See div inside the span element for example.
Probably you should need to check your layout if adding div screws it up.
An alternative i suggest you to try if it can work in your case is to use inline-block divs.

{% block sidebar %} <div class="test">{% render url( 'sidebar' ) %}</div>{% endblock %}

Then, in your css:

.test {
  display: inline-block;
}

See http://learnlayout.com/inline-block.html (and remember it's not entirely supported by IE6 and 7)

Upvotes: 1

Apul Gupta
Apul Gupta

Reputation: 3034

No. It is not possible.

The reason behind this is separations of web tiers i.e. jQuery runs on Client end which TWIG on Server.

Upvotes: 1

Related Questions