I have a view shown in two ways. Once I show it as a normal block and one time using views_embed_view() in combination with Ajax.

For the regular block I created a views-view-list.html.twig template in my theme adding a div wrapper. My styling is based on this added div.

For the embedded block, this views-view-list.html.twig block is ignored and therefor the styling is off.

Using views_embed_view() inside a preprocess does work the way you'd expect, but using it with Ajax ignores the templates.

How can I make sure the template is not ignored, or is there a way to add another template that is seen when embedding the view?

I have a custom module my_view_ajax

I attach the js file in my .module file:

function my_view_ajax_preprocess_node(&$variables) {
  $node = $variables['node'];
  //check content type
  if ($node->getType() == 'article' || $node->getType() == 'issue' ) {
    $variables['#attached']['library'][] = 'my_view_ajax/my_view_ajax__js';

In my .js file I use Ajax to get my views block.

const routing_url = '/custom/ajax/articles';
  type: 'GET',
  url: routing_url,
  data: 'nid='+nid,
  dataType: 'html',
  async: true,
  complete: function (data) {


In my routing.yml I create the Ajax path

  path: '/custom/ajax/articles'
    _title: 'Articles'
    _controller: '\Drupal\my_view_ajax\Controller\viewsajaxController::issueArticles'
    _permission: 'access content'

And finally in my viewsajaxController.php

namespace Drupal\my_view_ajax\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\node\Entity\Node;
use \Symfony\Component\HttpFoundation\Response;

class viewsajaxController extends ControllerBase {
  public function issueArticles() {
    $nid = 11;

    $markup = \Drupal::service('renderer')->render(views_embed_view('ccr_overview', 'block_articles_view', $nid));
    return new Response($markup);

1 Answer 1


I have tried to replicate the case as follow.

1.Added following structure in views-view-table--articles.html.twig

<div class="example-template">
{% for row in rows %}
  {% set titletxt = '' %}
  {% set viewnod = '' %}
  {% set editnode = '' %}
    {% for key, column in row.columns %}
      {% for field in column.fields %}                                    
        {% for content in column.content %}
          {% if field == 'title' %}
             {% set titletxt = content.field_output %}
          {% endif %}
          {% if field == 'view-node' %}
             {% set viewnod = content.field_output %}
          {% endif %}              
          {% if field == 'edit-node' %}
             {% set editnode = content.field_output %}
          {% endif %}
        {% endfor %}
      {% endfor %}
    {% endfor %}
{% endfor %}
  1. In node--article.html.twig embed following view

     <div class="relatedarticles">
       <h2>Articles From Block View</h2>
       {% if articles %}
         {{ articles }}
       {% endif %}
  2. in fluffiness.theme add as follow

    function fluffiness_preprocess_node(&$variables) { $relatedArticles = views_embed_view('articles', 'block_1'); $variables['articles'] = \Drupal::service('renderer')->render($relatedArticles);

Then i noticed in node view mode, structure with template of view is rendered as follow.

enter image description here

  • Thanks for your reply, I found out. Because of your test that the problem is in using views embed in combination with Ajax. I updated my question with some extra information.
    – Joost
    Jan 6, 2022 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.