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