网页公用代码

一般指网站公用的头部和尾部,可以命名为:_layout.html。

做法:头尾部的代码提取出来,然后用标签:

  1. #define layout()
  2. ...
  3. #@content()
  4. ...
  5. #end

其他页面引用头尾部代码时,用标签:

  1. #include("./_layout.html")
  2. #define content()
  3. ...
  4. #end

公用代码示例代码如下:

  1. #define layout()
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  7. <meta name="description" content="#(SEO_DESCRIPTION)">
  8. <meta name="keywords" content="#(SEO_KEYWORDS)">
  9. <title>#(SEO_TITLE ?? WEB_TITLE +'-'+ WEB_SUBTITLE)</title>
  10. </head>
  11. <body>
  12. <nav class="">
  13. <div class="container">
  14. <div class="navbar-left">
  15. <button class="navbar-toggler" type="button">&#9776;</button>
  16. <a class="navbar-brand" href="#(CPATH)/">
  17. <img class="logo-dark" src="#option('jpress_logo_light','images/logo.png')" alt="logo">
  18. <img class="logo-light" src="#option('jpress_logo_dark','images/logo-inverse.png')" alt="logo">
  19. </a>
  20. </div>
  21. <section class="navbar-mobile">
  22. <ul class="nav-navbar nav ml-auto">
  23. #for(menu : MENUS)
  24. <li class="nav-item">
  25. #if(menu.hasChild())
  26. <a class="nav-link #(menu.isActive ? 'active' : '')">
  27. #(menu.text ??)
  28. <i class="fa fa-caret-down"></i>
  29. </a>
  30. #else
  31. <a class="nav-link #(menu.isActive ? 'active' : '')" href="#(menu.url ??)">
  32. #(menu.text ??)
  33. </a>
  34. #end
  35. #if(menu.hasChild())
  36. <nav class="nav">
  37. #for(childMenu : menu.getChilds())
  38. <a class="nav-link" href="#(childMenu.url ??)">#(childMenu.text ??)</a>
  39. #end
  40. </nav>
  41. #end
  42. </li>
  43. #end
  44. </ul>
  45. <div class="nav-download d-inline-flex">
  46. #if(USER)
  47. <div class="dropdown">
  48. <span class="dropdown-toggle no-caret" data-toggle="dropdown">
  49. <img class="avatar avatar-xs" src="#(USER.avatar ?? 'images/jp-portrait.jpg')" alt="user">
  50. </span>
  51. <div class="dropdown-menu">
  52. <a class="dropdown-item" href="#(CPATH)/ucenter/article/write">我要投稿</a>
  53. <a class="dropdown-item" href="#(CPATH)/ucenter/info">我的信息</a>
  54. <a class="dropdown-item" href="#(CPATH)/ucenter/avatar">修改头像</a>
  55. <div class="dropdown-divider"></div>
  56. <a class="dropdown-item" href="#(CPATH)/ucenter/doLogout?csrf_token=#(CSRF_TOKEN)">退出登录</a>
  57. </div>
  58. </div>
  59. #else
  60. <a class="nav-link" href="#(CPATH)/user/login">登录</a>
  61. #end
  62. <a class="btn" href="/download">下载源码</a>
  63. </div>
  64. </section>
  65. </div>
  66. </nav>
  67. #@content()
  68. <footer class="footer">
  69. ...
  70. </footer>
  71. <!-- Scripts -->
  72. <script src="js/main.js"></script>
  73. #@script?()
  74. </body>
  75. </html>
  76. #end
  1. 备注:
  2. #(SEO_DESCRIPTION) —— 网站的SEO描述
  3. #(SEO_KEYWORDS) —— 网站的SEO关键字
  4. #(SEO_TITLE ?? WEB_TITLE +'-'+ WEB_SUBTITLE) —— 网站的标题调用后台填写SEO的标题,如果没有填写,则使用 网页标题-网页描述 的方式。
  5. #(menu.text ??) —— 菜单名称
  6. #(childMenu.text ??) —— 子菜单名称
  7. #(menu.url ??) —— 菜单链接
  8. #(childMenu.url ??) —— 子菜单链接
  9. #(CPATH)/ucenter/doLogout?csrf_token=#(CSRF_TOKEN) —— 退出登录
  10. #(CPATH)/user/login —— 登录