【问题标题】:How to implement theme/template loading for an html page [closed]如何为 html 页面实现主题/模板加载[关闭]
【发布时间】:2019-04-14 12:39:49
【问题描述】:

我有一个显示博客文章列表的网页。我需要一种方法来为用户可以使用设置页面设​​置的页面设置不同的主题(html 结构和样式)。选择主题后,整个页面结构和样式都会更改为当前主题。我想使用 Nodejs 或 Php 来实现。

【问题讨论】:

    标签: javascript php node.js express templates


    【解决方案1】:

    如果我要尝试做到这一点,下面是我会如何使用 PHP:

    1. 将他们的主题偏好存储在数据库中,声明所做的更改将在他们注销并重新登录后可用。
    2. 每当他们再次登录时,在您编写的任何登录/身份验证函数中将首选项设置为会话变量。
    3. 创建一个 php 函数来检查他们的主题偏好并回显相应的 css 链接。
    4. 将您刚刚创建的函数放在主题应适用的每个 .php 文件的头部(或模板,如果使用 MVC)。

    只是一个简短的例子:

    数据库至少应该有什么

    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>
    

    【讨论】:

    • 像这样也看看每个主题的html标记结构是否不同。
    • 抱歉,您能否详细说明您要检查的其他内容?
    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多