Jump to content

wireRenderFile and access PW variables


LMD
 Share

Recommended Posts

I have a strange problem and I don't know if it's bug or I'm just not quite understanding something right.

I'm using ProcessWire 3.0.34.

I have a search box fragment (the HTML) in a separate template which I 'include' via wireRenderFile() because I'm using the delayed output method for my templates.

It works perfectly well, and shows the form.

However, on the search results page, I also want to show the keywords that were entered in the search text input. This should be easy, my search function sanitises the user input (the search form uses GET) as a selectorValue and saves it to the $input->whitelist.

This is where it gets weird.

On the search results page itself I can spit out the whitelisted variable ok for the "you searched for 'blah'" text, but it doesn't get passed to the file included by wireRenderFile to show in the search form text input.

I thought all PW API variables (in this case $input) were available to included files and that only custom variables had to be passed as an array?

My first thought was that there is a context issue, so I tried wire('input') instead, but that didn't work either (and it didn't show an error), so my second thought was that maybe $input is not passed automatically and only the $page object is ($page->whatever does work), so I tried to pass the value I wanted as a custom variable:

$page_body .= wireRenderFile("includes/comic-search-form", array(
    'search_keywords' => $input->whitelist->search_keywords
));

// Note the variable works perfectly elsewhere on the template calling wireRenderFile:
$page_body .= "You searched for: {$input->whitelist->search_keywords}"; // WORKS!

In the included file, I then used:

<input type="search" name="search_keywords" id="search_keywords" size="30" placeholder="Enter keywords" 
value="<?php echo $search_keywords; ?>"> 

<!-- note, when working, the code will check the $search_keywords actually has content before echoing -->

However, it still came out blank! So I did some more tests passing other variables and also used Tracy Debugger to var dump:

bd($input->whitelist->search_keywords, 'Before');

$page_body .= wireRenderFile("includes/comic-search-form", array(
    'test_a' => $page_summary, // another custom variable from the template
    'test_b' => 'This is a test', // a direct string
    'search_keywords' => $input->whitelist->search_keywords
));

// On the incude file itself, I did the following

bd(array($test_a, $test_b, $search_keywords), 'Inside wireRenderFile()');

echo "<pre>
Test A (variable): $test_a
  Test B (string): $test_b
  Search Keywords: $search_keywords
</pre>";

In the Tracy output and the directly printed output the $test_a and $test_b variables worked perfectly as expected, but the $search_keywords was always blank (null)!

Weirdly, in the output on the template calling the wireRenderFile ("Before"), the Tracy output for $input->whitelist->search_keywords was also blank!?

If I can echo out the $input->whitelist->search_keywords value on the template it is called on, why can I not then save it as a variable to be passed to wireRenderFile ?

Is there something I am overlooking or not understanding?

 

 

Link to comment
Share on other sites

8 hours ago, LMD said:

 


$page_body .= wireRenderFile("includes/comic-search-form", array(
    'search_keywords' => $input->whitelist->search_keywords
));

// Note the variable works perfectly elsewhere on the template calling wireRenderFile:
$page_body .= "You searched for: {$input->whitelist->search_keywords}"; // WORKS!

 

What do you mean by "elsewhere"? Why don't you store the intermediate values in variable so that you can check them? E.g.:

$whitelist=whitelist; bd($whitelist);
$search_keywords=$whitelist->search_keywords; bd($search_keywords);

$page_body .= wireRenderFile("includes/comic-search-form", array(
    'search_keywords' => $search_keywords
));

Also, @adrian's Tracy has a panel called "Template Resources" showing all available variables loaded for the page.

If it is null in the template partial, then it should be null when it was passed to it...

  • Like 2
Link to comment
Share on other sites

Um, well, I am now feeling very sheepish...

I've found the problem and it is monumentally stupid of me.  I was trying to include the template partial _BEFORE_ I had run the function to do the search, so of course the variable was empty! The other places I was using the variable were after the function had run!

I only spotted it after spending some time away from the code.  I've now rejigged the code so it works as intended.

Sorry for wasting anybody's time.

  • 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

×
×
  • Create New...