Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Archive

Change theme dynamically

Started by Muut Archive 11 years ago · 3 replies · 494 views
11 years ago

Hello,
I'm trying to develop a plugin which change theme if mobile is detect.
I found how to get the current theme in the array with grav['config']->get('system.pages.theme') and how to find all theme grav['themes']->all()
But I don't know how to set/change the current theme to another one.
For your information, my plugin is using Mobile_Detect.php for mobile detection.
Thanks.

11 years ago

A couple of things. Grav already includes https://github.com/donatj/PhpUserAgent and is available to use in your plugins. It has a simple platform detection function. It doesn't actually have a simple way to tell you if you are on a mobile platform or not, although it detects a variety of mobile platforms.

TWIG
{{ browser.platform }}

Anyway, mobile_detect has more features, and may suit you better.

To set the theme, you should simply change the config:

PHP
$grav['config']->set('system.pages.theme', 'mobile_theme')

You should do this before the theme is rendered. So pick an appropriate event:

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1282 9 years ago
Archive · by Muut Archive, 9 years ago
2 889 9 years ago
Archive · by Muut Archive, 9 years ago
2 4019 9 years ago
Archive · by Muut Archive, 9 years ago
1 2894 9 years ago
Archive · by Muut Archive, 9 years ago
3 1078 9 years ago