PHP:重写 CSS/JS 等资源的路径

发布于 2024-10-03 11:11:08 字数 672 浏览 0 评论 0原文

我想知道是否有人知道重写网页资源(例如CSS和JS文件)路径的一些技巧。

原因是;我正在用 PHP 开发一个小型 CMS 项目,我想将不同的站点组件分开,例如模板,例如:

+- /
   +- classes/
      +- template.class.php
      +- datasource.class.php
      +- cache.class.php
   +- templates/
      +- admin.tpl.php
      +- admin.dashboard.tpl.php
   +- resources/
      +- admin.css
      +- admin.js
      +- jquery.js       
   +- index.php
   +- config.php
   +- bootstrap.php

大多数内容都是通过 bootstrap.php 调用的,并且 template.class.php 类负责从 /templates/ 中的文件构建模板。

无论如何,在创建 HTML 模板文件时,我应该只使用 CSS 和其他资源的站点根相对路径吗?或者有没有人熟悉的更直观的重写方法?我考虑过重新调整所有内容,但我很想知道建议的选项。

谢谢 :)

I'm wondering if anyone knows of some tricks for rewriting the paths of web page resources, such as CSS and JS files.

Reason being; I'm working on a little CMS project in PHP, and I want to keep the different site components separated, such as templates, eg:

+- /
   +- classes/
      +- template.class.php
      +- datasource.class.php
      +- cache.class.php
   +- templates/
      +- admin.tpl.php
      +- admin.dashboard.tpl.php
   +- resources/
      +- admin.css
      +- admin.js
      +- jquery.js       
   +- index.php
   +- config.php
   +- bootstrap.php

Most everything is called via bootstrap.php, and the template.class.php class is responsible for building templates out of files from /templates/.

Anyways, should I just be using site-root relative paths for the CSS and other resources when creating the HTML template files? Or is there a more intuitive way of rewriting them that someone is familiar with? I've considered just regexing it all, but I'm curious to know what options are suggested.

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˉ厌 2024-10-10 11:11:08

我建议模板指定它需要哪些 CSS/JS 文件(一些语法,如 use_javascript('jquery'))。您的模板构建器会将适当的 scriptlink 标记注入页面的头部。然后您可以稍后添加其他功能,例如依赖项解析 (use_javascript('jquery-ui', 'jquery'))。

您可以查看 REQUEST_URI 和 FILE/_DIR_ 来了解如何构建资源的相对路径,不过生成绝对路径也可以正常工作。

I'd suggest the template specify what CSS/JS files it needs (some syntax like use_javascript('jquery')). Your template builder will inject the appropriate script and link tags into the head of the page. Then you can add other features like dependency resolution (use_javascript('jquery-ui', 'jquery')) later on.

You can look at the REQUEST_URI and FILE/_DIR_ to figure out how to build a relative path to the resources, though generating an absolute path works fine too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文