phug-php / phug

Phug - The Pug Template Engine for PHP
https://phug.selfbuild.fr
MIT License
62 stars 3 forks source link

Read arguments passed into a mixin before it is rendered #78

Closed alessandro-fazzi closed 3 years ago

alessandro-fazzi commented 3 years ago

Hello,

I am trying to achieve to read parameters passed to a mixin before it is rendered. My will would be to validate parameters with custom implementation. E.g:

mixin post($postID)
  post
    header
      h3!= link_to(get_the_title($postID), get_permalink($postID))
    content!= get_the_excerpt($postID)

+post(1)

I've tried intercepting the rendering process with \Phug\RendererEvent::RENDER but it seems really wrong. I've tried using \Phug\CompilerEvent::NODE but it seems like I could get only the declared params with their default value ($postID with a null value given my example). I can't get how I could intercept the value 1 I'm passing to the mixin and inject my custom valdation.

Thanks in advance for your time.

kylekatarnls commented 3 years ago

Indeed, the sure thing is RENDER event is too late: the whole mixin is yet replaced by HTML/PHP code at this point. Hooking the node event is a possible solution, but if you can you should try first to make it done using pug code so the magic is not hidden to the next developer working in the template:

Does something prevent you to rename your post() mixin into an other name and add a middle mixin:

mixin validated_post($postID)
  post
    header
      h3!= link_to(get_the_title($postID), get_permalink($postID))
    content!= get_the_excerpt($postID)

mixin post($unvalidatedPostID)
  +validated_post(get_validated_post_id($unvalidatedPostID))

+post(1)
alessandro-fazzi commented 3 years ago

@kylekatarnls I'd sum up saying you convinced me :) I've opted for a more explicit approach similar to the one you proposed.

Thanks for your always kind support.