Skip to content

Lacking full support for Closures #57

@adjenks

Description

@adjenks

On the releases page it states that there was added support for closures:
https://github.com/phptal/PHPTAL/releases

However, I found that when I used a closure directly on a template variable a closure would not work.
Only when using a "repeat" tag do closures appear to work. I'm not sure where you would fix this, but I found that closure support was provided in RepeatController.php in these lines:

elseif ($source instanceof Closure) {
    $this->iterator = new ArrayIterator( (array) $source() );

So perhaps something similar needs to be done.

For example this will fail to load:
$details = function(){return 'some string';};
$template->details = $details;

<b tal:content="details"></b>

But if $details was a closure that returned an array and it was iterrated over like so it works:

 $details = function(){return [0=>['name'=>'Jim']];};

<li tal:repeat="detail details" class="media">
    <div tal:content="detail/name">Name Here</div>
</li>

There might be a typo somewhere in that code but you should get the idea.

I wanted to use a closure to lazy load my template variable but it only worked when I used repeat, so I believe I found a problem. I found that I can temporarily work around this problem by returning a one element array and using the tal:repeat directive.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions