Jump to content

$wire->set in prepend file.


Zeka
 Share

Recommended Posts

Hi. 

I declare a new wire variable in the _init.php (prepended file). 

$wire->set('general', WireArray::new($general_info));

In the template file '$general' variable is not populated, but I can access it via wire('general') or $wire->general.

So my question is why it behaves in that way? Prepended files are just included during the rendering process, but in some forum posts, I have seen examples where declared variable in _init.php is populated in a template file.

So far all 'global' variables should be declared in init.php or ready.php?  

 

Link to comment
Share on other sites

14 hours ago, Zeka said:

So far all 'global' variables should be declared in init.php or ready.php?  

It depend on your needs, check :

[...]
const statusInit = 2; // system and modules are initializing
const statusReady = 4; // system and $page are ready
[...]
Quote

/site/init.php
This file is included during ProcessWire's boot initialization, immediately after autoload modules have been loaded and had their init() methods called. Anything you do in here will behave the same as an init() method on a module. When this file is called, the current $page has not yet been determined. This is an excellent place to attach hooks that don't need to know anything about the current page.

/site/ready.php
This file is included immediately after the API is fully ready. It behaves the same as a ready() method in an autoload module. The current $page has been determined, but not yet rendered. This is an excellent place to attach hooks that may need to know something about the current page. It's also an excellent place to include additional classes or libraries that will be used on all pages in your site.

E.g:

  • in init.php, $page is not accessible (called too early)
  • in ready.php, $page is accessible.

 

14 hours ago, Zeka said:

In the template file '$general' variable is not populated, but I can access it via wire('general') or $wire->general.

Summarily said, new API variables are set (populated) before your prepended file to be consumed in your templates. If you want to access the $general variable as is from a template, you will have to set it in ready.php because here you can know the template/page context.

Check those four examples..

 

Let's say we want to debug $general from our home.php template using Tracy with bd($general);

#1 - Set new API var in init.php :

(both call return null because we try to access the API var from a template, but still here the template context is unknown)

  • $general is null
  • $wire->general is null

#2 - Set the new API var in ready.php :

  • $general is populated
  • $wire->general is populated

#3 - Set the new API var in _init.php :

  • $general is null
  • $wire->general is populated

#4 - Set the new API var in home.php :

(same result as _init.php)

  • $general is null
  • $wire->general is populated

 

Hopefully this will make thing clearer for you though an expert could explain deeper the thing.

 

  • Like 4
  • Thanks 2
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...