2016-11-05 3 views
0

私はテンプレートエンジンとしてEJSを使用しており、ページレイアウトの最後に1ページだけ必要なスクリプトをどのように取り入れるのか不思議です。EJSはページレイアウトの開始/終了時にスクリプトを含む

例:

レイアウト:

<head> 
    //Scripts and stuff 
</head> 
<body> 
    <article> 
    <%-body%> // Render page here 
    </article> 
</body> 

ページ:

//This script needs to go within the head of the layout 
    //Or before the closing </body> tag in the layout, but only 
    //when this particular page is loaded. 
    <script src="onlyRequiredOnThisPageWithinTheLayout.js"></script> 
    <p>Page content</p> 

ありがとう!

答えて

0

には、記事タグ内にモジュールページが含まれています。レイアウトのheadタグ内の<%- include("page.ejs") %>

は、私が取ったheadタグ

layout.ejs

<head> 
    <script> 
    function clean(){ 
     sc=document.querySelectorAll(".pagescript"); 
     head=document.getElementsByTagName("head"); 

     for(s=0;s<sc.length;s++){ 

      head[0].appendChild(sc[s]); 
     } 
     } 
    document.addEventListener("DOMContentLoaded", clean, false); 

    </script>  
    </head> 
    <body> 
     <article> 
     <%- include("page.ejs") %> // Render page here 
     </article> 
    </body> 

あなたpage.ejs

<script class="pagescript" src="onlyRequiredOnThisPageWithinTheLayout.js"></script> 
    <p>Page content</p> 

<script></script>タグを追加するスクリプトを作成しますスクリプトタグを追加する場合はsc[0]を使用してください。

関連する問題