Twig की सर्वोत्तम प्रथाएँ — फ़ंक्शंस और प्री-प्रोसेसिंग टेम्पलेट्स
Drupal 8 थीमिंग को अधिकतम परफ़ॉर्मेंस देने और Twig टेम्पलेट्स में बेहतर कस्टमाइज़ेशन विकल्प सुनिश्चित करने के लिए नीचे दिए गए सुझावों का पालन करें:
यह गाइड Drupal डेवलपर्स की मदद के लिए लिखा गया है, जिनके पास Drupal 7 का अनुभव हो सकता है और जो theme() या drupal_render() जैसी फ़ंक्शंस को हटाना चाहते हैं, जिन्हें अब Drupal 8+ में उपयोग नहीं करना चाहिए। यहाँ दिए गए "पहले" (Before) वाले उदाहरण सामान्यत: Drupal 7 स्टाइल कोड में हैं।
प्री-प्रोसेस फ़ंक्शंस से रेंडर एरेज़ रिटर्न करना
हमेशा theme() या drupal_render() को कॉल करने के बजाय रेंडर एरेज़ रिटर्न करें।
Twig सब कुछ ऑटोमैटिकली रेंडर करता है, इसलिए प्री-प्रोसेस फ़ंक्शन के अंदर drupal_render() या theme() कॉल करने की ज़रूरत नहीं है। इसके बजाय, रेंडर एरेज़ को टेम्पलेट तक पास करना चाहिए, क्योंकि इससे पहले से रेंडर की गई HTML स्ट्रिंग की तुलना में बहुत अधिक कस्टमाइज़ेशन मिलता है।
theme() हटाने का उदाहरण:
// पहले - टेम्पलेट में HTML स्ट्रिंग पास करना
$variables['table'] = theme('table', ['header' => $header, 'rows' => $rows]);
// बाद में - टेम्पलेट में रेंडर एरे पास करना
$variables['table'] = [
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
];
drupal_render() हटाने का उदाहरण:
// पहले, drupal_render() का अनावश्यक कॉल $variables['teaser'] = drupal_render($node_teaser); // बाद में, drupal_render() हटाकर $variables['teaser'] = $node_teaser;
अक्सर drupal_render() का उपयोग डेटा टेबल में ऐड करने पर होता था:
// पहले, drupal_render() का अनावश्यक कॉल $row[] = drupal_render($display['title']); // बाद में, drupal_render() हटाकर $row[]['data'] = $display['title'];
फ़िल्टर और यूटिलिटी फ़ंक्शंस को टेम्पलेट्स में कॉल करना
हालाँकि रेंडर एरेज़ डेटा के लिए स्ट्रक्चर्ड और एडिटेबल स्ट्रक्चर प्रदान करते हैं, लेकिन सभी वेरिएबल्स को रेंडर एरे की ज़रूरत नहीं होती। टेम्पलेट्स को कच्चा डेटा (raw data) यथासंभव लंबे समय तक देने के लिए, थीम डेवलपर्स को फ़िल्टर्स (जैसे t) और यूटिलिटी फ़ंक्शंस (जैसे url()) को Twig टेम्पलेट्स में कॉल करना चाहिए। इन्हें प्री-प्रोसेस फ़ंक्शन में कॉल करने की बजाय टेम्पलेट्स में कॉल करना ज़्यादा परफ़ॉर्मेंस देता है।
पहले:
प्री-प्रोसेस फ़ंक्शन में:
$variables['no_content_text'] = t('You have not created any content types yet. Go to the <a href="@create-content">content type creation page</a> to add a new content type.', array('@create-content' => url('admin/structure/types/add')));
टेम्पलेट में:
<p>{{ no_content_text }}</p>
बाद में:
सीधे टेम्पलेट में:
<p>{{ 'You have not created any content types yet. Go to the <a href="@create-content">content type creation page</a> to add a new content type.'|t({'@create-content': url('admin/structure/types/add')}) }}</p>
drupal_render_children और element_children हटाना
यदि hide() को पुराने टेम्पलेट में कॉल किया गया था और drupal_render_children() का उपयोग बाकी डेटा को रेंडर करने के लिए किया गया था, तो अब इन्हें प्री-प्रोसेस के दौरान अलग-अलग वेरिएबल्स में विभाजित करना होगा।
पहले (PHPTemplate फ़ाइल):
<?php
hide($form['advanced']);
hide($form['actions']);
?>
<div class="layout-node-form clearfix">
<div class="layout-region layout-region-node-main">
<?php print drupal_render_children($form); ?>
</div>
<div class="layout-region layout-region-node-secondary">
<?php print render($form['advanced']); ?>
</div>
<div class="layout-region layout-region-node-footer">
<?php print render($form['actions']); ?>
</div>
</div>
अब Twig का without फ़िल्टर उपयोग करें, ताकि कुछ एलिमेंट्स को छुपाया जा सके:
बाद में (Twig टेम्पलेट):
<div class="layout-node-form clearfix">
<div class="layout-region layout-region-node-main">
{{ form|without('advanced', 'actions') }}
</div>
<div class="layout-region layout-region-node-secondary">
{{ form.advanced }}
</div>
<div class="layout-region layout-region-node-footer">
{{ form.actions }}
</div>
</div>
वैकल्पिक तरीका (अब ज़रूरी नहीं):
सब कुछ प्री-प्रोसेस में अलग-अलग वेरिएबल्स में सेट करें और टेम्पलेट को पास करें।
पहले (प्री-प्रोसेस):
function template_preprocess_node_edit_form(&$variables) {
$form = $variables['form'];
$variables['advanced'] = $form['advanced'];
$variables['actions'] = $form['actions'];
unset($form['advanced'], $form['actions']);
$variables['form'] = drupal_render_children($form);
}
बाद में (Twig टेम्पलेट):
<div class="layout-node-form clearfix">
<div class="layout-region layout-region-node-main">
{{ form }}
</div>
<div class="layout-region layout-region-node-secondary">
{{ advanced }}
</div>
<div class="layout-region layout-region-node-footer">
{{ actions }}
</div>
</div>