How can I reference a javascript file with slim

10,109

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
Share:
10,109

Related videos on Youtube

nilanjan
Author by

nilanjan

Updated on September 15, 2022

Comments

  • nilanjan
    nilanjan almost 2 years

    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
    
    • Blender
      Blender over 10 years
      Just include a script tag: script src="about.js"