When we calling a widget many times, we have to specify all attributes to be able to cleanup old ones.
eg.
We have a HeroTitle widget with a tile, a optional subtitle, a description and a optional image.
If we use the first time the widget with all attributes, the second time with only a title and a description, the widget will stay with previous subtitle and image.
My workaround is to add a optional resetAttributes method to widget and add id to Theme::widget
if (method_exists($instance, 'resetAttributes')) {
$instance->resetAttributes();
}
so you will get this code :
public function widget($className, $attributes = array())
{
static $widgets = array();
// If the class name is not lead with upper case add prefix "Widget".
if ( ! preg_match('|^[A-Z]|', $className))
{
$className = 'Widget'.ucfirst($className);
}
if ( ! $instance = array_get($widgets, $className))
{
$reflector = new ReflectionClass($className);
if ( ! $reflector->isInstantiable())
{
throw new UnknownWidgetClassException("Widget target [$className] is not instantiable.");
}
$instance = $reflector->newInstance($this, $this->config, $this->view);
array_set($widgets, $className, $instance);
}
if (method_exists($instance, 'resetAttributes')) {
$instance->resetAttributes();
}
$instance->setAttributes($attributes);
$instance->beginWidget();
$instance->endWidget();
return $instance;
}
When we calling a widget many times, we have to specify all attributes to be able to cleanup old ones.
eg. We have a HeroTitle widget with a tile, a optional subtitle, a description and a optional image.
If we use the first time the widget with all attributes, the second time with only a title and a description, the widget will stay with previous subtitle and image.
My workaround is to add a optional
resetAttributes
method to widget and add id toTheme::widget
so you will get this code :