webwizo / laravel-shortcodes

Wordpress like shortcodes for Laravel 4.2, 5.x, 6.x, 7.x, 8.x, 9.x and 10.x
MIT License
214 stars 49 forks source link

$viewData is not being passed through to the shortcode #35

Closed mebdev closed 5 years ago

mebdev commented 6 years ago

Hi, unless I'm doing something wrong, I think the $viewData is not being passed through to the shortcode.

I think the following needs chaning in src/View/View.php

From:

/**
 * Enable the shortcodes
 */
public function withShortcodes()
{
    $this->shortcode->viewData($this->getData());
    return $this;
}

To:

/**
 * Enable the shortcodes
 */
public function withShortcodes()
{
    $this->shortcode->enable();
    $this->shortcode->viewData($this->getData());
    return $this;
}
mebdev commented 6 years ago

Actually, putting the line

$this->shortcode->viewData($this->getData());

in the renderContents function might be better so it works when you Shortcode::enable(); as well as ->withShortcodes()

/**
 * Get the contents of the view instance.
 *
 * @return string
 */
protected function renderContents()
{
    $this->shortcode->viewData($this->getData());
    // We will keep track of the amount of views being rendered so we can flush
    // the section after the complete rendering operation is done. This will
    // clear out the sections for any separate views that may be rendered.
    $this->factory->incrementRender();
    $this->factory->callComposer($this);
    $contents = $this->getContents();
    if ($this->shortcode->getStrip()) {
        // strip content without shortcodes
        $contents = $this->shortcode->strip($contents);
    } else {
        // compile the shortcodes
        $contents = $this->shortcode->compile($contents);
    }
    // Once we've finished rendering the view, we'll decrement the render count
    // so that each sections get flushed out next time a view is created and
    // no old sections are staying around in the memory of an environment.
    $this->factory->decrementRender();

    return $contents;
}
Nuranto commented 6 years ago

I confirm the issue. I also confirm that the second fix of @mebdev is fixing the issue.