How to render two views in the same twig template

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question





























    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.










    share|improve this question

























      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago

























      asked 5 hours ago









      briast

      9410




      9410




















          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.







          share|improve this answer
















          • 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










          • @briast drupal_view('view_calls', 'default', node.id)
            – No Sssweat
            25 mins ago

















          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:



          1. Remove caching completely, or

          2. Avoid using setOffset(), or


          3. 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!






          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "220"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            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






























            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.







            share|improve this answer
















            • 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










            • @briast drupal_view('view_calls', 'default', node.id)
              – No Sssweat
              25 mins ago














            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.







            share|improve this answer
















            • 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










            • @briast drupal_view('view_calls', 'default', node.id)
              – No Sssweat
              25 mins ago












            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.







            share|improve this answer












            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.








            share|improve this answer












            share|improve this answer



            share|improve this answer










            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










            • @briast drupal_view('view_calls', 'default', node.id)
              – No Sssweat
              25 mins ago












            • 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










            • @briast drupal_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












            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:



            1. Remove caching completely, or

            2. Avoid using setOffset(), or


            3. 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!






            share|improve this answer


























              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:



              1. Remove caching completely, or

              2. Avoid using setOffset(), or


              3. 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!






              share|improve this answer
























                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:



                1. Remove caching completely, or

                2. Avoid using setOffset(), or


                3. 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!






                share|improve this answer














                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:



                1. Remove caching completely, or

                2. Avoid using setOffset(), or


                3. 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!







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 7 mins ago

























                answered 13 mins ago









                Stefanos Petrakis

                5,9502719




                5,9502719



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    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













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery