How to ensure the javascript is at the bottom of the code in Jade template?

☆樱花仙子☆ 提交于 2019-12-08 04:13:41

问题


I'm new to Jade template. Here is the layout.jade:

html
  head
    meta(charset='UTF-8')
    title MyApplication
    meta(content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no', name='viewport')
    link(href='/bootstrap/dist/css//bootstrap.min.css', rel='stylesheet', type='text/css')

  body
    block content

script(src='http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js')
script(src='/bootstrap/dist/js/bootstrap.min.js', type='text/javascript')

As you see, there are two javascript at the end of the template file. My another jade file, home.jade is:

extends layout

block content

        // Content Header (Page header)
        section.content-header
          h1= title

        // Main content
        section.content

script(type='text/javascript').
  $(function() {
     alert('here!');
  }

The javascript related to this file is at the end of the file. Unfortunately the block content is embedded inside the layout.jade so that the script here is loaded before the jQuery. The code will never be called. Is there any way to solve this problem to make sure the javascript here is loaded after the jQuery? Thanks


回答1:


You can define more blocks in your layout to fill. You can even define default values for blocks: see http://jade-lang.com/reference/extends/

layout.jade:

html
  head
    meta(charset='UTF-8')
    title MyApplication
    meta(content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no', name='viewport')
    link(href='/bootstrap/dist/css//bootstrap.min.css', rel='stylesheet', type='text/css')

  body
    block content

    script(src='http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js')
    script(src='/bootstrap/dist/js/bootstrap.min.js', type='text/javascript')

    block script

home.jade:

extends layout

block content

        // Content Header (Page header)
        section.content-header
          h1= title

        // Main content
        section.content

block script
    script(type='text/javascript').
      $(function() {
        alert('here!');
      }

This will render your content, js libraries and your script respectively.

In layout.jade, indent script tags inside body.



来源:https://stackoverflow.com/questions/25283821/how-to-ensure-the-javascript-is-at-the-bottom-of-the-code-in-jade-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!