在使用布局文件的Middleman中,如何在编译期间为特定的html文件追加js文件?

时间:2014-08-14 19:49:52

标签: haml erb middleman

我正在使用Middleman,我有一个布局文件,它将jQuery CDN添加到所有.html.haml文件的底部。布局文件在渲染完所有html元素后添加jQuery CDN。

我的一个.html.haml文件将有一个使用jQuery的附加js文件;但是,它需要首先加载jQuery CDN。

如何告诉Middleman在编译期间,为这个特定的.html.haml文件附加js文件?我尝试在= javascript_include_tag "jsfile"文件的底部添加.html.haml,但它出错了,因为它需要首先加载jQuery,直到我到达布局文件的末尾才会发生。

布局文件:

!!! 5
%html{lang: 'en'}
  %head
  %body{class: current_page.data.body_class || 'page'}
    = yield
    // jQuery
    = javascript_include_tag  "//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"
    //I need to insert my js file here, but only for a specific HTML file.

P.S。另外,我需要将jQuery放在文件的底部以提高渲染效率,因此我无法将其放在head中。

1 个答案:

答案 0 :(得分:2)

在弄清楚对我有用之后,在这里提供我的答案:

首先,我必须通过index_options文件中的.html.haml设置一个标记:

---
title: My Site
index_options: target_file
---

然后,我不得不用if语句

更新布局文件
!!! 5
%html{lang: 'en'}
  %head
  %body{class: current_page.data.body_class || 'page'}
    = yield
    // jQuery
    = javascript_include_tag  "//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"
    //Here's the added update:
    = javascript_include_tag "my_jsFile" if (current_page.data.index_options == "target_file")
相关问题