Skip to content

Add support for docs property for BlockNode and TypesNode#4770

Open
Kocal wants to merge 1 commit intotwigphp:3.xfrom
Kocal:4768-types-and-blocks-docs
Open

Add support for docs property for BlockNode and TypesNode#4770
Kocal wants to merge 1 commit intotwigphp:3.xfrom
Kocal:4768-types-and-blocks-docs

Conversation

@Kocal
Copy link
Contributor

@Kocal Kocal commented Feb 23, 2026

Close #4768

(Code generated almost entirely using Claude Opus 4.5.)

The following code is now valid (essential parts of our UX Toolkit Shadcn Button component):

{%- types {
    variant: "'default'|'secondary'|'destructive'|'outline'|'ghost'|'link'" docs="Visual style variant.",
    size: "'default'|'xs'|'sm'|'lg'|'icon'|'icon-xs'|'icon-sm'|'icon-lg'" docs="The button size.",
    button: '"button"' docs="The HTML tag to render.",
} -%}

{%- block content docs="The button label and/or icon" %}{% endblock -%}

@Kocal Kocal force-pushed the 4768-types-and-blocks-docs branch 2 times, most recently from 1181325 to ce52af9 Compare February 23, 2026 07:42
$compiler
->addDebugInfo($this)
->write("/**\n")
->write($this->hasAttribute('docs') ? ' * '.str_replace("\n", "\n * ", $this->getAttribute('docs'))."\n" : '')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be a nice addition, it costs nothing:

Image

Copy link
Member

@stof stof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the introduction of the assignment operator, I think this proposal is ambiguous for blocks as {% block content docs="Hello world!" %} is a valid code already, using docs="Hello world!" as the expression (even though assigning a docs variable in that scope is quite useless, as the block is the scope boundary)

@willrowe
Copy link
Contributor

I think desc or description would be more clear than docs.

@Kocal
Copy link
Contributor Author

Kocal commented Feb 23, 2026

I think this proposal is ambiguous for blocks as {% block content docs="Hello world!" %} is a valid code already

Are you sure? Because using {%- block content docs="The button label and/or icon" %}{% endblock -%} on 3.x gives me Unknown "endblock" tag in components/Button.html.twig at line 35.:
image

Or did I miss something?

@stof
Copy link
Member

stof commented Feb 24, 2026

@Kocal the {% endblock %} tag is forbidden when using the shortcut in the start tag. But the switch between both variants is done purely based on the start tag (we need to know the variant before trying to parse the tag body as only one variant has it).
What is valid on 3.x is {%- block content docs="The button label and/or icon" %}

@Kocal
Copy link
Contributor Author

Kocal commented Feb 25, 2026

Ah I see, I didn't think about this case. Thank you!

I've pushed a new commit that look for =token , a string token, and then endblock token.

@Kocal Kocal force-pushed the 4768-types-and-blocks-docs branch from 94b4edf to d1d0374 Compare February 25, 2026 10:16
@Kocal Kocal force-pushed the 4768-types-and-blocks-docs branch from d1d0374 to 8918e4d Compare March 18, 2026 07:39
@willrowe
Copy link
Contributor

I want to reiterate that I do not think that docs is a good name for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Standardize Twig comment annotations for documenting template variables and blocks

3 participants