Possible fix for #1 - ability to use partials with an arbitrary (non-Mustache) view class.
This commit is contained in:
+4
-44
@@ -424,10 +424,10 @@ class Mustache {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function _renderPartial($tag_name, &$context) {
|
protected function _renderPartial($tag_name, &$context) {
|
||||||
$view = new self($this->_getPartial($tag_name), $this->_flattenContext($context), $this->_partials);
|
$view = clone($this);
|
||||||
$view->_otag = $this->_otag;
|
$view->_otag = '{{';
|
||||||
$view->_ctag = $this->_ctag;
|
$view->_ctag = '}}';
|
||||||
return $view->render();
|
return $view->render($this->_getPartial($tag_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -450,7 +450,6 @@ class Mustache {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepare a new context reference array.
|
* Prepare a new context reference array.
|
||||||
*
|
*
|
||||||
@@ -470,45 +469,6 @@ class Mustache {
|
|||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prepare a new (flattened) context.
|
|
||||||
*
|
|
||||||
* This is used to create a view object or array for rendering partials.
|
|
||||||
*
|
|
||||||
* @access protected
|
|
||||||
* @param array &$context
|
|
||||||
* @return array
|
|
||||||
* @throws MustacheException
|
|
||||||
*/
|
|
||||||
protected function _flattenContext(&$context) {
|
|
||||||
$keys = array_keys($context);
|
|
||||||
$first = $context[$keys[0]];
|
|
||||||
|
|
||||||
if ($first instanceof Mustache) {
|
|
||||||
$ret = clone $first;
|
|
||||||
unset($keys[0]);
|
|
||||||
|
|
||||||
foreach ($keys as $name) {
|
|
||||||
foreach ($context[$name] as $key => $val) {
|
|
||||||
$ret->$key =& $val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (is_array($first)) {
|
|
||||||
$ret = array();
|
|
||||||
|
|
||||||
foreach ($keys as $name) {
|
|
||||||
foreach ($context[$name] as $key => $val) {
|
|
||||||
$ret[$key] =& $val;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw new MustacheException('Unknown root context type.');
|
|
||||||
}
|
|
||||||
|
|
||||||
return $ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a variable from the context array.
|
* Get a variable from the context array.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class PartialsWithViewClass extends Mustache {
|
||||||
|
public function __construct($template = null, $view = null, $partials = null) {
|
||||||
|
// Use an object of an arbitrary class as a View for this Mustache instance:
|
||||||
|
$view = new StdClass();
|
||||||
|
$view->name = 'ilmich';
|
||||||
|
$view->data = array(
|
||||||
|
array('name' => 'federica', 'age' => 27, 'gender' => 'female'),
|
||||||
|
array('name' => 'marco', 'age' => 32, 'gender' => 'male'),
|
||||||
|
);
|
||||||
|
|
||||||
|
$partials = array(
|
||||||
|
'children' => "{{#data}}{{name}} - {{age}} - {{gender}}\n{{/data}}",
|
||||||
|
);
|
||||||
|
|
||||||
|
parent::__construct($template, $view, $partials);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
Children of {{name}}:
|
||||||
|
{{>children}}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
Children of ilmich:
|
||||||
|
federica - 27 - female
|
||||||
|
marco - 32 - male
|
||||||
Reference in New Issue
Block a user