Skip to main content
added 133 characters in body
Source Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

I am sending data from a custom module controller to a template file using the following code.

module.info.yml

name: Module
description: Module description
package: Custom
type: module
version: 1.0
core: 8.x

module.module

function module_theme ($existing, $type, $theme, $path) {
  return array[
 (   'module_template' => array[
 (     'variables' => array[
 (       'var' => array ()[],
        'title' => '')));
      ]
    ]
  ];
}

module.routing.yml

module.event:
  path: '/path/{dynamic}'
  defaults:
    _controller: '\Drupal\module\controller::RunQuery'
    _title: 'Module'
  requirements:
    _permission: 'access content'

module-template.html.twig

<p> test {{ title }}</p>
<ul>
{% for user in var %}
  <li> {{ user.title }} </li>
{% endfor %}
</ul>
test 2

controller.php

namespace Drupal\module;

class controller { 

  public function RunQuery ($dynamic) { 

    // Some query here which work fine and return what I want
    $ok = 'Some value';

    return array[
 (     '#theme' => 'module_template',
      '#var'   => $ok,
      '#title' => 'Title');,
    ];
  } 

}

This will return only test and test 2 in the template file, so the template file is called by the module controller, but it does not show {{ title }} and {{ user.title }}.

I am sending data from a custom module controller to a template file using the following code.

module.info.yml

name: Module
description: Module description
package: Custom
type: module
version: 1.0
core: 8.x

module.module

function module_theme ($existing, $type, $theme, $path) {
  return array ('module_template' => array ('variables' => array ('var' => array (), 'title' => '')));
}

module.routing.yml

module.event:
  path: '/path/{dynamic}'
  defaults:
    _controller: '\Drupal\module\controller::RunQuery'
    _title: 'Module'
  requirements:
    _permission: 'access content'

module-template.html.twig

<p> test {{ title }}</p>
<ul>
{% for user in var %}
<li> {{ user.title }} </li>
{% endfor %}
</ul>
test 2

controller.php

namespace Drupal\module;

class controller {
  public function RunQuery ($dynamic) {
    // Some query here which work fine and return what I want
    $ok = 'Some value';

    return array ('#theme' => 'module_template', '#var' => $ok, '#title' => 'Title');
  }
}

This will return only test and test 2 in the template file, so the template file is called by the module controller, but it does not show {{ title }} and {{ user.title }}.

I am sending data from a custom module controller to a template file using the following code.

module.info.yml

name: Module
description: Module description
package: Custom
type: module
version: 1.0
core: 8.x

module.module

function module_theme($existing, $type, $theme, $path) {
  return [
    'module_template' => [
      'variables' => [
        'var' => [],
        'title' => ''
      ]
    ]
  ];
}

module.routing.yml

module.event:
  path: '/path/{dynamic}'
  defaults:
    _controller: '\Drupal\module\controller::RunQuery'
    _title: 'Module'
  requirements:
    _permission: 'access content'

module-template.html.twig

<p> test {{ title }}</p>
<ul>
{% for user in var %}
  <li> {{ user.title }} </li>
{% endfor %}
</ul>
test 2

controller.php

namespace Drupal\module;

class controller { 

  public function RunQuery($dynamic) { 

    // Some query here which work fine and return what I want
    $ok = 'Some value';

    return [
      '#theme' => 'module_template',
      '#var'   => $ok,
      '#title' => 'Title',
    ];
  } 

}

This will return only test and test 2 in the template file, so the template file is called by the module controller, but it does not show {{ title }} and {{ user.title }}.

removed "Drupal" and tags from the title
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Drupal 8 Data returned data from the controller is not showing using twigin the template file

I have problemam sending data from a custom module controller to a template twig file :using the following code.

module.info.yml:

module.info.yml

name: Module
description: Module description
package: Custom
type: module
version: 1.0
core: 8.x

module.module:

array ('variables' => array ('var' => array (), 'title' => ''))); } **module.routing.yml:** module.event: path: '/path/{dynamic}' defaults: _controller: '\Drupal\module\controller::RunQuery' _title: 'Module' requirements: _permission: 'access content' **module-template.html.twig:**

module.module

function module_theme ($existing, $type, $theme, $path) {
  return array ('module_template' => array ('variables' => array ('var' => array (), 'title' => '')));
}

module.routing.yml

module.event:
  path: '/path/{dynamic}'
  defaults:
    _controller: '\Drupal\module\controller::RunQuery'
    _title: 'Module'
  requirements:
    _permission: 'access content'

module-template.html.twig

<p> test {{ title }}</p>
<ul>
{% for user in var %}
<li> {{ user.title }} </li>
{% endfor %}
</ul>
test 2

controller.php

namespace Drupal\module;

class controller {
  public function RunQuery ($dynamic) {
    // Some query here which work fine and return what I want
    $ok = 'Some value';

    return array ('#theme' => 'module_template', '#var' => $ok, '#title' => 'Title');
  }
}

testThis will return only {{ titletest and }}test 2 in the template file, so the template file is called by the module controller, but it does not show {{ title }} and {{ user.title }}.

    {% for user in var %}
  • {{ user.title }}
  • {% endfor %}
test 2 **controller.php:** 'module_template', '#var' => $ok, '#title' => 'Title'); } } This will return only the texts (test and test 2) in twig file, so the twig file is called by the module controller, but it does not show the *{{ title }}* & *{{ user.title }}*. Thank you for your help in advance.

Drupal 8 returned data from controller not showing using twig

I have problem sending data from a custom module controller to a template twig file :

module.info.yml:

name: Module
description: Module description
package: Custom
type: module
version: 1.0
core: 8.x

module.module:

array ('variables' => array ('var' => array (), 'title' => ''))); } **module.routing.yml:** module.event: path: '/path/{dynamic}' defaults: _controller: '\Drupal\module\controller::RunQuery' _title: 'Module' requirements: _permission: 'access content' **module-template.html.twig:**

test {{ title }}

    {% for user in var %}
  • {{ user.title }}
  • {% endfor %}
test 2 **controller.php:** 'module_template', '#var' => $ok, '#title' => 'Title'); } } This will return only the texts (test and test 2) in twig file, so the twig file is called by the module controller, but it does not show the *{{ title }}* & *{{ user.title }}*. Thank you for your help in advance.

Data returned from the controller is not showing in the template file

I am sending data from a custom module controller to a template file using the following code.

module.info.yml

name: Module
description: Module description
package: Custom
type: module
version: 1.0
core: 8.x

module.module

function module_theme ($existing, $type, $theme, $path) {
  return array ('module_template' => array ('variables' => array ('var' => array (), 'title' => '')));
}

module.routing.yml

module.event:
  path: '/path/{dynamic}'
  defaults:
    _controller: '\Drupal\module\controller::RunQuery'
    _title: 'Module'
  requirements:
    _permission: 'access content'

module-template.html.twig

<p> test {{ title }}</p>
<ul>
{% for user in var %}
<li> {{ user.title }} </li>
{% endfor %}
</ul>
test 2

controller.php

namespace Drupal\module;

class controller {
  public function RunQuery ($dynamic) {
    // Some query here which work fine and return what I want
    $ok = 'Some value';

    return array ('#theme' => 'module_template', '#var' => $ok, '#title' => 'Title');
  }
}

This will return only test and test 2 in the template file, so the template file is called by the module controller, but it does not show {{ title }} and {{ user.title }}.

Source Link
Sinf
  • 71
  • 1
  • 10

Drupal 8 returned data from controller not showing using twig

I have problem sending data from a custom module controller to a template twig file :

module.info.yml:

name: Module
description: Module description
package: Custom
type: module
version: 1.0
core: 8.x

module.module:

array ('variables' => array ('var' => array (), 'title' => ''))); } **module.routing.yml:** module.event: path: '/path/{dynamic}' defaults: _controller: '\Drupal\module\controller::RunQuery' _title: 'Module' requirements: _permission: 'access content' **module-template.html.twig:**

test {{ title }}

    {% for user in var %}
  • {{ user.title }}
  • {% endfor %}
test 2 **controller.php:** 'module_template', '#var' => $ok, '#title' => 'Title'); } } This will return only the texts (test and test 2) in twig file, so the twig file is called by the module controller, but it does not show the *{{ title }}* & *{{ user.title }}*. Thank you for your help in advance.