symfony实现多主题界面
现在web程序为了实现多个主题,或者模版,skin,方便用户定制或者选择自己的个性界面,采用的方法很多。这里symfony可以这样方便的实现这样的功能。
首先做出不同的主题,如下目录结构:
mymodule/
templates/
indexSuccess.php # Template for index action, default theme
theme1/
indexSuccess.php # Template for index action, theme1
theme2/
indexSuccess.php # Template for index action, theme2
在lib/目录下新建一个myView.class.php:
<?php
class myView extends sfPHPView
{
public function configure()
{
parent::configure();
// Grab the theme from the user (of from anywhere else)
$theme = $this->getContext()->getUser()->getTheme();
// If there is a theme and if the theme feature is enabled
if($theme && sfConfig::get('app_theme'))
{
// Look for templates in a $theme/ subdirectory of the usual template location
if (is_readable($this->getDirectory().'/'.$theme.'/'.$this->getTemplate()))
{
$this->setDirectory($this->getDirectory().'/'.$theme);
}
// Look for a layout in a $theme/ subdirectory of the usual layout location
if (is_readable($this->getDecoratorDirectory().'/'.$theme.'/'.$this->getDecoratorTemplate()))
{
$this->setDecoratorDirectory($this->getDecoratorDirectory().'/'.$theme);
}
}
}
}
在应用的config目录里,新建一个module.yml
all:
view_class: my
在你的app.yml里设置应用允许使用多主题:
all:
theme: on
清除一下cache,你的主题功能就做好了。
如果有一个foobar名字的主题,同时有这样一个访问请求的话mymodule/myaction,symfony会查找
apps/myapp/modules/mymodule/templates/foobar/myActionSuccess.php
调用的layout为:
apps/myapp/templates/foobar/layout.php
如果theme不存在的话,symfony会加载默认的主题。
搜索更多相关主题的帖子:
symfony 界面 web php 主题