Skip to content

Access to @type and Macro methods in Kilt.render #13

@danielpclark

Description

@danielpclark

When I use ECR.def_to_s I have access to @type and the macro methods from https://github.com/crystal-lang/crystal/blob/master/src/compiler/crystal/macros/methods.cr and the following works for me to create a rendered view dynamically based on the class name. This is written in a inheritable class BaseController.

ECR.def_to_s \
  "#{`pwd`.chomp}/app/views/" +
  @type.name.id.
  split("::").last.
  split("Controller").first.
  downcase +
  ".ecr"

When IndexController inherits it it evaluates to "/home/user/dev/project/app/views/index.ecr" and the website works.

When I try to use Kilt.render I get the following error:

   1.     String.build do |__kilt_io__|
>  2.       Kilt.embed(("#{(`pwd`).chomp}/app/views/" + ((@type.name.id.split("::")).last.split("Controller")).first.downcase) + ".ecr", "__kilt_io__", )
   3.     end
   4.   

undefined macro method 'Call#split'

This particular issue isn't a limitation of the language as I can already accomplish it with ECR.def_to_s. If you could please refactor Kilt.render to allow the above code sample to work I would greatly appreciate it. Thanks!

P.S. One context note is ECR.def_to_s must be executed within a class but outside of any method.
P.P.S. The reason this issue is important to me is Kemal uses this project for wrapping layout templates.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions