teepluss / laravel-theme

Theme and asset managing for laravel
MIT License
546 stars 132 forks source link

[feature request] Widgets arn't cleanup between calls #76

Closed j-a-p-dev closed 9 years ago

j-a-p-dev commented 10 years ago

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;
    }
manfredjb commented 10 years ago

Actually, the widget system of this package is not the best option.

teepluss commented 9 years ago

I know this problem,