How to render two views in the same twig template
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I want to render two views in the same twig template, but only one of them is rendered, and the second is always override with the same content as the first one.
I have a content type 'contacts' and when its nodes are displayed (full view) I want to render two views in the same page after the node fields. I use a twig template node--contacts.html.twig :
..... node fields twig template ....
viewCalls
....
viewAppointments
....
And my function my_theme_preprocess_node(&$variables)
in my .theme file:
....
$view = DrupalviewsViews::getView('view_calls');
$view->setDisplay('default');
$view->setArguments(array($variables['node']->id()));
$view->setOffset(0);
$view->execute();
$variables['viewCalls'] = $view->render();
$view2 = DrupalviewsViews::getView('view_appointments');
$view2->setDisplay('default');
$view2->setArguments(array($variables['node']->id()));
$view2->setOffset(0);
$view2->execute();
$variables['viewAppointments'] = $view2->render();
.....
As I said, the second view always shows the same content as the first. If I reverse the order of rendering, then the content is reverse too, and the one which is first rendered is the only one I see, and always two times. It is as viewCalls
and viewAppointments
always get the same content.
views 8 theming
add a comment |Â
up vote
2
down vote
favorite
I want to render two views in the same twig template, but only one of them is rendered, and the second is always override with the same content as the first one.
I have a content type 'contacts' and when its nodes are displayed (full view) I want to render two views in the same page after the node fields. I use a twig template node--contacts.html.twig :
..... node fields twig template ....
viewCalls
....
viewAppointments
....
And my function my_theme_preprocess_node(&$variables)
in my .theme file:
....
$view = DrupalviewsViews::getView('view_calls');
$view->setDisplay('default');
$view->setArguments(array($variables['node']->id()));
$view->setOffset(0);
$view->execute();
$variables['viewCalls'] = $view->render();
$view2 = DrupalviewsViews::getView('view_appointments');
$view2->setDisplay('default');
$view2->setArguments(array($variables['node']->id()));
$view2->setOffset(0);
$view2->execute();
$variables['viewAppointments'] = $view2->render();
.....
As I said, the second view always shows the same content as the first. If I reverse the order of rendering, then the content is reverse too, and the one which is first rendered is the only one I see, and always two times. It is as viewCalls
and viewAppointments
always get the same content.
views 8 theming
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I want to render two views in the same twig template, but only one of them is rendered, and the second is always override with the same content as the first one.
I have a content type 'contacts' and when its nodes are displayed (full view) I want to render two views in the same page after the node fields. I use a twig template node--contacts.html.twig :
..... node fields twig template ....
viewCalls
....
viewAppointments
....
And my function my_theme_preprocess_node(&$variables)
in my .theme file:
....
$view = DrupalviewsViews::getView('view_calls');
$view->setDisplay('default');
$view->setArguments(array($variables['node']->id()));
$view->setOffset(0);
$view->execute();
$variables['viewCalls'] = $view->render();
$view2 = DrupalviewsViews::getView('view_appointments');
$view2->setDisplay('default');
$view2->setArguments(array($variables['node']->id()));
$view2->setOffset(0);
$view2->execute();
$variables['viewAppointments'] = $view2->render();
.....
As I said, the second view always shows the same content as the first. If I reverse the order of rendering, then the content is reverse too, and the one which is first rendered is the only one I see, and always two times. It is as viewCalls
and viewAppointments
always get the same content.
views 8 theming
I want to render two views in the same twig template, but only one of them is rendered, and the second is always override with the same content as the first one.
I have a content type 'contacts' and when its nodes are displayed (full view) I want to render two views in the same page after the node fields. I use a twig template node--contacts.html.twig :
..... node fields twig template ....
viewCalls
....
viewAppointments
....
And my function my_theme_preprocess_node(&$variables)
in my .theme file:
....
$view = DrupalviewsViews::getView('view_calls');
$view->setDisplay('default');
$view->setArguments(array($variables['node']->id()));
$view->setOffset(0);
$view->execute();
$variables['viewCalls'] = $view->render();
$view2 = DrupalviewsViews::getView('view_appointments');
$view2->setDisplay('default');
$view2->setArguments(array($variables['node']->id()));
$view2->setOffset(0);
$view2->execute();
$variables['viewAppointments'] = $view2->render();
.....
As I said, the second view always shows the same content as the first. If I reverse the order of rendering, then the content is reverse too, and the one which is first rendered is the only one I see, and always two times. It is as viewCalls
and viewAppointments
always get the same content.
views 8 theming
views 8 theming
edited 1 hour ago
asked 5 hours ago
briast
9410
9410
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
There's at least one typo in your code. Remove the space in $variables['viewAppointments ']
.
Alternatively you could also use Twig Tweak. Which will provide you a function you can use to place views directly in your templates.
drupal_view('who_s_new', 'block_1', arg_1, arg_2, arg_3) `
Twig Tweak is a small module which provides a Twig extension with some
useful functions and filters that can improve development experience.
Read the full guide on Twig Tweak and Views.
Twig Tweak's
drupal_view()
method provide's access to embed views
within any Twig code, including dynamically from within each row of
another view. This feature provides an alternate method to accomplish
the nesting provided by the Views field view module.
1
Thanks for your help. Yes, there was a typo but It was only in my answer. My code is right. Before posting my answer I read about the module you have mentioned. A quick review of this module didn't let me see how to pass arguments to the views, but I'll try it more in depth. Any way, I'm wondering why my code doesn't work and duplicate the view. It's a mystery.
â briast
56 mins ago
@briastdrupal_view('view_calls', 'default', node.id)
â No Sssweat
25 mins ago
add a comment |Â
up vote
0
down vote
The reason you get the same content is due to the #cache
property of the render array you get when you call the render()
method.
Most specifically, by calling setOffset()
you are implicitly setting a cache key on the render array, (see here):
$this->element['#cache']['keys'] = 'offset:' . $offset;
This key setting, in combination with the #theme
property of the render array (views_view
) which is the same for both views, will result in getting a cached version of the first one, when including the second one.
To work around that you could:
- Remove caching completely, or
- Avoid using
setOffset()
, or Add more specific keys to the cache so that you have a different cache for each view. E.g.
$variables['viewCalls']['#cache']['keys'] =
array_merge(
$variables['viewCalls']['#cache']['keys'],
array('viewCalls')
);
...
$variables['viewAppointments']['#cache']['keys'] =
array_merge(
$variables['viewAppointments']['#cache']['keys'],
array('viewAppointments')
);
I tested your case using the 3. option and it worked as expected, I think before resolving to using another module, you could stick to your code.
Good luck!
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
There's at least one typo in your code. Remove the space in $variables['viewAppointments ']
.
Alternatively you could also use Twig Tweak. Which will provide you a function you can use to place views directly in your templates.
drupal_view('who_s_new', 'block_1', arg_1, arg_2, arg_3) `
Twig Tweak is a small module which provides a Twig extension with some
useful functions and filters that can improve development experience.
Read the full guide on Twig Tweak and Views.
Twig Tweak's
drupal_view()
method provide's access to embed views
within any Twig code, including dynamically from within each row of
another view. This feature provides an alternate method to accomplish
the nesting provided by the Views field view module.
1
Thanks for your help. Yes, there was a typo but It was only in my answer. My code is right. Before posting my answer I read about the module you have mentioned. A quick review of this module didn't let me see how to pass arguments to the views, but I'll try it more in depth. Any way, I'm wondering why my code doesn't work and duplicate the view. It's a mystery.
â briast
56 mins ago
@briastdrupal_view('view_calls', 'default', node.id)
â No Sssweat
25 mins ago
add a comment |Â
up vote
1
down vote
There's at least one typo in your code. Remove the space in $variables['viewAppointments ']
.
Alternatively you could also use Twig Tweak. Which will provide you a function you can use to place views directly in your templates.
drupal_view('who_s_new', 'block_1', arg_1, arg_2, arg_3) `
Twig Tweak is a small module which provides a Twig extension with some
useful functions and filters that can improve development experience.
Read the full guide on Twig Tweak and Views.
Twig Tweak's
drupal_view()
method provide's access to embed views
within any Twig code, including dynamically from within each row of
another view. This feature provides an alternate method to accomplish
the nesting provided by the Views field view module.
1
Thanks for your help. Yes, there was a typo but It was only in my answer. My code is right. Before posting my answer I read about the module you have mentioned. A quick review of this module didn't let me see how to pass arguments to the views, but I'll try it more in depth. Any way, I'm wondering why my code doesn't work and duplicate the view. It's a mystery.
â briast
56 mins ago
@briastdrupal_view('view_calls', 'default', node.id)
â No Sssweat
25 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
There's at least one typo in your code. Remove the space in $variables['viewAppointments ']
.
Alternatively you could also use Twig Tweak. Which will provide you a function you can use to place views directly in your templates.
drupal_view('who_s_new', 'block_1', arg_1, arg_2, arg_3) `
Twig Tweak is a small module which provides a Twig extension with some
useful functions and filters that can improve development experience.
Read the full guide on Twig Tweak and Views.
Twig Tweak's
drupal_view()
method provide's access to embed views
within any Twig code, including dynamically from within each row of
another view. This feature provides an alternate method to accomplish
the nesting provided by the Views field view module.
There's at least one typo in your code. Remove the space in $variables['viewAppointments ']
.
Alternatively you could also use Twig Tweak. Which will provide you a function you can use to place views directly in your templates.
drupal_view('who_s_new', 'block_1', arg_1, arg_2, arg_3) `
Twig Tweak is a small module which provides a Twig extension with some
useful functions and filters that can improve development experience.
Read the full guide on Twig Tweak and Views.
Twig Tweak's
drupal_view()
method provide's access to embed views
within any Twig code, including dynamically from within each row of
another view. This feature provides an alternate method to accomplish
the nesting provided by the Views field view module.
answered 2 hours ago
leymannx
5,98742356
5,98742356
1
Thanks for your help. Yes, there was a typo but It was only in my answer. My code is right. Before posting my answer I read about the module you have mentioned. A quick review of this module didn't let me see how to pass arguments to the views, but I'll try it more in depth. Any way, I'm wondering why my code doesn't work and duplicate the view. It's a mystery.
â briast
56 mins ago
@briastdrupal_view('view_calls', 'default', node.id)
â No Sssweat
25 mins ago
add a comment |Â
1
Thanks for your help. Yes, there was a typo but It was only in my answer. My code is right. Before posting my answer I read about the module you have mentioned. A quick review of this module didn't let me see how to pass arguments to the views, but I'll try it more in depth. Any way, I'm wondering why my code doesn't work and duplicate the view. It's a mystery.
â briast
56 mins ago
@briastdrupal_view('view_calls', 'default', node.id)
â No Sssweat
25 mins ago
1
1
Thanks for your help. Yes, there was a typo but It was only in my answer. My code is right. Before posting my answer I read about the module you have mentioned. A quick review of this module didn't let me see how to pass arguments to the views, but I'll try it more in depth. Any way, I'm wondering why my code doesn't work and duplicate the view. It's a mystery.
â briast
56 mins ago
Thanks for your help. Yes, there was a typo but It was only in my answer. My code is right. Before posting my answer I read about the module you have mentioned. A quick review of this module didn't let me see how to pass arguments to the views, but I'll try it more in depth. Any way, I'm wondering why my code doesn't work and duplicate the view. It's a mystery.
â briast
56 mins ago
@briast
drupal_view('view_calls', 'default', node.id)
â No Sssweat
25 mins ago
@briast
drupal_view('view_calls', 'default', node.id)
â No Sssweat
25 mins ago
add a comment |Â
up vote
0
down vote
The reason you get the same content is due to the #cache
property of the render array you get when you call the render()
method.
Most specifically, by calling setOffset()
you are implicitly setting a cache key on the render array, (see here):
$this->element['#cache']['keys'] = 'offset:' . $offset;
This key setting, in combination with the #theme
property of the render array (views_view
) which is the same for both views, will result in getting a cached version of the first one, when including the second one.
To work around that you could:
- Remove caching completely, or
- Avoid using
setOffset()
, or Add more specific keys to the cache so that you have a different cache for each view. E.g.
$variables['viewCalls']['#cache']['keys'] =
array_merge(
$variables['viewCalls']['#cache']['keys'],
array('viewCalls')
);
...
$variables['viewAppointments']['#cache']['keys'] =
array_merge(
$variables['viewAppointments']['#cache']['keys'],
array('viewAppointments')
);
I tested your case using the 3. option and it worked as expected, I think before resolving to using another module, you could stick to your code.
Good luck!
add a comment |Â
up vote
0
down vote
The reason you get the same content is due to the #cache
property of the render array you get when you call the render()
method.
Most specifically, by calling setOffset()
you are implicitly setting a cache key on the render array, (see here):
$this->element['#cache']['keys'] = 'offset:' . $offset;
This key setting, in combination with the #theme
property of the render array (views_view
) which is the same for both views, will result in getting a cached version of the first one, when including the second one.
To work around that you could:
- Remove caching completely, or
- Avoid using
setOffset()
, or Add more specific keys to the cache so that you have a different cache for each view. E.g.
$variables['viewCalls']['#cache']['keys'] =
array_merge(
$variables['viewCalls']['#cache']['keys'],
array('viewCalls')
);
...
$variables['viewAppointments']['#cache']['keys'] =
array_merge(
$variables['viewAppointments']['#cache']['keys'],
array('viewAppointments')
);
I tested your case using the 3. option and it worked as expected, I think before resolving to using another module, you could stick to your code.
Good luck!
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The reason you get the same content is due to the #cache
property of the render array you get when you call the render()
method.
Most specifically, by calling setOffset()
you are implicitly setting a cache key on the render array, (see here):
$this->element['#cache']['keys'] = 'offset:' . $offset;
This key setting, in combination with the #theme
property of the render array (views_view
) which is the same for both views, will result in getting a cached version of the first one, when including the second one.
To work around that you could:
- Remove caching completely, or
- Avoid using
setOffset()
, or Add more specific keys to the cache so that you have a different cache for each view. E.g.
$variables['viewCalls']['#cache']['keys'] =
array_merge(
$variables['viewCalls']['#cache']['keys'],
array('viewCalls')
);
...
$variables['viewAppointments']['#cache']['keys'] =
array_merge(
$variables['viewAppointments']['#cache']['keys'],
array('viewAppointments')
);
I tested your case using the 3. option and it worked as expected, I think before resolving to using another module, you could stick to your code.
Good luck!
The reason you get the same content is due to the #cache
property of the render array you get when you call the render()
method.
Most specifically, by calling setOffset()
you are implicitly setting a cache key on the render array, (see here):
$this->element['#cache']['keys'] = 'offset:' . $offset;
This key setting, in combination with the #theme
property of the render array (views_view
) which is the same for both views, will result in getting a cached version of the first one, when including the second one.
To work around that you could:
- Remove caching completely, or
- Avoid using
setOffset()
, or Add more specific keys to the cache so that you have a different cache for each view. E.g.
$variables['viewCalls']['#cache']['keys'] =
array_merge(
$variables['viewCalls']['#cache']['keys'],
array('viewCalls')
);
...
$variables['viewAppointments']['#cache']['keys'] =
array_merge(
$variables['viewAppointments']['#cache']['keys'],
array('viewAppointments')
);
I tested your case using the 3. option and it worked as expected, I think before resolving to using another module, you could stick to your code.
Good luck!
edited 7 mins ago
answered 13 mins ago
Stefanos Petrakis
5,9502719
5,9502719
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f271663%2fhow-to-render-two-views-in-the-same-twig-template%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password