Support the ongoing development of Laravel.io →
Views Blade
Last updated 1 year ago.
0

Hi,

for adding my widgets to a view I use an HTML extension.

HTML Extension:

// app/html_extensions.php

// Creates a widget instance and calls the render method.
HTML::macro('widget', function($widgetName, $parameters = null)
{ 
    if (! is_array($parameters)) $parameters = (array) $parameters;

    $className = 'Controllers\\'.$widgetName.'Widget';
    $widget = new $className();

    return $widget->render($parameters);
});

Example Widget:

namespace Controllers;

use Post;
use View;
use Widget;

class ChannelWidget extends Widget {

    public function render($parameters = array())
    {
        $posts = Post::where('channel', '=', $parameters['channel'])->orderBy('date_published', 'DESC')->take($parameters['howMany'])->get();


        return View::make('channel.widget', compact('posts'));
    }

}

Blade Template:

...
{{ HTML::widget('Channel', ['channel' => 'geek corner', 'howMany' => 3]) }}
...
Last updated 1 year ago.
0

[code] tags are not used here, you just indent with 4 spaces or tabs and it's marked as code.

View composers are what you want.

Last updated 1 year ago.
0

Why? They are tied to a view. And you cannot pass arguments to them, right? What is the benefit?

I started with composers, too. But then decided to use my own way a.k.a widgets.

Last updated 1 year ago.
0

Class-based view composers can utilize dependency injection similar to controllers.

If a sub-view requires parameters then its process cannot be isolated and OP's question is moot IMO - but a composer can easily access a view's variables via $view->variable if necessary.

Last updated 1 year ago.
0

Yeah okay sherwinncnadev should at least consider to use class-based view composers. It's probably the laravelish way.

However, if his/her sub-views require parameters then maybe it's not an issue of view composers. Maybe view composers do not excatly fit the requirements. Take a look at the code, it has this $channel variable. It's like a parameter. How can sherwinncnadev use it with view composers? (Accessing variables of the parent view is not the problem here.)

Last updated 1 year ago.
0

@anlutro Hi and thanks for the reply, I thought of using view composers but I can't find a way to pass data to the view composer. Also I would like to learn how to use/create a view composer class but (also) I can't find a more detailed documentation on how to use/create a view composer class. I've read the laravel documentation about view composer and it is kind of short. A link to tutorial or a better documentation would be appreciated.

@siconize Hi and thanks for sharing your code. I'll try your approach and see if it gets the job done.

Last updated 1 year ago.
0

View composers can read the View's variable

Controller

public function view($id)
{
    return View::make('post.view', ['post' => Post::find($id)]);
}

View composer

public function compose($view)
{
    return $view->with('widget', Widget::where('post_id', '=', $view->post->id)->get());
}
Last updated 1 year ago.
0

@siconize Hi siconize, I tweaked your code and it works pretty well. It's actually what I need but I'm still gonna do some research if laravel's view composers can do something like that. Thanks!

@zenry I wanted the widgets to be separate from the controller. Correct me if I'm wrong but in your example every time i need to use a certain widget let's call it widget A, and let's say widget A uses the $post->id data, I always need to provide the post data in every controller function that loads a view that has widget A in it.

Last updated 1 year ago.
0

Great question!

@zenry - What if I wanted to use the widget more than once in the same view? e.g. Show the top posts in separate blocks for more than one category on the Home Page.

I like your solution @siconize but is there any benefit to using a HTML macro vs. simply creating a helper class and calling a static method directory from the view?

I wonder what would be the "most Laravel friendly" way to do this?

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.