view.js 766 B

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * @Author HonorLee (deve@honorlee.me)
  3. * @Version 1.0 (2018-05-04)
  4. * @License MIT
  5. */
  6. var View = function(src,params){
  7. if(!src) return null;
  8. this.html = '';
  9. this.params = params?params:{};
  10. let viewPath = src;
  11. if(!src.match(Core.Path.View)){
  12. viewPath = Core.Path.View + '/' + src + '.html';
  13. }
  14. try{
  15. FILE.statSync(viewPath);
  16. }catch(e){
  17. LOGGER.error('No such view template ['+src+']');
  18. return null;
  19. }
  20. try{
  21. var data = FILE.readFileSync(viewPath,'UTF-8');
  22. this.html = EJS.render(data,this.params);
  23. }catch(e){
  24. LOGGER.error('View template render error ['+src+']');
  25. LOGGER.error(e);
  26. return null;
  27. }
  28. return this;
  29. }
  30. module.exports = View;