Guest User

Untitled

a guest
Jan 22nd, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. module Renderer
  2.  
  3. # The rendering method, mixed in to AbstractController, called at the end of
  4. # controller methods ala: render 'blog/show', :layout => 'layouts/blog'
  5. #
  6. # Can also set the content type and http response code.
  7. #
  8. # #render always just assumes you are rendering the default application template (APP_TEMPLATE)
  9. # set in config/constants.rb. May add an option for this eventually.
  10. #
  11. # #render basically loads up a rack-ready response that is returned to the Dispatcher, via
  12. # the Router, usually.
  13. #
  14. # Rendering is performed by Haml::Engine (set default options in config/gem_settings), which takes
  15. # the template, 'self' as context (allowing it to use controller instance variables during render),
  16. # and blocks of further Haml::Engine action which execute inside template yield statements.
  17. #
  18. # Would be ideal to have some more caching in here (disk IO caching now done). For instance,
  19. # it seems extreme to be re-rendering the application template every time, booting up a new haml object...
  20. def render(template, opts={})
  21. defaults = {
  22. :code => '200',
  23. :type => 'text/html',
  24. :layout => nil,
  25. }
  26.  
  27. @response = {}
  28.  
  29. @response[:code] = opts[:code] || defaults[:code]
  30. @response[:type] = {"Content-Type" => opts[:type] || defaults[:type]}
  31. layout = opts[:layout] || defaults[:layout]
  32.  
  33. app_template = get_template(APP_TEMPLATE)
  34.  
  35. # This conditional is not ideal. Couldn't get it to work in such a way that the 2nd block
  36. # arg can not happen without killing the last. May be some way with a proc or rendering
  37. # the templates un-nested...
  38. #
  39. # Think I could optimise this if I combined the raw strings with
  40. # some block accepting argument, somehow respecting template yields, and then
  41. # processed it all at once.
  42. if layout
  43. @response[:body] = Haml::Engine.new(app_template, HAML_OPTS).render(self) do
  44. Haml::Engine.new(get_template(layout), HAML_OPTS).render(self) do
  45. Haml::Engine.new(get_template(template), HAML_OPTS).render(self)
  46. end
  47. end
  48. else
  49. @response[:body] = Haml::Engine.new(app_template, HAML_OPTS).render(self) do
  50. Haml::Engine.new(get_template(template), HAML_OPTS).render(self)
  51. end
  52. end
  53. @response
  54. end
  55.  
  56. # Renders partials within templates:
  57. # %section
  58. # = partial 'blog/footer'
  59. def partial(file)
  60. template = get_template(file)
  61. Haml::Engine.new(template, HAML_OPTS).render(self)
  62. end
  63.  
  64.  
  65. private
  66.  
  67. # Quick method to read the necessary template file.
  68. # Has some basic mem caching to avoid endless pointless disk IO
  69. #
  70. # Right now the cache is just a hash. It's also possibly a poor tradeoff - they both use
  71. # a fair bit of processors - need to profile properly. But the cached version seems to refuse
  72. # to respond or something strange - perhaps it gets jammed up?
  73. def get_template(template)
  74. if DEVELOPMENT_MODE
  75. File.read(PATH + '/views/' + template + '.haml')
  76. else
  77. if TemplateCache.get(template).nil?
  78. puts 'hard read'
  79. TemplateCache.store template, File.read(PATH + '/views/' + template + '.haml')
  80. end
  81. TemplateCache.get template
  82. end
  83. end
  84.  
  85. end
Add Comment
Please, Sign In to add comment