2017-05-25 71 views
0

我創建了一個迷你CMS來存儲代碼片段。 它從mongodb數據庫中提取文章。 我使用highlgiht.js來顯示代碼並允許用戶複製代碼片段。使用把手輸出原始javascript

隨着HTML,一切正常:

{{#article}} 
    <h1>Demo</h1> 
    {{{markup}}} <!-- prints raw HTML in the screen, yay! --> 

    <h1>HTML</h1> 
    <pre class="pre-scrollable"><code class="html">{{markup}}</code></pre> 
    <!-- prints escaped HTML in the screen, and highlight.js highlights it perfectly! YAY --> 

{{/article}} 

現在,用javascript:

{{#article}} 

    <h1>Demo</h1> 
    {{{js}}} <!-- prints empty script tag: <script></script> --> 

    <h1>JS</h1> 
    <pre class="pre-scrollable"><code class="html">{{js}}</code></pre> 
    <!-- prints <script></script> --> 

{{/article}} 

在數據庫中的字段 「JS」 的內容是 「執行console.log(」 測試「);」

爲什麼它不輸出保存在數據庫中的內容,而是它自己組成一個新的腳本標記?

謝謝!

+0

把手不會改變'console.log'腳本,所以'js'值實際上是''或其他的東西正在改變那個值。顯示如何編譯句柄並在將句柄傳遞給handlebars時放置一個'console.log',並向我們顯示輸出。 –

+0

@MarcosCasagrande:當我從我的文章控制器做一個console.log時,我得到原始的javascript作爲文檔字段的實際內容。 下面是我發現的其他東西: - 它不是瀏覽器,它發生在所有的瀏覽器中; - 它不是highlight.js,因爲它按預期工作: '''

$(document).ready(function() {$('pre code').each(function(i, e) {hljs.highlightBlock(e)});});
''' 這是帶把手的東西。 – rsilva

+0

另外,我在我的應用程序中使用了這個handlebars包裝:https://github.com/ericf/express-handlebars – rsilva

回答