Module: Crystal::Template
- Defined in:
- lib/crystal/template/template.rb
Constant Summary collapse
- DIRECTORY_NAME =
"views"
Class Method Summary collapse
- .basic_render(options, &block) ⇒ Object
- .exist?(tname, options = {}) ⇒ Boolean
- .parse_arguments(*args) ⇒ Object
- .read(tname, options = {}) ⇒ Object
- .render(*args, &block) ⇒ Object
-
.render_layout(*args) ⇒ Object
def render_and_return_context *args, &block basic_render(*parse_arguments(*args), &block) end.
- .template_name_with_prefix(tname, prefix) ⇒ Object
Class Method Details
.basic_render(options, &block) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/crystal/template/template.rb', line 70 def basic_render , &block = .clone.to_openobject with_context do |context| context.content_block ||= block with_scope , context do |scope| unless file = .file file = find_template .template!, calculate_prefixes(), scope.format, scope.current_dir # p [tname, calculate_prefixes(options), scope.format, scope.current_dir] raise "No template '#{.template!}'!" unless file end scope.current_dir = dirname(file) template = create_tilt_template file result = with_template context, template do render_template template, , &context.content_block end return result, context end end end |
.exist?(tname, options = {}) ⇒ Boolean
40 41 42 43 |
# File 'lib/crystal/template/template.rb', line 40 def exist? tname, = {} = .to_openobject !!find_template(tname, calculate_prefixes(), .format, .current_dir) end |
.parse_arguments(*args) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/crystal/template/template.rb', line 59 def parse_arguments *args = args..to_openobject if args.size == 1 .template = args.first else raise "Invalid input" if args.size != 0 end end |
.read(tname, options = {}) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/crystal/template/template.rb', line 45 def read tname, = {} = .to_openobject file = find_template tname, calculate_prefixes(), .format, .current_dir raise "No template '#{tname}'!" unless file File.read file end |
.render(*args, &block) ⇒ Object
12 13 14 15 |
# File 'lib/crystal/template/template.rb', line 12 def render *args, &block result, context = basic_render(parse_arguments(*args), &block) result end |
.render_layout(*args) ⇒ Object
def render_and_return_context *args, &block
basic_render(*parse_arguments(*args), &block)
end
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/crystal/template/template.rb', line 21 def render_layout *args = parse_arguments(*args) = .stringify_keys! .must.include :content .must.include :context result, context = basic_render do |*args| if args.empty? .content else args.size.must_be == 1 variable_name = args.first.to_s self.context.content_variables[variable_name] end end result end |
.template_name_with_prefix(tname, prefix) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/crystal/template/template.rb', line 52 def template_name_with_prefix tname, prefix index = tname.rindex('/') index = index ? index + 1 : 0 tname = tname.clone tname.insert index, prefix end |