phug-php / phug

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

accessing $block returns true, but nothing about the content #58

Closed BananaAcid closed 4 years ago

BananaAcid commented 4 years ago

Hello,

I encountered an issue with the following code:

mixin item()
    - $newBlock = strip_tags($block);
    div= $newBlock
+item()
    | <b>hello</b>

First I thought, $block would be a function or class that would hold the content - but var_dump($block) results in (bool)true

I expected to get:

hello

But I actually get:

<div>1</div>

Thanks!

kylekatarnls commented 4 years ago

The only purpose of block variable is to know from inside the mixin declaration if the block have been filled or not on call. So to do if block as a boolean answer. If someone made a strict test if block === true changing the content of $block will break this app.

As it's a breaking change I won't change the $block variable.

Now there is still __pug_children(get_defined_vars()) to actually trigger manually the block. But it displays it, it does not return it.

In your example, it looks feasible but $__pug_children is a closure that take locals variable and actually output in the sdtout.

So you need to catch the output:

mixin item
  - ob_start()
  - __pug_children(get_defined_vars())
  - content = ob_get_contents()
  - ob_end_clean()
  div=strip_tags(content)

+item
  | <b>hello</b>

It sounds still a bit like a tweaked pattern. Can you explain why you would add tags in mixin blocks then remove them from inside the mixin declaration ?

BananaAcid commented 4 years ago

Thanks for the reply. Works great!

I have 2 reasons.

  1. I modified the item mixin to export all data (also block data) to a CSV. (was statically build with pugjs to html)
  2. changing the page, to parse the pug (and cache) through phug and taking the data dynamically from a csv/database I keep the structure, but need to pass the data in and prevent tags from appearing (script tags and so on) - this will keep the original pug file close to what it was, and does some additional stuff in the item mixin file

By any chance, do you know how to get the block data in pugjs?

kylekatarnls commented 4 years ago

It will be difficult, in pugjs, blockvariable is a function doing basically: function(){ pug_html = pug_html + "<p>"; pug_html = pug_html + "foo</p>"; }