print nid in drupal 8 twig file or template file
add this code in _preprocess_page in .theme file at following location
ryder/themes/theamname/theamname.theme
find for _preprocess_page and add this
$node = \Drupal::routeMatch()->getParameter('node');
if ($node) {
$variables['nodeid'] = $node->id();
}
now in twig print as {{ nodeid }}
add this code in _preprocess_page in .theme file at following location
ryder/themes/theamname/theamname.theme
find for _preprocess_page and add this
$node = \Drupal::routeMatch()->getParameter('node');
if ($node) {
$variables['nodeid'] = $node->id();
}
now in twig print as {{ nodeid }}
Note: The above code works if you are wanting to render the Node ID on a page template. If you are wanting to do it in another type of template change the preprocess function name accordingly. Example: For field templates use: _preprocess_field
ReplyDelete