How to include JS/CSS files into templates of Slim Framework?

社会主义新天地 提交于 2020-01-02 07:17:32

问题


I am developing a simple web app with Slim framework. I got stuck with a probably simple problem. I want to include static files (CSS and Javascript) into my template.

My project folder structure is as follows:

index.php //<=== where all the routing happens.
/flot
      layout.css
      jquery.js
      ....
/templates
      first_template.php

My header of first_template.php contains:

<link href="../flot/layout.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript" src="../flot/jquery.js"></script>

When I call the projects root url

http://localhost/xampp/ProjectX/ 

(I'm using XAMPP) the template shows up, but the css and javascript stuff is not working. The Google Chrome console shows:

GET http://localhost/xampp/ProjectX/flot/layout.css 404 (Not Found)
GET http://localhost/xampp/ProjectX/flot/jquery.js 404 (Not Found) 

Any suggestions? I spent almonst one hour in googling, but the overall documentation of the Slim framework is still literally slim :)


回答1:


I assume you are displaying your Template on the Project root (index.php), so you should remove the ../ from your Stylesheet and JS relative Paths:

<link href="flot/layout.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript" src="flot/jquery.js"></script>


来源:https://stackoverflow.com/questions/15600861/how-to-include-js-css-files-into-templates-of-slim-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!