Skip to content

Instantly share code, notes, and snippets.

@nesquena
Created February 16, 2011 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesquena/829030 to your computer and use it in GitHub Desktop.
Save nesquena/829030 to your computer and use it in GitHub Desktop.
Things padrino needs to have the helpers work with a template language...

Padrino Helpers Support

This guide outlines what is needed to make Padrino helpers fully compatible with a template language. Examples are given in HAML.

Implementing an Adapter

To make Padrino work with any arbitrary templating system, simply define the following handler and we can bring it into Padrino. Here are the methods that must be defined for the foobar template language inside the handler:

  • is_type? (Boolean) - Returns whether a given template is a foobar template
  • block_is_type? (Boolean) - Returns whether a given block is from a foobar template
  • capture_from_template (String) - Captures the result of a block of foobar code and returns it as a string
  • concat_to_template - Outputs text directly to the foobar buffer, with the proper indentation.

See the haml adapter

module Padrino
  module Helpers
    module OutputHelpers
      class HamlHandler < AbstractHandler
        def is_type?
          template.respond_to?(:is_haml?) && template.is_haml?
        end

        def block_is_type?(block)
          template.block_is_haml?(block)
        end

        def capture_from_template(*args, &block)
          template.capture_haml(*args, &block)
        end

        def concat_to_template(text="")
          template.haml_concat(text)
          nil
        end
      end # HamlHandler

      OutputHelpers.register(HamlHandler)
    end # OutputHelpers
  end
end

Once that has been defined and placed into Padrino, all the helpers will begin to work.

Capturing HTML Output from Block

# - form_tag("/example")
#   = text_field_tag "foo"
def capture_html(*args, &block)
  if self.respond_to?(:is_haml?) && is_haml?
    block_is_haml?(block) ? capture_haml(*args, &block) : block.call
  else # ...
    # ...
  end
end
# should return html from block "<input type='text' name='foo' />

Required methods to make this work:

  1. A way to detect if a given template is of the particular type - is_haml?
  2. A way to detect if a given block is of the type - block_is_haml?
  3. A way to capture the html content of the particular type - capture_haml

These three methods allow the template to be detected as the proper type and then capture the output of the block for the given type.

Concatenating Output to the Template

def concat_content(text="")
  if self.respond_to?(:is_haml?) && is_haml?
    haml_concat(text)
  else # ...
    # ...
  end
end
# concat_content("foobar") should print out "foobar" to the output_buffer

Required methods to make this work:

  1. A way to detect if a given template is of the particular type - is_haml?
  2. A way to concatenate to the output buffer for the template - haml_concat

Reasons for Failure

With a new templating language the common problems are always the same. Either content can't be captured into the helpers from the template properly (capture_html) or the helpers cannot properly concatenate the content back to a template file (concat_content). Once these two problems are fixed then all helpers will work.

Resources

Good resources for learning more about padrino helpers and the 'output helpers' along with tilt rendering in general:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment