2

I am using Middleman for a project and would like to only load certain JS files on certain pages. How can I configure the asset pipeline to only load specific JS files? I've found answers for the asset pipeline in rails applications (ex. http://blog.seancarpenter.net/2012/11/05/page-specific-javascript-with-the-asset-pipeline/) but I'm not sure how to apply this Middleman.

Thanks!

1 Answer 1

2
//= require_tree ./general
//= require_tree ./ckeditor
//= require_tree ./custom

In application.js file include the path of those js files which you want to load by default for all the applications. In the above code ./general will include all the js files under app/assets/javascripts/general/ and so on.

If you want certain js file to be loaded in your pre-defined view page then you need to call it explicitly from that view page as:

<% content_for :scripts do %>
  <%= javascript_include_tag 'your_custom_js_file_name' %>
<% end %>

The file location of your_custom_js_file_name.js file is app/assets/javascript

We need to yield script in the layout file so, include this:

<%= yield :scripts %>

Besides you need to specify to compile in for production environment. So in production.rb do this:

 config.assets.precompile += [ "your_custom_js_file_name.js", "your_custom_js_file_name2.js"]

Credit goes to: site

Sign up to request clarification or add additional context in comments.

1 Comment

I had to use <%= yield_content :scripts %>. source: middlemanapp.com/basics/helper-methods/#output-helpers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.