Smarty 4 הגירה
מדריך זה מכסה את השינויים ושלבי ההעברה הדרושים בעת שדרוג מ-Smarty 3 ל-Smarty 4 ב-XOOPS. הבנת ההבדלים הללו חיונית לשמירה על תאימות עם התקנות XOOPS מודרניות.
תיעוד קשור
Section titled “תיעוד קשור”- Smarty-Basics - היסודות של Smarty ב-XOOPS
- פיתוח נושא - יצירת ערכות נושא של XOOPS
- Template-Variables - משתנים זמינים בתבניות
סקירה כללית של שינויים
Section titled “סקירה כללית של שינויים”Smarty 4 הציג מספר שינויים פורצים מ-Smarty 3:
- התנהגות הקצאת משתנה השתנתה
- תגיות
{php}הוסרו לחלוטין - שמירה בcache API שינויים
- טיפול בעדכונים של שינוי
- שינויים במדיניות האבטחה
- תכונות שהוצאו משימוש הוסרו
שינויים בגישה משתנה
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}”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>שינויים בcache
Section titled “שינויים בcache”Smarty 3 cache
Section titled “Smarty 3 cache”// 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 cache
Section titled “Smarty 4 cache”// 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;שמירה בcache
Section titled “שמירה בcache”// Caching modesSmarty::CACHING_OFF // No cachingSmarty::CACHING_LIFETIME_CURRENT // Use cache_lifetimeSmarty::CACHING_LIFETIME_SAVED // Use cached lifetimeNocache בתבניות
Section titled “Nocache בתבניות”{* 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 “לפני ההגירה”- גבה את כל התבניות
- רשום את כל השימוש בתג
{php} - מסמך תוספים מותאמים אישית
- בדוק את הפונקציונליות הנוכחית
במהלך ההגירה
Section titled “במהלך ההגירה”- הסר את כל תגי
{php} - עדכן תחביר גישה משתנה
- בדוק את השימוש במשנה
- עדכן את תצורת הcache
- סקור את הגדרות האבטחה
לאחר ההגירה
Section titled “לאחר ההגירה”- בדוק את כל התבניות
- בדוק שכל הטפסים עובדים
- ודא שהcache עובד
- בדיקה עם תפקידי משתמש שונים
בדיקת תאימות
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}>כתיבת תבניות תואמות צולבות
Section titled “כתיבת תבניות תואמות צולבות”שיטות עבודה מומלצות
Section titled “שיטות עבודה מומלצות”-
הימנע לחלוטין מתגי
{php}- הם לא עובדים ב-Smarty 3+ -
שמור על תבניות פשוטות - היגיון מורכב שייך ל-PHP
-
השתמש בשינויים סטנדרטיים - הימנע משינויים שיצאו משימוש
-
בדוק בשתי הגרסאות - אם אתה צריך לתמוך בשתיהן
-
השתמש בתוספים לפעולות מורכבות - ניתנים לתחזוקה יותר
דוגמה: תבנית תואמת צולבת
Section titled “דוגמה: תבנית תואמת צולבת”{* 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 “בעיה: הגבלת אבטחה”בעיה: הפונקציה אינה מותרת בתבנית
פתרון: הוסף פונקציה לרשימה המותרת של מדיניות האבטחה
#חכם #הגירה #שדרוג #xoops #smarty4 #תאימות