Jump to content

variable on _main.php is undefined on template


froot
 Share

Recommended Posts

I get an "Undefined Variable" Error on a template for a variable that I declare on the _main.php template which is clearly included.

Some background: At the beginning I didn't have a _main.php template for that particular project because I started off with a different profile but all my other projects work that way so I just created one and yes also included it on the config.php…

$config->prependTemplateFile = '_init.php';
$config->appendTemplateFile = '_main.php';
$config->useMarkupRegions = true;
$config->useFunctionsAPI = true; 

Might there be something that am I doing wrong or missing out on though? 

Also, not sure if related, when I include "namespace ProcessWire" on the top of the template, cause I thought that might be the issue, it wouldn't know the functions like __() and _x(), you know, the ones to make strings multilingual.

Call to undefined function _x(), did you mean _()?

 

Link to comment
Share on other sites

Where are you getting the error, and where are you declaring the variable? If you're trying to access a variable in the template file corresponding to the page template that has been declared in the _main.php, this won't work because the _main.php is executed last. The template files are included in the following order (assuming your config above):

_init.php -> [current-template].php -> _main.php

So if you want to declare a variable to use in all templates, you need to do that in the _init.php

All the template files should have the ProcessWire namespace. Not sure about the undefined function errors, those functions should be available in the ProcessWire namespace. Would need to see the file to diagnose.

  • Like 1
Link to comment
Share on other sites

OK I fixed it considering all the above. For some reason I always thought _main.php is prepended ?, maybe because that's where my <header> and <head> (meta data), navigation menu and whatnot is located and I got pretty far with that wrong assumption. It's still puzzling though, because I always put a <div id="content"> on _main.php which is then replaced by what comes after, i.e. what's on the template files with that same id. 

Link to comment
Share on other sites

it's actually astonishing how far I got while not thinking too much about this detail. 

I re-read the whole output strategies documentation, I say re-read because I did before, it probably just was too much to take in at once. I have been using Markup Regions, it gives me the best of both worlds, just as it states. I also appreciate the fact that I can thereby escape the php-syntax highlighting in my editor when there's a lot of HTML-markup in the template file and have it show HTML-syntax-highlighting instead and still delay the output.

I also put 

$config->prependTemplateFile = '_init.php';
$config->appendTemplateFile = '_main.php';

$config->prependTemplateFile = '_head.php'; // this I added now
$config->appendTemplateFile = '_foot.php';  // this I added now

$config->useMarkupRegions = true;
$config->useFunctionsAPI = true; 

in the config.php file and remove the head and foot section from the _main.php file. Now I get the error:

Call to undefined function ProcessWire\ukIcon()

which I reckon is just one of many functions. If I add include '_head.php' to the top of whichever template, everything works fine, just not the prepending (and possibly appending) on config file. What could be the reason? What am I missing? As much as I enjoy programming, I dislike this guessing around for hours and days so I turn to the forum.

Does the order of the $config->prependTemplateFile-included files matter? Does in the above scenario _init.php get included before _head.php or vice versa?

Thanks for help!

Link to comment
Share on other sites

9 hours ago, fruid said:

$config->prependTemplateFile = '_init.php';
$config->appendTemplateFile = '_main.php';
$config->prependTemplateFile = '_head.php'; // this I added now
$config->appendTemplateFile = '_foot.php';  // this I added now

Hello, in line 3 and 4 you simply overwrite the values you assigned to the properties in line 1 and 2. In other words: your _init.php and _main.php do not get prepended/appended because what you added afterwards is to instruct ProcessWire to prepend/append _head.php/_foot.php instead.

I recommend removing the two lines you added and use _main.php to define the placeholders for your header and footer. Something like this:

In this linked example of @Pixrael <header> and <footer> are not defined as regions but you are free to do it if you need to change them on page-by-page cases.

 

Edited by szabesz
typos
  • Like 2
Link to comment
Share on other sites

OK thanks for the input, it helps. I stick with markup regions and just "include" _head.php and _foot.php in the _main.php rather than using markup regions in this case (i.e. <div id="head">) or prepending/appending.

But why did I even start want to change that?

Because I'm hooking the page-object to add a ->fulltitle and a ->shorttitle method.

the full title is $page->parent->title.'/'.$page->title; and will be used for the snipcart-basket.

And the shot title is $page->title and will be used for the breadcrumbs to avoid redundancies.

I thought the hooks kind of store whatever I tell them to store before anything else renders. Now I realise the value they return is composed at the moment the method gets called. So on the product-page I put:

$product->title = $page->title; // which is the full title

And since the _main.php renders last and that's where my _breadcrumbs.php is included, the ->shorttitle method thinks the ->title is the fullttitle and basically overwrites what the hook is doing.

I hope I made myself clear.

Solutions could be to not set $product->title = $page->title but I don't know any other way to change the title that gets sent to the cart.
Or, include the breadcrumbs on _init.php ? But then it's above all the content… Store it in a variable on _init.php and return on _main.php?

I had a working version where I basically manually composed on each page it's required what my ->fulltitle hook is doing and then disassemble it manually with standard php to accomplish what my short-title hook is doing. I thought using hooks would make this matter simpler, now in doubt.

Thanks for further help! 

Link to comment
Share on other sites

17 hours ago, fruid said:

Solutions could be to not set $product->title = $page->title

I presume that $product is a ProcessWire page object, so you should not overwrite its $title property anyway.

17 hours ago, fruid said:

Store it in a variable on _init.php and return on _main.php?

You should be able to compose the breadcrumb's html block in advance and store it in a variable to be outputted ("rendered") later on. If you render the breadcrumb on almost all pages, the simplest solution is to compose it _main.php's opening <?php namespace ProcessWire; block. I am not saying this is the best place, you should be able to do it in _init.php too.

For example, in one of my sites I use Ryan's ukBreadcrumb() function: https://github.com/ryancramerdesign/regular/blob/master/site-regular/templates/_uikit.php

Which is included like this in _init-php:  include_once('pages/shared/utils/uikit.php'); // UIkit 3 library of Regular Blog Site Profile
I do not even store its returned value in a variable, I just output it like this where it needs to be: <?= ukBreadcrumb() ?>

Hope this helps.

Edited by szabesz
typos
Link to comment
Share on other sites

why shouldn't I overwrite the title? If I shouldn't and consequently won't, then there's no need for any mentioned work-around-solutions after all. Using the full title (i.e. parents-title/page-title) for the products and the actual title (here short title) for breadcrumbs and admin backend is what I need. 

16 minutes ago, szabesz said:

You should be able to compose the breadcrumb's html block in advance and store it in a variable to be outputted ("rendered") later on. If you render the breadcrumb on almost all pages, the simplest solution is to compose it _main.php's opening <?php namespace ProcessWire; block. I am not saying this is the best place, you should be able to do it in _init.php too.

Composing the breadcrumbs on the _main.php won't work for me. I was able to accomplish what I need by including _breadcrumbs.php on _init.php. On _breadcrumbs.php I compose the breadcrumb-markup making use of the ->shorttitle and ->fulltitle hooks I created on _init.php, and store this markup in a $breadcrumbs variable. On _main.php I output this variable and done.

However, now I'm in doubt again because…

21 minutes ago, szabesz said:

You should be able to compose the breadcrumb's html block in advance and store it in a variable to be outputted ("rendered") later on. If you render the breadcrumb on almost all pages, the simplest solution is to compose it _main.php's opening <?php namespace ProcessWire; block. I am not saying this is the best place, you should be able to do it in _init.php too.

What's the concern? It seems to work fine…

 
Link to comment
Share on other sites

On 10/15/2020 at 5:00 PM, fruid said:

$product->title = $page->title; // which is the full title

Unless I want to save the value of a page object's property in the database, I would not change a page object's property because it is reasonable to expect that a property corresponds to a value in the database. If I change the property but do not save it to the database, then from that point on it is "confusing" to keep track of the actual value of the property as its value is no longer "in sync" with the database (as long as we are talking about the same request being executed, of course).

To tell the truth, I do not really get what you are trying to solve. I was just trying to point out a few things that might – but not necessarily – cause issues.

  • Like 3
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

×
×
  • Create New...