Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Support] Can we register function ? #177

Open
ylacaute opened this issue Jan 6, 2020 · 1 comment
Open

[Support] Can we register function ? #177

ylacaute opened this issue Jan 6, 2020 · 1 comment

Comments

@ylacaute
Copy link

ylacaute commented Jan 6, 2020

Is your feature request related to a problem? Please describe.
I am building a template, but it seems complicated to do almost nothing, for example :

{{!-- If contains the tag "Ignore", then the annotation @Ignore is set on this class --}}
{{#if has_tags?}}{{#each rendered_children.tags}}{{#case this}}{{#when_includes "Ignore"}}
    @Ignore
{{/when_includes}}{{/case}}{{/each}}{{/if}}

Describe the solution you'd like
I would like to have a "contains" function, and more generally, I would like to register custom function in order to have a cleaner code.

Describe alternatives you've considered
Learn handlebars ? ^^

Also, I don't understand why we can't write :

{{#each rendered_children.tags as |tag|}}

Bonus: could you give us some documentation or links to learn and use ruby handlebars ?

Thank

@tenpaiyomi
Copy link
Contributor

tenpaiyomi commented May 27, 2020

Hey @ylacaute,

For the custom handlebars functions, that should work by doing a little monkeypatching in your local code. The function that is utilized to register helpers is the Hiptest::HandlebarsHelper#register_custom_helpers:

def register_custom_helpers
  self.class.instance_methods.each do |method_name|
    if method_name.to_s.start_with? 'hh_'
      @handlebars.register_helper(method_name.to_s.gsub(/hh_/, '')) do |*args|
        send(method_name, *args)
      end
    elsif method_name.to_s.start_with? 'as_hh_'
      @handlebars.register_as_helper(method_name.to_s.gsub(/as_hh_/, '')) do |*args|
        send(method_name, *args)
      end
    end
  end
end

What it does is look for all instance methods within Hiptest::HandlebarsHelper that begin with hh_ or as_hh and, for each one, it drops the hh_ or as_hh of the method name and registers the helper with any given arguments it may have.

So, in your code you could simply create a monkeypatch like so, that gets called prior to the HiptestPublisher gem:

module Hiptest
  class HandlebarsHelper
    def hh_if_contains(context, list, expected, block, else_block)
      if list&.include?(expected)
        block.fn(context)
      elsif else_block
        else_block.fn(context)
      else
        ""
      end
    end
  end
end

(the above is some off the top of my head code, so you might have to tweak it a bit)

For the question regarding {{#each ...}}, there should be no reason that wouldn't work. That is a valid statement and rendered_children.tags acts as an array (as can be seen in the usage of join like in {{{ join rendered_children.tags ' '}}}). Are you getting some particular error?

Lastly, for the documentation for Handlebars, you can see the doc here and the gem source repo here

Hope that helps!

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

No branches or pull requests

3 participants