2015-10-10 85 views
5

如何在yii2 assetmanager中使用Yii::$app->session['somename']在assetmanager中使用自定義函數

如何在assetmanager中訪問某些函數?

class AppAsset extends AssetBundle{ 
public function getLang() { 
    $currentLang = Yii::$app->session['lang']; 
    if ($currentLang == 'fa' || $currentLang == 'ar') { 
     return 'RTL'; 
    } else { 
     return 'LTR'; 
    } 
} 
public $lang; 

public $basePath = '@webroot'; 
public $baseUrl = '@web'; 
public $css = [ 
    'css/iconSprite.min.css', 
    // how call getLang here 
] 

如何在css中調用getLang

回答

1

你可以像下面這樣做

.. other functions 

public function init() { 
    $this->setupAssets(); 
    parent::init(); 
} 

protected function setupAssets() { 
    $lang = $this->getLang(); 
    $this->css[] = "css/myfile.$lang.css"; 
} 
+0

這就是我在尋找!完善!!! – mohsen