view.js 990 B

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