1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
# frozen_string_literal: true
require 'sinatra/base'
require 'sinatra/capture'
module Sinatra
# = Sinatra::HamlHelpers
#
# This extension provides some of the helper methods that existed in Haml 5
# but were removed in Haml 6. To use this in your app, just +register+ it:
#
# require 'sinatra/base'
# require 'sinatra/haml_helpers'
#
# class Application < Sinatra::Base
# helpers Sinatra::HamlHelpers
#
# # now you can use the helpers in your views
# get '/' do
# haml_code = <<~HAML
# %p
# != surround "(", ")" do
# %a{ href: "https://example.org/" } example.org
# HAML
# haml haml_code
# end
# end
#
module HamlHelpers
include Sinatra::Capture
def surround(front, back = front, &block)
"#{front}#{_capture_haml(&block).chomp}#{back}\n"
end
def precede(str, &block)
"#{str}#{_capture_haml(&block).chomp}\n"
end
def succeed(str, &block)
"#{_capture_haml(&block).chomp}#{str}\n"
end
def _capture_haml(*args, &block)
capture(*args, &block)
end
end
helpers HamlHelpers
end
|