pugjs / pug

Pug – robust, elegant, feature rich template engine for Node.js
https://pugjs.org
21.68k stars 1.95k forks source link

same named block in nesting extends file #2436

Closed Secbone closed 7 years ago

Secbone commented 8 years ago

page.pug

extends layout.pug

block foo
    h1 page text

block widgets
    include widget.pug

widget.pug

extends widget_base.pug

block foo
    h3 widget text

the block foo in page.pug will be <h3>widget text</h3>.

pug 2.0.0-alpha3

is this a bug?

ForbesLindesay commented 8 years ago

Did it have the same behaviour in jade 1.11? I've tried to keep consistency with the quirks of jade for this first release, and then change to something more clear and well defined in v3.

Secbone commented 8 years ago

@ForbesLindesay I test the case in Jade, It is rendered <h1>page text</h1> in both places. It seems a little different between jade and pug.

TimothyGu commented 8 years ago

With pug 2.0.0-beta4, and these files:

//- a.pug
extend b.pug

block a
  | a
//- cat b.pug
extend c.pug

block a
  | b
//- cat c.pug
block a
  | c

and

pug.renderFile('a.pug')

The result is

a

Try updating your Pug version.

Secbone commented 8 years ago

@TimothyGu that is not the problem. I test the case with pug 2.0.0-beta4 :

//- main.pug
extends main_base.pug
block a
    | Main A
block b
    include other.pug

//- other.pug
extends other_base.pug
block a  // this block is extends from other_base.pug, but with the same name in main_base.pug
    | Other A

I think it will render

Main A
Other A

but the result is

Other A
Other A

and I also test with jade 1.11.0, the result is

Main A
Main A
ForbesLindesay commented 7 years ago

@Secbone could you add a test case demonstrating this bug? See #2687 for an example of doing this. The snapshot files get automatically generated when you run npm test and then you can edit them to show the output you expected. Once we have a failing test, this should be much easier for us to fix.

Secbone commented 7 years ago

@ForbesLindesay OK, I will create a PR.

kylekatarnls commented 7 years ago

Hi, I'm about to implement named block in https://github.com/phug-php/phug (the PHP port of Pug that will both replace pug-php and tale-jade). Is everyone agree the expected behaviour is:

Main A
Other A

As in the unit test Secbone suggest. So if it's planned to be fixed before 2.0.0 release, I will yet implement it the Secbone way in our PHP project.

Thanks,

ForbesLindesay commented 7 years ago

@kylekatarnls yes, I agree that should be the output in this instance. I propose the following rule:

If an included template uses extends, the blocks in that template will be ignored by the template that includes it. i.e. a template being included can either use blocks to interact with the primary template, or it can be part of its own completely separate inheritance chain, but not both.

Does this make sense to people?

The consequence of that is that:

layout.pug

h1 layout
block foo

page.pug

extends layout.pug

block foo
    h2 page text

block widgets
    include widget.pug

widget.pug (note no extends in this file)

block foo
    h3 widget text

would produce:

<h1>layout </h1>
<h3>widget text</h3>"