kilt icon indicating copy to clipboard operation
kilt copied to clipboard

Access to @type and Macro methods in Kilt.render

Open danielpclark opened this issue 7 years ago • 3 comments

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.

danielpclark avatar Apr 07 '17 08:04 danielpclark

Have you tried:

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

Kilt.file does the same as def_to_s IIRC.

jeromegn avatar Apr 17 '17 13:04 jeromegn

I wonder if I should raise this issue in Kemal then since the provided way of rendering content inside a template calls Kilt.render and leads to this error..

danielpclark avatar Apr 17 '17 14:04 danielpclark

cc @sdogruyol

danielpclark avatar Apr 29 '17 21:04 danielpclark