Smarty 4 प्रवासन
यह मार्गदर्शिका XOOPS में Smarty 3 से Smarty 4 में अपग्रेड करते समय आवश्यक परिवर्तनों और माइग्रेशन चरणों को शामिल करती है। आधुनिक XOOPS इंस्टॉलेशन के साथ अनुकूलता बनाए रखने के लिए इन अंतरों को समझना आवश्यक है।
संबंधित दस्तावेज़ीकरण
Section titled “संबंधित दस्तावेज़ीकरण”- Smarty- मूल बातें - XOOPS में Smarty के मूल सिद्धांत
- थीम-विकास - XOOPS थीम बनाना
- टेम्प्लेट-वेरिएबल्स - टेम्प्लेट में उपलब्ध वेरिएबल्स
परिवर्तनों का अवलोकन
Section titled “परिवर्तनों का अवलोकन”Smarty 4 ने Smarty 3 से कई महत्वपूर्ण बदलाव पेश किए:
- परिवर्तनीय असाइनमेंट व्यवहार बदल गया
{php}टैग पूरी तरह से हटा दिए गए- कैशिंग API परिवर्तन
- संशोधक हैंडलिंग अद्यतन
- सुरक्षा नीति में बदलाव
- अप्रचलित सुविधाएँ हटा दी गईं
परिवर्तनीय पहुंच परिवर्तन
Section titled “परिवर्तनीय पहुंच परिवर्तन”समस्या
Section titled “समस्या”Smarty 2/3 में, निर्दिष्ट मान सीधे पहुंच योग्य थे:
// PHP$GLOBALS['xoopsTpl']->assign('mod_url', $helper->url());{* Smarty 2/3 - worked fine *}<img src="<{$mod_url}>/assets/images/icon.png">Smarty 4 में, वेरिएबल्स को Smarty_Variable ऑब्जेक्ट में लपेटा जाता है:
Smarty_Variable Object( [value] => http://example.com/modules/mymodule/ [nocache] =>)समाधान 1: मूल्य संपत्ति तक पहुंचें
Section titled “समाधान 1: मूल्य संपत्ति तक पहुंचें”{* Smarty 4 - access the value property *}<img src="<{$mod_url->value}>/assets/images/icon.png">समाधान 2: संगतता मोड
Section titled “समाधान 2: संगतता मोड”PHP में संगतता मोड सक्षम करें:
$smarty = new Smarty();$smarty->setCompatibilityMode(true);यह Smarty 3 जैसे सीधे वैरिएबल एक्सेस की अनुमति देता है।
समाधान 3: सशर्त संस्करण जाँच
Section titled “समाधान 3: सशर्त संस्करण जाँच”ऐसे टेम्प्लेट लिखें जो दोनों संस्करणों में काम करते हों:
<{if $smarty.version|regex_replace:'[^0-9]':'' >= 4}> <{$mod_url->value}><{else}> <{$mod_url}><{/if}>समाधान 4: रैपर फ़ंक्शन
Section titled “समाधान 4: रैपर फ़ंक्शन”असाइनमेंट के लिए एक सहायक फ़ंक्शन बनाएं:
function smartyAssign($smarty, $name, $value){ if (version_compare($smarty->version, '4.0.0', '>=')) { // Smarty 4+ - assign normally, access via ->value in templates $smarty->assign($name, $value); } else { // Smarty 3 - standard assignment $smarty->assign($name, $value); }}{php} टैग हटाना
Section titled “{php} टैग हटाना”समस्या
Section titled “समस्या”Smarty 3+ सुरक्षा कारणों से {php} टैग का समर्थन नहीं करता:
{* This NO LONGER works in Smarty 3+ *}<{assign var="cid" value=$downloads.cid}><{php}> $catid = $this->get_template_vars('cid');<{/php}>समाधान: Smarty वेरिएबल का उपयोग करें
Section titled “समाधान: Smarty वेरिएबल का उपयोग करें”{* Use Smarty's built-in variable access *}<{assign var="cid" value=$downloads.cid}><{assign var="catid" value=$smarty.template_vars.cid}>समाधान: तर्क को PHP में ले जाएँ
Section titled “समाधान: तर्क को PHP में ले जाएँ”जटिल तर्क PHP में होना चाहिए, टेम्पलेट नहीं:
// In PHP - do the processing$catid = $downloads['cid'];$categoryInfo = getCategoryInfo($catid);
// Assign processed data to template$GLOBALS['xoopsTpl']->assign('category', $categoryInfo);{* In template - just display *}<h2><{$category.name}></h2>समाधान: कस्टम प्लगइन्स
Section titled “समाधान: कस्टम प्लगइन्स”पुन: प्रयोज्य कार्यक्षमता के लिए, Smarty प्लगइन बनाएं:
function smarty_function_getcategory($params, $smarty){ $catId = $params['id'] ?? 0; $categoryHandler = xoops_getModuleHandler('category', 'mymodule'); $category = $categoryHandler->get($catId);
if ($category) { $smarty->assign($params['assign'], $category->toArray()); }}{* In template *}<{getcategory id=$cid assign="category"}><h2><{$category.name}></h2>कैशिंग परिवर्तन
Section titled “कैशिंग परिवर्तन”Smarty 3 कैशिंग
Section titled “Smarty 3 कैशिंग”// Smarty 3 style$smarty->caching = true;$smarty->cache_lifetime = 3600;$smarty->cache_dir = '/path/to/cache';
// Per-variable nocache$xoopsTpl->tpl_vars["mod_url"]->nocache = false;Smarty 4 कैशिंग
Section titled “Smarty 4 कैशिंग”// Smarty 4 style$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);$smarty->setCacheLifetime(3600);$smarty->setCacheDir('/path/to/cache');
// Or using properties (still works)$smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;$smarty->cache_lifetime = 3600;कैशिंग स्थिरांक
Section titled “कैशिंग स्थिरांक”// Caching modesSmarty::CACHING_OFF // No cachingSmarty::CACHING_LIFETIME_CURRENT // Use cache_lifetimeSmarty::CACHING_LIFETIME_SAVED // Use cached lifetimeटेम्प्लेट में नोकैचे
Section titled “टेम्प्लेट में नोकैचे”{* Mark content as never cached *}<{nocache}> <p>Current time: <{$smarty.now|date_format:"%H:%M:%S"}></p><{/nocache}>संशोधक परिवर्तन
Section titled “संशोधक परिवर्तन”स्ट्रिंग संशोधक
Section titled “स्ट्रिंग संशोधक”कुछ संशोधकों का नाम बदल दिया गया या हटा दिया गया:
{* Smarty 3 *}<{$text|escape:'htmlall'}>
{* Smarty 4 - use 'html' instead *}<{$text|escape:'html'}>सारणी संशोधक
Section titled “सारणी संशोधक”सरणी संशोधक को @ उपसर्ग की आवश्यकता होती है:
{* Count array elements *}<{$items|@count}> items
{* Join array *}<{$tags|@implode:', '}>
{* JSON encode *}<{$data|@json_encode}>कस्टम संशोधक
Section titled “कस्टम संशोधक”कस्टम संशोधक पंजीकृत होना चाहिए:
// Register a custom modifier$smarty->registerPlugin('modifier', 'my_modifier', 'my_modifier_function');
function my_modifier_function($string, $param1 = 'default'){ // Process and return return processed_string($string, $param1);}सुरक्षा नीति में बदलाव
Section titled “सुरक्षा नीति में बदलाव”Smarty 4 सुरक्षा
Section titled “Smarty 4 सुरक्षा”Smarty 4 में कड़ी डिफ़ॉल्ट सुरक्षा है:
// Configure security policy$smarty->enableSecurity('Smarty_Security');
// Or create custom policyclass MySecurityPolicy extends Smarty_Security{ public $php_functions = ['isset', 'empty', 'count']; public $php_modifiers = ['escape', 'count']; public $allow_super_globals = false;}
$smarty->enableSecurity(new MySecurityPolicy($smarty));अनुमत कार्य
Section titled “अनुमत कार्य”डिफ़ॉल्ट रूप से, Smarty 4 प्रतिबंधित करता है कि कौन से PHP फ़ंक्शंस का उपयोग किया जा सकता है:
{* These may be restricted *}<{if isset($variable)}><{if empty($array)}><{$array|@count}>यदि आवश्यक हो तो अनुमत कार्यों को कॉन्फ़िगर करें:
$smarty->security_policy->php_functions = [ 'isset', 'empty', 'count', 'sizeof', 'in_array', 'is_array', 'date', 'time'];टेम्पलेट इनहेरिटेंस अपडेट
Section titled “टेम्पलेट इनहेरिटेंस अपडेट”सिंटेक्स को ब्लॉक करें
Section titled “सिंटेक्स को ब्लॉक करें”ब्लॉक सिंटैक्स समान रहता है लेकिन कुछ बदलावों के साथ:
{* Parent template *}<html><head> {block name=head} <title>Default Title</title> {/block}</head><body> {block name=content}{/block}</body></html>{* Child template *}{extends file="parent.tpl"}
{block name=head} {$smarty.block.parent} {* Include parent block content *} <meta name="custom" content="value">{/block}
{block name=content} <h1>My Content</h1>{/block}जोड़ें और जोड़ें
Section titled “जोड़ें और जोड़ें”{block name=head append} {* This is added after parent content *} <link rel="stylesheet" href="extra.css">{/block}
{block name=scripts prepend} {* This is added before parent content *} <script src="early.js"></script>{/block}बहिष्कृत सुविधाएँ
Section titled “बहिष्कृत सुविधाएँ”Smarty 4 में हटाया गया
Section titled “Smarty 4 में हटाया गया”| फ़ीचर | वैकल्पिक |
|---|---|
{php} टैग | तर्क को PHP में ले जाएँ या प्लगइन्स का उपयोग करें |
{include_php} | पंजीकृत प्लगइन्स का उपयोग करें |
$smarty.capture | अभी भी काम करता है लेकिन बहिष्कृत |
{strip} रिक्त स्थान के साथ | लघुकरण उपकरण का उपयोग करें |
विकल्पों का उपयोग करें
Section titled “विकल्पों का उपयोग करें”{* Instead of {php} *}{* Move to PHP and assign result *}
{* Instead of include_php *}<{include file="db:mytemplate.tpl"}>
{* Instead of capture (still works but consider) *}<{capture name="sidebar"}> <h3>Sidebar</h3><{/capture}><div><{$smarty.capture.sidebar}></div>माइग्रेशन चेकलिस्ट
Section titled “माइग्रेशन चेकलिस्ट”प्रवास से पहले
Section titled “प्रवास से पहले”- सभी टेम्प्लेट का बैकअप लें
- सभी @@00042@@ टैग उपयोग की सूची बनाएं
- दस्तावेज़ कस्टम प्लगइन्स
- वर्तमान कार्यक्षमता का परीक्षण करें
प्रवास के दौरान
Section titled “प्रवास के दौरान”- सभी
{php}टैग हटा दें - वेरिएबल एक्सेस सिंटैक्स अपडेट करें
- संशोधक उपयोग की जाँच करें
- कैशिंग कॉन्फ़िगरेशन अपडेट करें
- सुरक्षा सेटिंग्स की समीक्षा करें
प्रवास के बाद
Section titled “प्रवास के बाद”- सभी टेम्प्लेट का परीक्षण करें
- सभी प्रपत्रों के कार्य की जाँच करें
- कैशिंग कार्यों को सत्यापित करें
- विभिन्न उपयोगकर्ता भूमिकाओं के साथ परीक्षण करें
अनुकूलता के लिए परीक्षण
Section titled “अनुकूलता के लिए परीक्षण”संस्करण का पता लगाना
Section titled “संस्करण का पता लगाना”// Check Smarty version in PHP$version = Smarty::SMARTY_VERSION;
if (version_compare($version, '4.0.0', '>=')) { // Smarty 4+ specific code} else { // Smarty 3 code}टेम्प्लेट संस्करण जांचें
Section titled “टेम्प्लेट संस्करण जांचें”{* Check version in template *}<{assign var="smarty_major" value=$smarty.version|regex_replace:'/\\..*$/':''}>
<{if $smarty_major >= 4}> {* Smarty 4+ template code *}<{else}> {* Smarty 3 template code *}<{/if}>```## क्रॉस-संगत टेम्पलेट लिखना
### सर्वोत्तम प्रथाएँ
1. **`{php}` टैग से पूरी तरह बचें** - वे Smarty 3+ में काम नहीं करते हैं
2. **टेम्प्लेट को सरल रखें** - जटिल तर्क PHP में आता है
3. **मानक संशोधक का उपयोग करें** - अप्रचलित संशोधकों से बचें
4. **दोनों संस्करणों में परीक्षण करें** - यदि आपको दोनों का समर्थन करने की आवश्यकता है
5. **जटिल परिचालनों के लिए प्लगइन्स का उपयोग करें** - अधिक रखरखाव योग्य
### उदाहरण: क्रॉस-संगत टेम्पलेट
```smarty{* Works in both Smarty 3 and 4 *}<!DOCTYPE html><html><head> <title><{$page_title|default:'Default Title'|escape}></title></head><body> <{if isset($items) && $items|@count > 0}> <ul> <{foreach $items as $item}> <li><{$item.name|escape}></li> <{/foreach}> </ul> <{else}> <p>No items found.</p> <{/if}></body></html>सामान्य प्रवासन मुद्दे
Section titled “सामान्य प्रवासन मुद्दे”समस्या: वेरिएबल्स खाली लौटते हैं
Section titled “समस्या: वेरिएबल्स खाली लौटते हैं”समस्या: <{$mod_url}> Smarty 4 में कुछ भी नहीं लौटाता
समाधान: <{$mod_url->value}> का उपयोग करें या संगतता मोड सक्षम करें
समस्या: PHP टैग त्रुटियाँ
Section titled “समस्या: PHP टैग त्रुटियाँ”समस्या: टेम्प्लेट {php} टैग पर त्रुटि उत्पन्न करता है
समाधान: सभी PHP टैग हटाएं और तर्क को PHP फ़ाइलों में ले जाएं
समस्या: संशोधक नहीं मिला
Section titled “समस्या: संशोधक नहीं मिला”समस्या: कस्टम संशोधक “अज्ञात संशोधक” त्रुटि उत्पन्न करता है
समाधान: संशोधक को registerPlugin() के साथ पंजीकृत करें
मुद्दा: सुरक्षा प्रतिबंध
Section titled “मुद्दा: सुरक्षा प्रतिबंध”समस्या: टेम्पलेट में फ़ंक्शन की अनुमति नहीं है
समाधान: सुरक्षा नीति की अनुमत सूची में फ़ंक्शन जोड़ें
#Smarty #माइग्रेशन #अपग्रेड #xoops #smarty4 #संगतता