我为我的网站为 cecutients(视力低下的人)制作了附加主题。我可以按主页上的某个按钮动态更改站点主题吗?
是的,你可以这样做。我建议您在控制器上执行一个操作来更新主题。然后,您可以将当前活动的主题存储在会话中,并在访问页面时使用它。
这是我将如何实现(在您的Page_Controller)中:
class Page_Controller extends ContentController
{
private static $allowed_actions = ['changeTheme'];
public function init(){
parent::init();
if ($theme = Session::get('theme')) {
Config::inst()->update('SSViewer', 'theme', $theme);
}
}
public function changeTheme()
{
$theme = $this->request->param('ID');
$existingThemes = SiteConfig::current_site_config()->getAvailableThemes();
if (in_array($theme, $existingThemes)) {
// Set the theme in the config
Config::inst()->update('SSViewer', 'theme', $theme);
// Persist the theme to the session
Session::set('theme', $theme);
}
// redirect back to where we came from
return $this->redirectBack();
}
}
Run Code Online (Sandbox Code Playgroud)
现在您的 中有一个changeTheme操作Page_Controller,这意味着您可以在每个页面上使用它。然后您可以简单地通过链接触发主题更改,例如:
<%-- replace otherTheme with the folder-name of your theme --%>
<a href="$Link('changeTheme')/otherTheme">Change to other theme</a>
Run Code Online (Sandbox Code Playgroud)
在Page.ss您的基本主题模板中,您可以为 cecutients 添加一个指向主题的链接。在 cecutients 的主题中,您添加一个指向 base-theme 的链接。