smarty-php / smarty

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.
Other
2.25k stars 709 forks source link

Block assign doesn't break, but doesn't work? #858

Closed rudiedirkx closed 1 year ago

rudiedirkx commented 1 year ago

In the child template:

{block content}
  Content with lots of html here
{/block}

In the parent template:

{block name=content assign=content}{/block}
{$content|strlen}

But $content is empty. assign seems to be a valid parameter, but it's not used?

wisskid commented 1 year ago

It is supported, but probably not in the way you expected it to be. The child block completely overwrites the block with the same name in the parent. So in your example, the assign=content part is never executed. If you set assign=content on the child block, it does work though:

038_parent.pl

{block name=content}{/block}Captured content is: {$content}

038_child.tpl

{extends file='038_parent.tpl'}
{block name=content assign=content}Content with lots of html here{/block}

result

Captured content is: Content with lots of html here
rudiedirkx commented 1 year ago

But I can't have the assignment depend on the child template.... The parent template decides what to do with the block output. Is my case not possible? In a different way, not with assign.

In Twig you can 'call' a block from the parent in 2 ways:

  1. {% block content %}{% endblock %} if you just want to print/stream it inline, like a Smarty block.
  2. {% set content = block('content')|trim %} if you want to do something with it, like check if it's empty after trimming the output, or show it twice, etc.

Does Smarty have 2.?

wisskid commented 1 year ago

Blocks can be nested, or you can nest the block in a {capture}. SimonOp 6 feb. 2023 om 03:06 heeft Rudie Dirkx @.***> het volgende geschreven: But I can't have the assignment depend on the child template.... The parent template decides what to do with the block output. Is my case not possible? In a different way, not with assign. In Twig you can 'call' a block from the parent in 2 ways:

{% block content %}{% endblock %} if you just want to print/stream it inline, like a Smarty block. {% set content = block('content')|trim %} if you want to do something with it, like check if it's empty after trimming the output, or show it twice, etc.

Does Smarty have 2.?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you modified the open/close state.Message ID: @.***>