Jump to content

$files->include() don't get vars from _init.php??


Pixrael
 Share

Recommended Posts

Hi, in my _main.php when I use the native PHP include() function, I can get in the include file, the vars declared in _init.php, but if I use the PW functions $files->include(), $files->render() or wireRenderFile() I don't have access to this vars. It's normal? I need to pass all this vars explicitly to the PW function? An example vars in _ini.php is $styles = $config->urls->templates.'styles'; ..I use the PW namespace in all my files..

Link to comment
Share on other sites

Have you looked at the template in the backend of processwire and checked to make sure that the checkbox for "Disable automatic prepend of file: _init.php"  is unchecked? I believe by default processwire appends this file to all templates (unless you uncheck it) giving access to what is defined in init (someone please correct me if I am wrong).

Link to comment
Share on other sites

Only the native include() function does implicitly share variables with included php files. Any userland functions/methods cannot do that, which is why $files->include() cannot do it as well. $files->include() is there, because it's aware of processwire's filecompiler, if you don't need that you can simply use include() as well. 

  • Like 2
Link to comment
Share on other sites

louisstephens the prepend file option is ok, thanks.

@lostkobrakai - thanks for clarifying that. Another question for you: what is best (in your opinion of course) for stuff like twitter cards, facebook cards, prefetch tags, etc.. use an include file with the logic + markup, or use a function that do the logic and return the same markup?

Link to comment
Share on other sites

3 hours ago, Pixrael said:

what is best (in your opinion of course) for stuff like twitter cards, facebook cards, prefetch tags

I use a partial template for those. I have a meta-social.php where I keep FB, Twitter specific meta tags, and call it (and optionally pass specific variables) with wireRenderFile('partial/meta-social', ['a' => 1, 'b' => 2]); inside template files. Same for JSON+LD data for microdata.

region('foot+', partial('meta-breadcrumbs'));
region('foot+', partial('meta-site'));
region('foot+', partial('meta-analytics'));
region('+head', '<link rel="dns-prefetch" href="//www.google-analytics.com">');

For prefetch tags, like CSS, and JS that needs to be in every page, I use _init.php that is prepended to every template (using $config->prependTemplateFile), but template specific assets goes to individual template files.

If you're using delayed output strategy, I recomment region() function that comes with Functions API (enable $config->useFunctionsAPI = true) where you can define regions and append prepend any string. 

https://processwire.com/blog/posts/processwire-3.0.39-core-updates/#new-functions-api

Link to comment
Share on other sites

3 hours ago, abdus said:

I use a partial template for those. I have a meta-social.php where I keep FB, Twitter specific meta tags, and call it (and optionally pass specific variables) with wireRenderFile('partial/meta-social', ['a' => 1, 'b' => 2]); inside template files. Same for JSON+LD data for microdata.


region('foot+', partial('meta-breadcrumbs'));
region('foot+', partial('meta-site'));
region('foot+', partial('meta-analytics'));
region('+head', '<link rel="dns-prefetch" href="//www.google-analytics.com">');

For prefetch tags, like CSS, and JS that needs to be in every page, I use _init.php that is prepended to every template (using $config->prependTemplateFile), but template specific assets goes to individual template files.

If you're using delayed output strategy, I recomment region() function that comes with Functions API (enable $config->useFunctionsAPI = true) where you can define regions and append prepend any string. 

https://processwire.com/blog/posts/processwire-3.0.39-core-updates/#new-functions-api

I already use a configuration similar to you, where the modern favicon, social tags, json schemas, prefetch, etc. is under each own partial, and in _main.php I include the partials common for all pages, and the templates files put the rest, but instead of region() function I use the new <region> tags because allow me to use all the autocomplete features of my IDE. ..inside the partials sometimes I need to do adjustments before echo and the related queries are made there. A question: partial() is a function from your arsenal to include files eh?

My question to @LostKobrakai is because in some post before, he talk about using a call to a function to render the same partials in the head, something similar to the renderNav() function from the PW starter profiles..

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...