简体   繁体   English

NETTE框架中如何以及在何处包含CSS和JS文件?

[英]How and where to include CSS and JS file in NETTE framework?

I came across a problem to add external files to template in Nette when I used a traditional way of adding external files. 当我使用传统的添加外部文件的方法时,在Nette中遇到了将外部文件添加到模板的问题。 Either I added those links to incorrect file (template is not the place where they should be added) or the format used is incorrect. 我将这些链接添加到了错误的文件中(模板不是应在其中添加的地方),或者使用的格式不正确。

I tried to implement css and js internally to template (latte) which worked well. 我试图在内部将CSS和js实现为效果很好的模板(latte)。 However I need them to be added externally. 但是我需要在外部添加它们。

{block script}
  <link rel="stylesheet" type="text/css" href="..\sass\components\file.scss">
  <script src="..www\assets\js\file.js"></script>
{/block script}

You should understand that paths on the server's file system are independent from the URLs you will send to the browser in templates and which the browser will use for further HTTP requests. 您应该理解,服务器文件系统上的路径与您将以模板发送给浏览器的URL无关,并且浏览器将使用这些URL进行进一步的HTTP请求。

For example, in the common scenario with Nette applications, files in / will be searched under something /path/to/your/project/directory/www ; 例如,在使用Nette应用程序的常见情况下, /文件将在/path/to/your/project/directory/www some file system paths like everything outside the www directory do not even have a corresponding URL. 有些文件系统路径(如www目录之外的所有路径)甚至都没有相应的URL。

By default, Latte does not know which file matches to what URL so it keeps the src and href attributes exactly as written. 默认情况下,Latte不知道哪个文件与哪个URL匹配,因此它将srchref属性保持与编写时完全相同。 This means you need to make sure you use correct URLs yourself. 这意味着您需要确保自己使用正确的URL。

For your convenience, Nette automagically sets the $basePath variable in Latte templates: 为方便起见,Nette自动在Latte模板中设置$basePath变量:

{block script}

  <link rel="stylesheet" type="text/x-scss" href="{$basePath}/sass/components/file.scss">

  <script src="{$basePath}/assets/js/file.js"></script>

{/block script}

The variable will point to your application public root ( www/ directory), making coming up with the URLs a little easier. 该变量将指向您的应用程序的公共根目录( www/目录),从而使URL的创建更加容易。

Also note that you should use forward slashes in URLs and you will probably want to link to the built stylesheet instead of the SASS components. 还要注意,您应该在URL中使用正斜杠,并且可能要链接到内置样式表而不是SASS组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM