Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added tenjin support, tests and documentation #116

Closed
wants to merge 1 commit into from

5 participants

@hannesg

As discussed here: #11

@rtomayko
Owner

Perfect, thanks.

@ghost

@rtomayko, @rkh, any plans to merge this?
i could adapt it in case it is not good to merge.

@josephholsten josephholsten referenced this pull request
Closed

Tenjin support #11

@yegortimoshenko

Tenjin is dead for 3 years already, see the latest gem release:
http://rubygems.org/gems/tenjin

It's probably better to move this functionality to tilt-tenjin gem instead.
/cc #11

@judofyr
Collaborator

Closing this because time has passed. See also my comment in #11.

@judofyr judofyr closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 13, 2011
  1. @hannesg
This page is out of date. Refresh to see the latest.
View
1  README.md
@@ -46,6 +46,7 @@ Support for these template engines is included with the package:
Creole (Wiki markup) .wiki, .creole creole
WikiCloth (Wiki markup) .wiki, .mediawiki, .mw wikicloth
Yajl .yajl yajl-ruby
+ Tenjin .rbhtml, .tenjin tenjin
These template engines ship with their own Tilt integration:
View
39 TEMPLATES.md
@@ -23,6 +23,7 @@ cross-implementation features.
* Builder - `Tilt::BuilderTemplate`
* Markaby - `Tilt::MarkabyTemplate`
* [Radius](#radius) - `Tilt::RadiusTemplate`
+ * [Tenjin](#tenjin) - `Tilt::TenjinTemplate`
Tilt also includes support for CSS processors like [LessCSS][lesscss] and
[Sass][sass], [CoffeeScript][coffee-script] and some simple text formats.
@@ -498,7 +499,44 @@ using this template engine within a threaded environment.
* [Discount][discount]
* [RDiscount][rdiscount]
* GitHub: [rtomayko/rdiscount][rdiscount]
+
+<a name='tenjin'></a>
+Tenjin (`rbhtml`, `tenjin`)
+-----------------------------------
+
+Tenjin is very similiar to [Erubis], but has some unique features like different syntax for escaped/not-escaped output. It has very few builtins and is generally aimed to be fast and simple instead of full-featured.
+
+### Configuration
+
+#### `:engine => Tilt::TenjinTemplate.engine | instance of Tenjin::Engine | Hash of options`
+Tenjin can handle multiple different tenjin-engines at once. This makes it possible to configure for example the used buffertype, precompilation and the syntax. You can specify which engine to use by the :engine key. See also: [Tenjin Engine documentation](http://www.kuwata-lab.com/tenjin/rbtenjin-users-guide.html#dev-engineclass).
+
+ # Change the templateclasse:
+ Tilt['tenjin'].new('tpl', :engine => { :templateclass => Tenjin::ArrayBufferTemplate })
+
+### Example
+
+#### Hello World
+ >> tpl = Tilt.new('tpl.tenjin'){ 'Hello #{name} '}
+ >> tpl.render(nil, :name => "World")
+ => "Hello World"
+
+#### Adding Helpers
+ >> module Foo
+ >> def foo
+ >> "BAR!"
+ >> end
+ >> end
+ >> Tilt[:tenjin].engine.use(Foo)
+ >> tpl = Tilt.new('tpl.tenjin'){ 'Hello #{foo}'}
+ >> tpl.render
+ => "Hello BAR!"
+
+### Differences to bare Tenjin
+ * Tenjin caches by default to files. Tilt replaces this behavior with method-caching.
+ * :preamble and :postamble options are always enabled for templates, since they are needed to setup the buffer.
+ * Tilt circumvents the Tenjin file-methods for templates loaded through Tilt. Therefore the Tenjin file options on Tenjin::Engine are completly ignored for these templates. For templates loaded trough Tenjin like the design template, these options are still effective.
[lesscss]: http://lesscss.org/ "Less CSS"
[sass]: http://sass-lang.com/ "Sass"
@@ -513,4 +551,5 @@ using this template engine within a threaded environment.
[discount]: http://www.pell.portland.or.us/~orc/Code/discount/ "Discount"
[rdiscount]: http://github.com/rtomayko/rdiscount/ "RDiscount"
[smartypants]: http://daringfireball.net/projects/smartypants/ "Smarty Pants"
+[tenjin]: http://www.kuwata-lab.com/tenjin/ "Tenjin"
View
3  lib/tilt.rb
@@ -175,6 +175,9 @@ def clear
register RDiscountTemplate, 'markdown', 'mkd', 'md'
register RedcarpetTemplate, 'markdown', 'mkd', 'md'
+ require 'tilt/tenjin'
+ register TenjinTemplate, 'rbhtml', 'tenjin'
+
require 'tilt/textile'
register RedClothTemplate, 'textile'
View
110 lib/tilt/tenjin.rb
@@ -0,0 +1,110 @@
+require 'tilt/template'
+
+module Tilt
+
+ class TenjinTemplate < Template
+
+ # This module allows to create a template of the desired class directly.
+ # This is needed, since tilt can supply a source directly.
+ module EnginePatches
+
+ def new_template
+ @templateclass.new(nil, @init_opts_for_template)
+ end
+
+ def context_module
+ @context_module ||= Module.new{
+ include Tenjin::ContextHelper
+ extend ContextModule
+ }
+ end
+
+ def use(x)
+ context_module.use(x)
+ end
+
+ end
+
+ module ContextModule
+
+ def use(x)
+ include(x)
+ end
+
+ end
+
+
+ class << self
+
+ # Sets the engine to use.
+ # If the engine is set, no further atempt will be made to load Tenjin.
+ attr_writer :engine
+
+ def engine_initialized?
+ !@engine.nil?
+ end
+
+ # Initializes the tenjin engine.
+ def initialize_engine
+ return if engine_initialized?
+ require 'tenjin'
+ self.class_eval "
+ class Engine < Tenjin::Engine
+ include EnginePatches
+ end
+ "
+ self.engine = Engine.new(:preamble=>true, :postamble=>true)
+ end
+
+ # Creates a new engine from the given options.
+ # @see http://www.kuwata-lab.com/tenjin/rbtenjin-users-guide.html#dev-engineclass
+ def new_engine(options)
+ initialize_engine
+ Engine.new(options.merge(:preamble=>true, :postamble=>true))
+ end
+
+ def engine
+ initialize_engine
+ return @engine
+ end
+
+ end
+
+ attr_reader :context
+
+ def initialize_engine
+ self.class.initialize_engine
+ end
+
+ def prepare
+ e = @options.fetch(:engine, self.class.engine)
+ if e.kind_of?(Hash)
+ @engine = self.class.new_engine(e)
+ elsif defined?(Tenjin::Engine) and e.kind_of? Tenjin::Engine
+ if defined? Engine and e.kind_of? Engine
+ @engine = e
+ else
+ @engine = e
+ e.extend(EnginePatches)
+ end
+ end
+ @template = @engine.new_template
+ @template.convert(self.data,self.file)
+ end
+
+ def precompiled_template(_)
+ @template.script
+ end
+
+ def evaluate(scope, locals, &block)
+ method = compiled_method(locals.keys)
+ method.bind(scope).call({:_context => @engine.context_module}.update(locals), &block)
+ end
+
+ def precompiled_preamble(_)
+ ['extend locals[:_context] if locals[:_context];',super].join
+ end
+
+ end
+
+end
View
72 test/tilt_tenjin_test.rb
@@ -0,0 +1,72 @@
+# coding: utf-8
+require 'contest'
+require 'tilt'
+require 'tenjin'
+require 'tempfile'
+
+class TenjinTemplateTest < Test::Unit::TestCase
+
+ test "should support a simple template" do
+
+ tpl = Tilt::TenjinTemplate.new(){
+ 'foo #{bar} ${baz}'
+ }
+
+ assert_equal tpl.render(Object.new, 'bar'=>'BAR','baz'=>'BAZ'), 'foo BAR BAZ'
+
+ end
+
+ test "should support yield" do
+
+ tpl = Tilt::TenjinTemplate.new(){
+ 'foo #{yield} baz'
+ }
+
+ result = tpl.render do
+
+ "BAR"
+
+ end
+
+ assert_equal result , 'foo BAR baz'
+
+ end
+
+ test "should have correct line-numbers" do
+
+ tpl = Tilt::TenjinTemplate.new('a_template'){
+<<'TENJIN'
+line one
+line two
+${raise "Line THREE!"}
+line four
+TENJIN
+ }
+
+ error = nil
+ begin
+ tpl.render
+ rescue
+ error = $!
+ end
+ assert_not_nil error
+ assert error.backtrace.first =~ /\Aa_template:3/, "First backtrace should contain the template name and line number, but got: #{error.backtrace.first.inspect}"
+
+ end
+
+ test "should accept custom helpers" do
+
+ module Foo
+ def foo
+ "BAR!"
+ end
+ end
+
+ Tilt[:tenjin].engine.use(Foo)
+
+ tpl = Tilt.new('tpl.tenjin'){ 'Hello #{foo}'}
+ assert_equal tpl.render, "Hello BAR!"
+
+ end
+
+end
View
3  tilt.gemspec
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
lib/tilt/rdoc.rb
lib/tilt/string.rb
lib/tilt/template.rb
+ lib/tilt/tenjin.rb
lib/tilt/textile.rb
lib/tilt/wiki.rb
lib/tilt/yajl.rb
@@ -69,6 +70,7 @@ Gem::Specification.new do |s|
test/tilt_sasstemplate_test.rb
test/tilt_stringtemplate_test.rb
test/tilt_template_test.rb
+ test/tilt_tenjin_test.rb
test/tilt_test.rb
test/tilt_wikiclothtemplate_test.rb
test/tilt_yajltemplate_test.rb
@@ -97,6 +99,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rdoc'
s.add_development_dependency 'redcarpet'
s.add_development_dependency 'sass'
+ s.add_development_dependency 'tenjin'
s.add_development_dependency 'wikicloth'
s.add_development_dependency 'yajl-ruby'
Something went wrong with that request. Please try again.