📜  Yii-主题

📅  最后修改于: 2020-10-16 07:25:50             🧑  作者: Mango


主题化可帮助您用另一视图替换一组视图,而无需修改原始视图文件。您应该将视图应用程序组件的theme属性设置为使用主题。

您还应该定义以下属性-

  • yii \ base \ Theme :: $ basePath-定义CSS,JS,图像等的基本目录。

  • yii \ base \ Theme :: $ baseUrl-定义主题资源的基本URL。

  • yii \ base \ Theme :: $ pathMap-定义替换规则。

例如,如果您在UserController中调用$ this-> render(’create’) ,则将呈现@ app / views / user / create.php视图文件。但是,如果像下面的应用程序配置中那样启用主题设置,则将呈现视图文件@ app / themes / basic / user / create.php。

步骤1-以这种方式修改config / web.php文件。

 'basic',
      'basePath' => dirname(__DIR__),
      'bootstrap' => ['log'],
      'components' => [
         'request' => [
            // !!! insert a secret key in the following (if it is empty) - this
               //is required by cookie validation
            'cookieValidationKey' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO',
         ],
         'cache' => [
            'class' => 'yii\caching\FileCache',
         ],
         'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
         ],
         'errorHandler' => [
            'errorAction' => 'site/error',
         ],
         'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
         ],
         'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
               [
                  'class' => 'yii\log\FileTarget',
                  'levels' => ['error', 'warning'],
               ],
            ],
         ],
         'view' => [
            'theme' => [
               'basePath' => '@app/themes/basic',
               'baseUrl' => '@web/themes/basic',
               'pathMap' => [
                  '@app/views' => '@app/themes/basic',
               ],
            ],
         ],
         'db' => require(__DIR__ . '/db.php'),
      ],
      'modules' => [
         'hello' => [
            'class' => 'app\modules\hello\Hello',
         ],
      ],
      'params' => $params,
   ];
   if (YII_ENV_DEV) {
      // configuration adjustments for 'dev' environment
      $config['bootstrap'][] = 'debug';
      $config['modules']['debug'] = [
         'class' => 'yii\debug\Module',
      ];
      $config['bootstrap'][] = 'gii';
      $config['modules']['gii'] = [
         'class' => 'yii\gii\Module',
      ];
   }
   return $config;
?>

我们添加了视图应用程序组件。

步骤2-现在创建web / themes / basic目录结构和theme / basic / site 。在主题/基本/站点文件夹中,使用以下代码创建一个名为about.php的文件。

title = 'About';
   $this->params['breadcrumbs'][] = $this->title;
   $this->registerMetaTag(['name' => 'keywords', 'content' => 'yii, developing,
      views, meta, tags']);
   $this->registerMetaTag(['name' => 'description', 'content' => 'This is the
      description of this page!'], 'description');
?>

= Html::encode($this->title) ?>

This is the About page. You may modify the following file to customize its content:

步骤3-现在,转到http:// localhost:8080 / index.php?r = site / about ,将呈现themes / basic / site / about.php文件,而不是views / site / about.php文件

创建主题

步骤4-对于主题模块,以这种方式配置yii \ base \ Theme :: $ pathMap属性。

'pathMap' => [
   '@app/views' => '@app/themes/basic',
   '@app/modules' => '@app/themes/basic/modules',
],

步骤5-对于主题小部件,以这种方式配置yii \ base \ Theme :: $ pathMap属性。

'pathMap' => [
   '@app/views' => '@app/themes/basic',
   '@app/widgets' => '@app/themes/basic/widgets', // 

有时您需要指定一个基本主题,其中包含应用程序的基本外观。要实现此目标,可以使用主题继承。

步骤6-以这种方式修改视图应用程序组件。

'view' => [
   'theme' => [
      'basePath' => '@app/themes/basic',
      'baseUrl' => '@web/themes/basic',
      'pathMap' => [
         '@app/views' => [
            '@app/themes/christmas',
            '@app/themes/basic',
         ],
      ]
   ],
],

在上述配置中, @ app / views / site / index.php视图文件的主题将为@ app / themes / christmas / site / index.php或@ app / themes / basic / site / index.php,具体取决于在哪个文件上。如果两个文件都存在,则将使用第一个文件。

步骤7-创建themes / christmas / site目录结构。

步骤8-现在,在themes / christmas / site文件夹内,使用以下代码创建一个名为about.php的文件。

title = 'About';
   $this->params['breadcrumbs'][] = $this->title;
   $this->registerMetaTag(['name' => 'keywords', 'content' => 'yii, developing,
      views, meta, tags']);
   $this->registerMetaTag(['name' => 'description', 'content' => 'This is the
      description of this page!'], 'description');
?>

Christmas theme

This is the About page. You may modify the following file to customize its content:

步骤9-如果您访问http:// localhost:8080 / index.php?r = site / about ,您将看到使用圣诞节主题的更新后的关于页面。

圣诞主题