It is easy way to render/call template(.html.twig) file for custom module in drupal 8, But here concept is slightly different from drupal 7.
First we will go to the Controller file:
Like our controller name is ContactController.php so we will write there code like.
//Go to the ContactController.php file..
/**
* @file
* Contains \Drupal\contact\Controller\ContactController.
*/
namespace Drupal\contact\Controller;
use Drupal\node\Entity\Node;
use Drupal\Core\Controller\ControllerBase;
class Contactv4Controller extends ControllerBase {
public function indexv2() {
$data = "<div>Testing</div>";
return [
'#theme' => 'specific_contact',
'#contact' => $data,
];
}
}
// Then go to the contact.module file.
/**
* Implements hook_theme() to add the template definition..
**/
function contact_theme($existing, $type, $theme, $path) {
return array(
'specific_contact' => [
'variables' => [
'contact' => NULL,
]
]
);
}
// Now in the create template folder and the file with name of 'specific_contact.html.twig'.
And render the variable there like: <p> {{ contact }} </p>
And clear the cache after this.
Now Front end result will be 'Testing' on new template page.
Note: If have any suggestions or issue regarding 'How to render template for custom module in drupal 8?' then you can ask by comments.
First we will go to the Controller file:
Like our controller name is ContactController.php so we will write there code like.
//Go to the ContactController.php file..
/**
* @file
* Contains \Drupal\contact\Controller\ContactController.
*/
namespace Drupal\contact\Controller;
use Drupal\node\Entity\Node;
use Drupal\Core\Controller\ControllerBase;
class Contactv4Controller extends ControllerBase {
public function indexv2() {
$data = "<div>Testing</div>";
return [
'#theme' => 'specific_contact',
'#contact' => $data,
];
}
}
// Then go to the contact.module file.
/**
* Implements hook_theme() to add the template definition..
**/
function contact_theme($existing, $type, $theme, $path) {
return array(
'specific_contact' => [
'variables' => [
'contact' => NULL,
]
]
);
}
// Now in the create template folder and the file with name of 'specific_contact.html.twig'.
And render the variable there like: <p> {{ contact }} </p>
And clear the cache after this.
Now Front end result will be 'Testing' on new template page.
Note: If have any suggestions or issue regarding 'How to render template for custom module in drupal 8?' then you can ask by comments.
No comments:
Post a Comment