简体   繁体   English

如何使用slim来引用javascript文件

[英]How can I reference a javascript file with slim

I understand how to embed javascript using javascript: in a slim template. 我理解如何使用javascript嵌入javascript:在一个苗条的模板中。 Is there a way for me to reference a javascript file. 有没有办法让我引用一个javascript文件。

get '/about' do
  @title = "All About This Website"
  slim :about
end

here is about.slim 这是about.slim

p This site is a demonstration of how to build a website using Sinatra.
javascript:
  alert("hello world")

that works. 这样可行。 Can I reference a javascript file instead of the javascript statment/s? 我可以引用javascript文件而不是javascript statment / s吗? like this: 像这样:

p This site is a demonstration of how to build a website using Sinatra.
javascript:
  about.js

I've used script tag to refer the file within the slim template and it works for me. 我使用了脚本标签来引用苗条模板中的文件,它对我有用。

script src="jsfile.js"

The only thing we need to consider is, by default sinatra looks for static files in public folder , so keep the js file in the public folder. 我们唯一需要考虑的是,默认情况下sinatra会在公共文件夹中查找静态文件 ,因此请将js文件保存在公用文件夹中。 Try to access your js file from your browser. 尝试从浏览器访问您的js文件。 All files available under public directory should be accessible (without public in url). 公共目录下可用的所有文件都应该是可访问的(在url中没有public)。

yourhostedsite:port/jsfile.js

Your sinatra project structure might look like 您的sinatra项目结构可能看起来像

Project root
    |___ controller.rb
    |___ public
    |       |___ jsFile.js
    |       |___ cssFile.css
    |
    |___ views
            |___ html.slim

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

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