Class: PageTemplate::IncludeCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/PageTemplate/commands.rb

Overview

IncludeCommand allows the designer to include a template from another source.

% include variable %

or [% include literal %]

If literal exists in parser.source, then it is called without passing it to its namespace. If it does not exist, then it is evaluated within the context of its namespace and then passed to parser.source for fetching the body of the file/source.

The body returned by the Source is then passed to Parser for compilation.

Instance Attribute Summary

Attributes inherited from Command

#called_as

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ IncludeCommand

Returns a new instance of IncludeCommand.



474
475
476
# File 'lib/PageTemplate/commands.rb', line 474

def initialize(value)
  @value = value
end

Instance Method Details

#output(namespace) ⇒ Object

If @value exists in parser.source, then it is called without passing it to its namespace. If it does not exist, then it is evaluated within the context of its namespace and then passed to parser.source for fetching the body of the file/source.

The body returned by the Source is then passed to Parser for compilation.



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/PageTemplate/commands.rb', line 487

def output(namespace)
  # We don't use parser.compile because we need to know when something
  # doesn't exist.
  parser = namespace.parser
  fn = @value
  body = parser.source.get(fn)
  unless body
    fn = namespace.get(@value)
    body = parser.source.get(fn) if fn
  end
  if body.is_a?(Command)
    body.output(namespace)
  elsif body
    cmds = parser.parse(body)
	parser.source.cache(fn,cmds)
	cmds.output(namespace)
  else
    "[ Template '#{fn}' not found ]"
  end
end

#to_sObject



477
478
479
# File 'lib/PageTemplate/commands.rb', line 477

def to_s
  "[ Include: #{@value} ]"
end