Method: Zafu::Parser#include_template

Defined in:
lib/zafu/parser.rb

#include_templateObject



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/zafu/parser.rb', line 325

def include_template
  return parser_error("missing 'template' attribute") unless @params[:template]
  if @options[:part] && @options[:part] == @params[:part]
    # fetching only a part, do not open this element (same as original caller) as it is useless and will make us loop the loop.
    @method = 'ignore'
    enter(:void)
    return
  end
  @method = 'void'

  # fetch text
  @options[:included_history] ||= []

  included_text, absolute_url, base_path = self.class.get_template_text(@params[:template], @options[:helper], @options[:base_path])

  if absolute_url
    absolute_url += "::#{@params[:part].gsub('/','_')}" if @params[:part]
    absolute_url += "??#{@options[:part].gsub('/','_')}" if @options[:part]
    if @options[:included_history].include?(absolute_url)
      included_text = parser_error("infinity loop: #{(@options[:included_history] + [absolute_url]).join(' --> ')}", 'include')
    else
      included_history  = @options[:included_history] + [absolute_url]
    end
  else
    # Error: included_text contains the error meessage
    @blocks = [included_text]
    return
  end

  res = self.class.new(included_text, :helper => @options[:helper], :base_path => base_path, :included_history => included_history, :part => @params[:part], :parent => self) # we set :part to avoid loop failure when doing self inclusion

  if @params[:part]
    if iblock = res.ids[@params[:part]]
      included_blocks = include_part(iblock)
      # get all ids from inside the included part:
      @ids.merge! iblock.defined_ids
    else
      included_blocks = [parser_error("'#{@params[:part]}' not found in template '#{@params[:template]}'", 'include')]
    end
  else
    included_blocks = res.blocks
    @ids.merge! res.ids
  end

  enter(:void) # normal scan on content
  # replace 'with'

  @blocks.each do |b|
    next if b.kind_of?(String) || b.method != 'with'
    if target = res.ids[b.params[:part]]
      if target.kind_of?(String)
        # error
      elsif b.empty?
        target.method = 'ignore'
      else
        target.replace_with(b)
      end
    else
      # part not found
      parser_error("'#{b.params[:part]}' not found in template '#{@params[:template]}'", 'with')
    end
  end
  @blocks = included_blocks
end