လက်ရှိတည်နေရာ: ပင်မစာမျက်နှာ> နောက်ဆုံးရဆောင်းပါးများစာရင်း> အစပိုင်းတွင် Template Infination ကိုရှာဖွေရန် Template အင်ဂျင်ကိုပေါင်းစပ်ရန် Init function ကိုသုံးပါ

အစပိုင်းတွင် Template Infination ကိုရှာဖွေရန် Template အင်ဂျင်ကိုပေါင်းစပ်ရန် Init function ကိုသုံးပါ

gitbox 2025-05-28

PHP Development တွင် Ther Development သည် Views နှင့် Business Logic ကိုခွဲထုတ်ရန်အရေးကြီးသောကိရိယာတစ်ခုဖြစ်သည်။ မြင်ကွင်းကိုထိရောက်စွာစတင်နိုင်ရန်အတွက် init function တစ်ခု၏ဆင်ခြင်တုံတရားဒီဇိုင်းသည် template engine ၏ configuration နှင့်အသုံးပြုရန်ကိုရိုးရှင်းအောင်ပြုလုပ်ခြင်း,

ဤဆောင်းပါးသည် Init functions များနှင့် template engins များကိုမည်သို့ပေါင်းစပ်ရမည်ကိုရှင်းပြရန်ဥပမာတစ်ခုကိုအသုံးပြုပါမည်။

1 ။ အဘယ်ကြောင့်သင်တစ် ဦး init function ကိုလိုအပ်သနည်း

Template အင်ဂျင်ကိုအသုံးပြုသောအခါများသောအားဖြင့်ကျွန်ုပ်တို့သည်အများအားဖြင့်စတင်လည်ပတ်မှုများပြုလုပ်ရန်လိုအပ်သည်။

  • template ကိုအင်ဂျင်အရာဝတ်ထု;

  • တင်းပလိတ်များနှင့် complile directory ကို set;

  • cache နှင့် debug options များကို configure လုပ်ပါ။

  • ကမ္ဘာလုံးဆိုင်ရာ variable တွေကိုသို့မဟုတ် configuration သတင်းအချက်အလက်အတွက် pass ။

အကယ်. ဤလုပ်ငန်းများကို Controller တစ်ခုစီတွင်ထပ်ခါတလဲလဲပြုလုပ်ပါကသို့မဟုတ် processing function ကိုကြည့်လျှင် code သည်မလိုအပ်သောနှင့်ထိန်းသိမ်းရန်ခက်ခဲလိမ့်မည်။ တစ် ဦး init function ကို uncapsulate unified ထုံးစံ၌ encapsulate နိုင်ပါတယ်။ အနာဂတ်တွင် configured template engine အရာဝတ်ထုကိုလျင်မြန်စွာရယူရန် Init () ဟုခေါ်ဆိုပါ။

2 ။ နမူနာကုဒ်: Init function ကို အသုံးပြု. Smarty Smarty ကိုစတင်ပါ

 <?php
require_once 'libs/Smarty.class.php';

function init() {
    $smarty = new Smarty();

    // template လမ်းညွှန်ကို setting
    $smarty->setTemplateDir('/var/www/gitbox.net/templates/');
    
    // compile directory ကိုသတ်မှတ်ပါ
    $smarty->setCompileDir('/var/www/gitbox.net/templates_c/');
    
    // cache directory ကိုသတ်မှတ်ပါ
    $smarty->setCacheDir('/var/www/gitbox.net/cache/');
    
    // configuration လမ်းညွှန်ကို setting
    $smarty->setConfigDir('/var/www/gitbox.net/configs/');
    
    // Debug Mode ကိုဖွင့်ပါ(ဖွံ့ဖြိုးရေးအဆင့်တွင်ရရှိနိုင်ပါသည်,ထုတ်လုပ်မှုပတ်ဝန်းကျင်ကိုပိတ်ရန်အကြံပြုသည်)
    $smarty->debugging = true;
    
    // cache configuration ကို
    $smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;
    $smarty->cache_lifetime = 120;

    // ကမ္ဘာလုံးဆိုင်ရာ variable တွေကို assign,ဥပမာ, site နာမ
    $smarty->assign('site_name', 'GitBox နမူနာ site ကို');

    return $smarty;
}

// ဥပမာအသုံးပြုမှု
$smarty = init();

// template variable တွေကို assign
$smarty->assign('page_title', 'ဝမ်းသာစွာနှုတ်ဆက်ခြင်း GitBox!');
$smarty->assign('content', 'ဒါကသုံးတယ် init function ကိုစတင်ရန်ဥပမာ template ကို。');

// template ကို rendering
$smarty->display('index.tpl');
?>

3 ။ အဓိကအချက်များဖော်ပြချက်

  • Directory Configuration : Template, စုစည်းခြင်းနှင့် cache directory များသည်စာဖတ်ခြင်းနှင့်ရေးရာခွင့်ပြုချက်များရေးဆွဲထားရမည်ကိုသတိပြုပါ။

  • ကမ္ဘာလုံးဆိုင်ရာ variable များ - Init function တွင်ဖော်ပြထားသော variable များ (ဥပမာ site_name) တွင်ဖော်ပြထားသော variable များ ( site_name ) တွင်ပြောင်းလဲခြင်း,

  • မတူညီသောပတ်ဝန်းကျင် (ဖွံ့ဖြိုးရေး, စမ်းသပ်ခြင်း, ထုတ်လုပ်ခြင်း, ထုတ်လုပ်မှု) အရ Init function တွင်ကွဲပြားခြားနားသော configurations များကိုသင်ထည့်နိုင်သည်

  • စွမ်းဆောင်ရည်တိုးတက်စေရန်အတွက် - Init function ကိုသတ်မှတ်သည်နှင့်အမျှ Controller သည်စီးပွားရေးလုပ်ငန်းများကိုအာရုံစိုက်ရန်လိုအပ်သည်။