我有一个显示博客文章列表的网页。我需要一种方法,使该页面具有不同的主题(html结构和样式),用户可以使用设置页面进行设置。选择主题后,整个页面结构以及样式都会更改为当前主题。我想使用Nodejs或Php来实现。
答案 0 :(得分:1)
如果我要尝试完成此任务,请使用PHP:
只是一个简短的例子:
数据库至少应具备的条件
mysql> select * from _users;
+----+-------+-------+--------+
| id | _user | _pass | _theme |
+----+-------+-------+--------+
| 1 | john | ***** | dkblue |
| 2 | jane | ***** | redgld |
| 3 | bill | ***** | blkwht |
+----+-------+-------+--------+
主题首选项作为会话变量
function authenticate($username, $password) {
# authenticate
//
// code to authenticate...
# retrieve user's theme (can also be a separate function)
$stmt = $sql->prepare("SELECT _theme FROM _users WHERE _user = '$username'");
$stmt->execute();
$results = $stmt->fetch(PDO::FETCH_ASSOC);
# establish session variables
$_SESSION['theme'] = $results['_theme'];
//
// other session variable code...
}
用于检查主题的功能
function checkTheme($theme) {
# remember to place function between <head> tags
switch ($theme) {
case 'dkblue':
$stylesheet = '/inc/css/darkblue.css';
break;
case 'redgld':
$stylesheet = '/inc/css/red-gold.css';
break;
case 'blkwht':
$stylesheet = '/inc/css/black-white.css';
break;
default:
# will default to this theme if no preference has been chosen
$stylesheet = '/inc/css/default.css';
break;
}
echo '<link rel="stylesheet" href="' . $stylesheet . '">';
}
blog.php
<?=session_start();?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Blog</title>
<?=checkTheme($_SESSION['theme']);?>
</head>