Class: Trellis::Renderer
- Includes:
- Radius
- Defined in:
- lib/trellis/trellis.rb
Overview
– Renderer – Responsible for processing tags/components in the page templates Uses the Radius context object onto which components registered themselves (the tags that they respond to)
Constant Summary collapse
- SKIP_METHODS =
['before_load', 'after_load', 'before_render', 'after_render', 'get']
- INCLUDE_METHODS =
['render_partial']
Instance Method Summary collapse
-
#initialize(page) ⇒ Renderer
constructor
A new instance of Renderer.
- #render ⇒ Object
- #render_partial(name, locals = {}) ⇒ Object
Constructor Details
#initialize(page) ⇒ Renderer
Returns a new instance of Renderer.
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 |
# File 'lib/trellis/trellis.rb', line 875 def initialize(page) @page = page @context = Context.new # context for erubis templates @eruby_context = Erubis::Context.new #if @page.class.format == :eruby # add all instance variables in the page as values accesible from the tags page.instance_variables.each do |var| value = page.instance_variable_get(var) unless value.kind_of?(Trellis::Page) sym = "#{var}=".split('@').last.to_sym @context.globals.send(sym, value) @eruby_context["#{var}".split('@').last] = value #if @eruby_context end end # add other useful values to the tag context @context.globals.send(:page_name=, page.class.to_s) @eruby_context[:page_name] = page.class.to_s #if @eruby_context # add public page methods to the context page.public_methods(false).each do |method_name| # skip event handlers and the 'get' method unless method_name.starts_with?('on_') || SKIP_METHODS.include?(method_name) @eruby_context.(method_name) do |*args| page.send(method_name.to_sym, *args) end #if @eruby_context @context.globals.(method_name) do |*args| page.send(method_name.to_sym, *args) end end end # add page helper methods to the context INCLUDE_METHODS.each do |method_name| @eruby_context.(method_name) do |*args| page.send(method_name.to_sym, *args) end #if @eruby_context @context.globals.(method_name) do |*args| page.send(method_name.to_sym, *args) end end # add public application methods to the context page.application.public_methods(false).each do |method_name| @eruby_context.(method_name) do |*args| page.application.send(method_name.to_sym, *args) end #if @eruby_context @context.globals.(method_name) do |*args| page.application.send(method_name.to_sym, *args) end end # add the page to the context too @context.globals.page = page @eruby_context[:page] = page #if @eruby_context # register the components contained in the page with the renderer's context page.class.components.each do |component| component.register_with_tag_context(@context) end @parser = Parser.new(@context, :tag_prefix => 'trellis') end |
Instance Method Details
#render ⇒ Object
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 |
# File 'lib/trellis/trellis.rb', line 940 def render preprocessed = "" layout_id = @page.class.layout template = layout_id ? @page.class.to_xml(:no_declaration => true) : @page.class.to_xml if layout_id # page has a layout # retrieve the layout from the application layout = Application.layouts[layout_id] # render the page template to a variable if @page.class.format == :eruby body = Erubis::PI::Eruby.new(template, :trim => false).evaluate(@eruby_context) @eruby_context[:body] = body else @eruby_context[:body] = template end # render the layout around the page template preprocessed = Erubis::PI::Eruby.new(layout.to_xml, :trim => false).evaluate(@eruby_context) # clean up nokogiri namespace hack, see Page#template doc = Nokogiri::XML(preprocessed) to_be_removed = doc.at_css(%[div[id="trellis_remove"]]) parent = to_be_removed.parent to_be_removed.children.each { |child| child.parent = parent } to_be_removed.remove preprocessed = doc.to_xml else # page has no layout if @page.class.format == :eruby preprocessed = Erubis::PI::Eruby.new(template, :trim => false).evaluate(@eruby_context) else preprocessed = template end end # radius parsing @parser.parse(preprocessed) end |
#render_partial(name, locals = {}) ⇒ Object
979 980 981 982 983 984 985 986 987 988 989 990 |
# File 'lib/trellis/trellis.rb', line 979 def render_partial(name, locals={}) partial = Application.partials[name] if partial if partial.format == :eruby locals.each_pair { |n,v| @eruby_context[n] = v } preprocessed = Erubis::PI::Eruby.new(partial.to_xml, :trim => false).evaluate(@eruby_context) @parser.parse(preprocessed) else @parser.parse(partial.to_xml) end end end |