Jump to content

Pros and cons of $wire->set("custom", $custom);


Zeka
 Share

Recommended Posts

Hi.

I have found very useful to store some data like translations and some global settings inside $wire object by

$wire->set("custom", $custom);

What are pros and cons of using it?

And there is the thing in the docs that I can't understand:

Injection of dependencies to Wire derived objects:
$this->wire($widgets);

Can somebody explain what are dependencies in this case? 

Thanks, Eugene. 

Link to comment
Share on other sites

I enjoy using custom API variables in my templates. One of my usual setups is like this.

// ready.php or _init.php
$v = new \stdClass();
$v->class = "template--$page->template";
$v->{$page->template->name} = new \stdClass();
$v->scripts = new FilenameArray();
$wire->set('v', $v);

Then at any time during template render I can add scripts using $v->scripts->add() or body class with $v->class .= "one two three", and render in my main layout.

  • It's useful because you can always access it from templates, without needing to inject it with $files->render('template', $data)
  • You can set it and extend with anything: a utility class, a simple array, a flag

One disadvantage could be that since held in RAM all the time and injected to every TemplateFile instance, you'd need a slightly larger amount of RAM for large objects.

Shameless plug: I wrote a blog post on this topic which details the points I've written above.

Injection feature is for setting the current ProcessWire instance to an object. Not really needed unless you're working with multiple instances

 

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...