Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 如何让{{> partials}使用Consolidate.js和Mustache?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当与consolidate.js一起使用时,我无法读取像{{> my_partial}}这样添加的部分内容.我用以下内容开始我的快速申请:

文件index.js:

var express = require('express'),cons    = require('consolidate'); 

app.configure(function(){

  app.set( 'models',BACKEND + '/models' );       // Set the models directory
  app.set( 'views',BACKEND + '/views' );        // Set the views directory
  app.set( 'controllers',BACKEND + '/controllers' );  // Set the controllers dir


  app.set( 'view ENGIne','html' );                // Set Mustache as the templaTing ENGIne
  app.ENGIne( 'html',cons.mustache );
  app.ENGIne( 'mustache',cons.mustache );

  app.use( app.router );  // Set the router
  app.use( '/public',express.static( BASEPATH + '/frontend/public' ) );  // set frontend/public to /public for users
  app.use( '/js',express.static( BASEPATH + '/frontend/js' ) );
  app.use( '/out',express.static( BASEPATH + '/out' ) ); // documentation path

  // Set client side libraries here (Make them available to the public based on setTings.json)
  for(setTing in SETTinGs.publiC) {
    app.use( SETTinGs.public[setTing],express.static(BASEPATH + SETTinGs.public[setTing]) );
  }

});

然后,在控制器的某个地方,我像这样使用渲染:

function Blog(app,request,responsE){

  var fs      =  require('fs'),Blog    =  require( app.get('models') + '/blog' ),model   =  new Blog(),scripts =  fs.readFileSync( VIEWS + '/m_scripts.mustache').toString()     ;

  model.List(function(data){

    response.render('layout',{
        title     :  'My Blog',body      :  'Hello Mustache World',list      :  data.rows,cache     :  false,partials  : {
          m_scripts : partialStr
        }
      }
    );

  });

};


exports.list = Blog;
@H_665_5@m_scripts.mustache具有:

<script data-src="ergierjgoejoij"></script>

布局模板渲染得很好,并且params也很好地通过,部分m_scripts与readFileSync()传递的文本一起传递.toString()就好了但HTML内容被编码并呈现无用.

我的问题是,有没有一种方法可以放入layout.mustache {{> m_scripts}},并且胡须理解自动加载m_scripts.mustache而无需将其传递给render().如果没有,我错过了什么?

解决方法

事实证明,此时,consolidate.js不支持partials.一个拉动请求来解决这个问题,但尚未合并: https://github.com/simov/consolidate.js

我结束了使用那个分叉代替:

1)npm卸载合并

2)npm install simov / consolidate.js

npm install simov / consolidate.js将安装该分叉版本.然后我可以做以下事情:

/**
 * @module Blog Controller
 */ 
function Blog(app,responsE){

  var Blog    =  require( app.get('models') + '/blog' ),model   =  new Blog();

  model.List(function(data){

    response.render('layout',partials  : {
          m_scripts : './BACkend/views/m_scripts.html'
        }
      }
    );

  });

};


exports.list = Blog;

它比我想象的要简单得多.您将路径传递给部分,然后您可以在layout.html上使用{{> m_scripts}}

不过,不确定是否有办法制作它,认情况下,在views文件夹或其他东西上查找{{> m_scripts}}.那会很整洁.

大佬总结

以上是大佬教程为你收集整理的node.js – 如何让{{> partials}使用Consolidate.js和Mustache?全部内容,希望文章能够帮你解决node.js – 如何让{{> partials}使用Consolidate.js和Mustache?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。