This guide outlines what is needed to make Padrino helpers fully compatible with a template language. Examples are given in HAML.
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 templateblock_is_type?
(Boolean) - Returns whether a given block is from a foobar templatecapture_from_template
(String) - Captures the result of a block of foobar code and returns it as a stringconcat_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.
# - 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:
- A way to detect if a given template is of the particular type - is_haml?
- A way to detect if a given block is of the type - block_is_haml?
- 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.
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:
- A way to detect if a given template is of the particular type - is_haml?
- A way to concatenate to the output buffer for the template - haml_concat
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.
Good resources for learning more about padrino helpers and the 'output helpers' along with tilt rendering in general: