/broke/template/loaders.js

https://github.com/GunioRobot/broke · JavaScript · 128 lines · 99 code · 19 blank · 10 comment · 10 complexity · c2f42472eaad105eb1503bb6bad3de59 MD5 · raw file

  1. (function(_){
  2. var
  3. templatesCache= {}
  4. ,settings= require('broke/conf/settings').settings
  5. ,utils= require('broke/core/utils')
  6. ,TemplateDoesNotExist= require('broke/core/exceptions').TemplateDoesNotExist
  7. ;
  8. _.apps= {
  9. loadTemplate: function(templateName){
  10. var
  11. template
  12. ,len= settings.INSTALLED_APPS.length
  13. ,result
  14. ,app
  15. ;
  16. // loops through all the apps
  17. while(len--) {
  18. app= settings.INSTALLED_APPS[len];
  19. // check if the template exists inside this project's templates
  20. if(utils.typeOf(app) == "object" && 'templates' in app && templateName in app.templates) {
  21. return app.templates[templateName];
  22. }
  23. }
  24. // no template found
  25. return template;
  26. }
  27. };
  28. _.remote= {
  29. loadTemplate: function(templateName){
  30. var
  31. template
  32. ,url
  33. ,templatePath
  34. ,len= settings.TEMPLATE_PATHS.length
  35. ;
  36. while(len--) {
  37. templatePath= settings.TEMPLATE_PATHS;
  38. url= templatePath + '/' + templateName;
  39. if(url in templatesCache) {
  40. return templatesCache[url];
  41. }
  42. $.ajax({
  43. async: false,
  44. url: url,
  45. success: function(responseText){
  46. template= responseText;
  47. // cache the response
  48. templatesCache[url]= template;
  49. },
  50. error: function(error){
  51. // TODO
  52. }
  53. });
  54. }
  55. return template;
  56. }
  57. };
  58. _.filesystem= {
  59. getTemplateSources: function(templateName, templateDirs){
  60. var
  61. i= 0
  62. ,len
  63. ,join= require('path').join
  64. ,paths= []
  65. ;
  66. templateDirs= templateDirs || settings.TEMPLATE_DIRS;
  67. len= templateDirs.length;
  68. for(i= 0; i< len; i++) {
  69. try {
  70. paths.push(join(templateDirs[i], templateName));
  71. } catch(e) {}
  72. }
  73. return paths;
  74. }
  75. ,loadTemplate: function(templateName){
  76. // Returns the absolute paths to "template_name", when appended to each
  77. // directory in "template_dirs". Any paths that don't lie inside one of the
  78. // template dirs are excluded from the result set, for security reasons.
  79. var
  80. tried= []
  81. ,fs= require('fs')
  82. ,paths= _.filesystem.getTemplateSources(templateName)
  83. ,len= paths.length
  84. ,i= 0
  85. ,readFile
  86. ;
  87. for(i= 0; i< len; i++) {
  88. try {
  89. try {
  90. readFile= fs.readFileSync(paths[i], "utf-8");
  91. //return [ readFile, paths[i] ];
  92. return readFile;
  93. } catch(e) {
  94. } finally {
  95. //fs.close(file);
  96. }
  97. } catch(e){
  98. tried.push(paths[i]);
  99. }
  100. }
  101. if(tried.length) {
  102. errorMsg= utils.interpolate("Tried %s", tried);
  103. } else {
  104. errorMsg= "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory.";
  105. }
  106. throw new TemplateDoesNotExist(errorMsg);
  107. }
  108. };
  109. })(exports);