view.js 978 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. viewPath = Core.Path.View + '/' + src;
  18. try{
  19. FILE.statSync(viewPath);
  20. }catch(e){
  21. LOGGER.error('No such view template ['+src+']');
  22. return null;
  23. }
  24. }
  25. let content = FILE.readFileSync(viewPath,'UTF-8');
  26. if(this.params){
  27. try{
  28. this.html = EJS.render(content,this.params);
  29. }catch(e){
  30. LOGGER.error('View template render error ['+src+']');
  31. LOGGER.error(e);
  32. return null;
  33. }
  34. }else{
  35. this.html = content;
  36. }
  37. }
  38. module.exports = View;