Skip to content

Comments

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 from aeb35b7 to 1181325 Compare February 23, 2026 07:39
@Kocal Kocal force-pushed the 4768-types-and-blocks-docs branch 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)

$compiler
->addDebugInfo($this)
->write("/**\n")
->write($this->hasAttribute('docs') ? ' * '.str_replace("\n", "\n * ", $this->getAttribute('docs'))."\n" : '')
Copy link
Member

Choose a reason for hiding this comment

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

This need to handle cases where the content of the attribute contains */, to make sure it does not ends the PHP comment.


// Check for optional docs="..." attribute
$docs = null;
if ($stream->test(Token::NAME_TYPE, 'docs')) {
Copy link
Member

Choose a reason for hiding this comment

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

this is breaking existing code if the block is using an expression starting with a docs variable or function.

I think it needs to be parsed after the expression, not before it, for the case using the expression-based content.

@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" %}

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