Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reference a javascript file with slim

I understand how to embed javascript using javascript: in a slim template. Is there a way for me to reference a javascript file.

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

here is 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? like this:

p This site is a demonstration of how to build a website using Sinatra.
javascript:
  about.js
like image 632
nilanjan Avatar asked Mar 21 '23 03:03

nilanjan


1 Answers

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. Try to access your js file from your browser. All files available under public directory should be accessible (without public in url).

yourhostedsite:port/jsfile.js

Your sinatra project structure might look like

Project root
    |___ controller.rb
    |___ public
    |       |___ jsFile.js
    |       |___ cssFile.css
    |
    |___ views
            |___ html.slim
like image 51
Dineshkumar Avatar answered Apr 26 '23 03:04

Dineshkumar