[Solar-svn] Revision 2753
pmjones at solarphp.com
pmjones at solarphp.com
Mon Sep 17 09:07:24 CDT 2007
branch Solar_View: [BRK] Instead of converting objects to arrays in the partial() method, we now keep the object as-is and name it for the filename.
E.g., if you pass an object to a partial named `_item.php`, the object is now available as `$item`. Previously, the object would have been converted to an array so that its separate properties would be variables, but this causes trouble when working with complex Solar_Struct objects (like model records).
Modified: branches/orm/Solar/View.php
===================================================================
--- branches/orm/Solar/View.php 2007-09-08 20:05:33 UTC (rev 2752)
+++ branches/orm/Solar/View.php 2007-09-17 14:07:24 UTC (rev 2753)
@@ -527,13 +527,17 @@
*
* @param string $name The partial template to process.
*
- * @param array $vars Additional variables to extract within the
- * partial template scope.
+ * @param array|object $spec Additional variables to use within the
+ * partial template scope. If an array, we use extract() on it.
+ * If an object, we create a new variable named after the partial template
+ * file and set that new variable to be the object. E.g., passing an
+ * object to a partial template named `_foo-bar.php` will use that object
+ * as `$foo_bar` in the partial.
*
* @return string The results of the partial template script.
*
*/
- public function partial($name, $vars = null)
+ public function partial($name, $spec = null)
{
// use a try/catch block so that if a partial is not found, the
// exception does not break the parent template.
@@ -547,25 +551,39 @@
);
}
- // remove the partial name from local scope
- unset($name);
-
// save partial vars externally. special cases for different types.
- if ($vars instanceof Solar_Struct) {
- $this->_partial_vars = $vars->toArray();
- } elseif (is_object($vars)) {
- $this->_partial_vars = get_object_vars($vars);
+ if (is_object($spec)) {
+
+ // the object var name is based on the partial's template name.
+ // e.g., `foo/_bar-baz.php` becomes `$bar_baz`.
+ $key = basename($this->_partial_file); // file name
+ $key = substr($key, 1); // drop leading underscore
+ if (substr($key, -4) == '.php') {
+ $key = substr($key, 0, -4); // drop trailing .php
+ }
+ $key = str_replace('-', '_', $key); // convert dashes to underscores
+
+ // keep the object under the key name
+ $this->_partial_vars[$key] = $spec;
+
+ // remove the key name from the local scope
+ unset($key);
+
} else {
- $this->_partial_vars = (array) $vars;
+ // keep vars as an array to be extracted
+ $this->_partial_vars = (array) $spec;
}
- // remove the partial vars from local scope
- unset($vars);
-
- // disallow resetting of $this and inject vars into local scope
+ // remove the partial name and spec from local scope
+ unset($name);
+ unset($spec);
+
+ // disallow resetting of $this
unset($this->_partial_vars['this']);
+
+ // inject vars into local scope
extract($this->_partial_vars);
-
+
// run the partial template
ob_start();
require $this->_partial_file;
More information about the Solar-svn
mailing list