Method: MemoRack::Core#render_with_mustache

Defined in:
lib/memorack/core.rb

#render_with_mustache(template, engine = :markdown, options = {}, locals = {}) ⇒ Object

レイアウトに mustache を適用してテンプレートエンジンでレンダリングする



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/memorack/core.rb', line 387

def render_with_mustache(template, engine = :markdown, options = {}, locals = {})
	begin
		mustache_templ = []
		mustache_templ << options[:mustache] if options[:mustache]

		options = @options.merge(options)
		locals = @locals.merge(locals)

		locals.define_key(:__content__) { |hash, key|
			if engine
				render engine, template, options
			elsif locals[:directory?]
				# ディレクトリ
				nil
			else
				template
			end
		}

		locals[:directory?] = true if template.kind_of?(Pathname) && template.directory?
		locals[:content?] = true unless template == :index || locals[:directory?]
		locals[:page] = page = Locals[locals[:page] || {}]

		if template.kind_of?(Pathname)
			path = template.to_s
			plugin = PageInfo[path]
			locals[:page] = page = plugin.new(path, page, locals) if plugin
		end

		page.define_key(:name) { |hash, key|
			if hash.kind_of?(PageInfo)
				hash.value(:title)
			elsif template != :index
				fname = locals[:path_info]
				fname ||= template.to_s.force_encoding('UTF-8')
				File.basename(fname)
			end
		}

		# マクロを組込む
		embed_macro(locals, @macro)

		# HTMLページをレンダリングする
		if engine && engine.to_sym == :html
			unless template.kind_of?(Pathname)
				path = file_search(template, @options, [engine])
				return nil unless path
				template = Pathname.new(path)
			end

			locals.define_key(:__content__) { |hash, key| }
			return render :mustache, template, {views: @themes}, locals
		end

		mustache_templ << 'page.html' if locals[:content?]
		mustache_templ << 'index.html'

		mustache_templ.each { |templ|
			path = file_search(templ, {views: @themes}, nil)
			next unless path

			path = Pathname.new(path)
			return render :mustache, path, {views: @themes}, locals
		}

		raise "Not found template #{mustache_templ}"
	rescue => e
		e.to_s
	end
end