Upgrading: Iterator state-methods

27 views
Skip to first unread message

Andy Adiwidjaja

unread,
Mar 13, 2012, 10:07:23 AM3/13/12
to silverst...@googlegroups.com
Hi all,

I'm beginning to test silverstripe upgrading to 3.0beta1 but I stumbled upon this in the template upgrading guide:

"Some of the removed methods were utilities that exposed the current state of the current scope's iteration. These currently have no replacement. "

So, there is no "First", "Last" etc. in the loop-Statement. What is the plan for this, will something similar be implemented? Or should we count ourselves ;-)

Thanks for the info,


Andy

Hamish Friedlander

unread,
Mar 13, 2012, 4:55:31 PM3/13/12
to silverst...@googlegroups.com
Hi Andy,

These are available in templates (.ss files), but not in PHP files, like controllers. Most don't make sense outside a scope, and there were some nasty edge cases when storing the scope on the collection instance as in 2.4. But for example some tests were using TotalItems.

There are some legitimate cases I can imagine you'd want to get these values from PHP, like methods designed to be called in a loop in a template. An example that no longer works:

getColumnClass() {
    if ($this->First) return 'column-first';
}

We decided having First exposed on $this in this way was poor design. It only makes sense on collections but is present on all objects, and looking at that method you might be tempted to call it outside a template, where it will probably return something, but not something useful or valid.

The way I would expect we would expose this to php if needed is to expose the current scope, so that you could write methods like:

getColumnClass() {
    if (SSViewer::current_scope()->First) return 'column-first';
}

This currently isn't implemented, as I wasn't sure it was needed - having written the above example, perhaps it is. Thoughts?

Hamish Friedlander

--
You received this message because you are subscribed to the Google Groups "SilverStripe Core Development" group.
To view this discussion on the web visit https://groups.google.com/d/msg/silverstripe-dev/-/vrNKgopSvjYJ.
To post to this group, send email to silverst...@googlegroups.com.
To unsubscribe from this group, send email to silverstripe-d...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/silverstripe-dev?hl=en.

Andy Adiwidjaja

unread,
Mar 14, 2012, 4:23:17 AM3/14/12
to silverst...@googlegroups.com
Hi,


Am Dienstag, 13. März 2012 21:55:31 UTC+1 schrieb Hamish Friedlander:
These are available in templates (.ss files), but not in PHP files, like controllers. Most don't make sense outside a scope, and there were some nasty edge cases when storing the scope on the collection instance as in 2.4. But for example some tests were using TotalItems.

Ah ok, I misunderstood this in the docs. I will try to contribute to the documentation to make things clearer.
 
We decided having First exposed on $this in this way was poor design. It only makes sense on collections but is present on all objects, and looking at that method you might be tempted to call it outside a template, where it will probably return something, but not something useful or valid.

Yes that's right.
 
The way I would expect we would expose this to php if needed is to expose the current scope, so that you could write methods like:

getColumnClass() {
    if (SSViewer::current_scope()->First) return 'column-first';
}

This currently isn't implemented, as I wasn't sure it was needed - having written the above example, perhaps it is. Thoughts?

That would be no good design either as the model class shouldn't be aware of the view and usage of a global function won't help the design, too. That's something I am missing generally - the possibility to wrap models into view-specific adaptors. These could be aware of the view (disclaimer: I'm coming from the Python-/Zope-world).

But an easy solution would be to make the loop iterator available (maybe it is already). Something like First() is a template helper but in PHP we don't need something framework-specific. With the new template engine, we could write:

$ColumnClass($Loop)

And could implement:

getColumnClass($iter) {
    if ($iter->key() == 0) return 'column-first';
}

We could use a DataObjectSet-specific method, too, but I think the iterator interface should be sufficient in PHP. The syntax should be the same when iterating in pure PHP:

foreach($set as $item) {
        if ($set->key() == 0) print 'column-first';
}

Here, the SSViewer::current_scope() wouldn't work.


Andy

Sam Minnée

unread,
Mar 14, 2012, 8:20:08 PM3/14/12
to silverst...@googlegroups.com
Am Dienstag, 13. März 2012 21:55:31 UTC+1 schrieb Hamish Friedlander:
These are available in templates (.ss files), but not in PHP files, like controllers. Most don't make sense outside a scope, and there were some nasty edge cases when storing the scope on the collection instance as in 2.4. But for example some tests were using TotalItems.

Ah ok, I misunderstood this in the docs. I will try to contribute to the documentation to make things clearer.

That would be really helpful.  It's usually best to have someone other than the creator of a piece of code write documentation for it! ;-)

Reply all
Reply to author
Forward
0 new messages