Class: Bookfile::PageCtx

Inherits:
Object
  • Object
show all
Includes:
HybookHelper
Defined in:
lib/bookfile/book/book.rb

Overview

page context for evaluate

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HybookHelper

#columns_begin, #columns_end, #link_to, #number_with_delimiter

Constructor Details

#initialize(config) ⇒ PageCtx

BookConfig



21
22
23
24
25
26
27
28
29
30
# File 'lib/bookfile/book/book.rb', line 21

def initialize( config )   ## BookConfig

  @config  = config
  @content = ''    ## rename to body,text,buf,out - why, why not???

  
  ## track rendering (stack) level - only output if in top-level (1)

  ##  -- check/todo - is there a better way???

  ##  use a (separate) partial method  or keep using on render method etc. ???

  ##   any other ways??

  @level = 0
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



19
20
21
# File 'lib/bookfile/book/book.rb', line 19

def content
  @content
end

Instance Method Details

#_locals_code(locals) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bookfile/book/book.rb', line 67

def _locals_code( locals )
  ## convert locals hash to erb snippet with shortcuts

  ##    e.g.  country = locals[:country]

  ## and so on


  buf = "<%\n"
  locals.each do |k,v|
    puts "  add local '#{k}' #{k.class.name} - #{v.class.name}"

    buf << "#{k} = locals[:#{k}];\n"
  end
  buf << "%>\n"
  buf
end

#render(name, opts = {}, locals = {}) ⇒ Object

possible? - make opts required ??



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bookfile/book/book.rb', line 40

def render( name, opts={}, locals={} )  ## possible? - make opts required ??

  @level +=1
  puts "*** render(#{@level})  #{name}:"

  tmpl  = File.read_utf8( "#{@config.templates_dir}/#{name}.md" )  ## name e.g. includes/_city


  ### if any locals defined; add "header/preamble w/ shortcuts" to template

  #     e.g. country = locals[:country] etc.

  unless locals.empty?
    tmpl = _locals_code( locals ) + tmpl
  end

  text  = TextUtils::PageTemplate.new( tmpl ).render( binding )

  ## note: only add text to content if top-level render call

  ##   (do NOT add for partials/includes/etc.) 

  if @level == 1
    @content << text
  end

  @level -=1

  ## puts "  #{text}"

  text
end

#write(text) ⇒ Object



33
34
35
36
37
38
# File 'lib/bookfile/book/book.rb', line 33

def write( text )
  puts "*** write:"
  ## puts "  #{text}"

  
  @content << text
end