Jump to content

Bootstrap ProcessWire - fields not working like expected


Henrik
 Share

Recommended Posts

I' m trying to get an ajax call working - so that if a user clicks a button a paragraph of text is changing (among other things).

The php-file handling the call is in the root directory and bootstrapped. This is the code at issue:

<?php
// bootstrap ProcessWire
// Because this file isn't - and can't be - in the templates directory
// see: 
include('../index.php');

$nlPage = $wire->pages->get("/newsletter/");
$newText = $nlPage->text_without_p_tags;
echo $newText;	  
?>

This should return plain text without <p> tags because the field is formatted with "Paragraph Stripper". If I output this 3 lines of code through a regular template file It works like expected but this file returns the text with <p> tags.

I would like to know why this is so and how I can solve it.

There must be something going on with the bootstrapping or I just don't get it ...

Another thing is that if I want to resize and output an image with the field set to "maximum file 1" - thus just one image - to get it working I have to handle it as an array.

This confuses me somehow ...

Thanks for any help on this!

Link to comment
Share on other sites

Hi henrik,

this problem comes from the fact, that there is no outputformatting when bootstrapping PW. therefore image fields are always arrays even if they are set to max = 1 and all your textformatters won't work.

https://processwire.com/talk/topic/5375-image-paths-when-bootstrapping-pw/

regarding your ajax problem: you don't need to create a file outside your site folder! you have two options:

1) create a separate template only for ajax requests and also a page for that (eg hidden page /ajax) - then you can just call this page and return your content

2) return your ajax content from within the same file. if you are using delayed output strategy that's really easy. all you have to do is put something like this on top of your template file:

if($config->ajax) {
    // do your ajax stuff here
    echo 'i am an ajax response';
    die();
}

// regular call
$headline = '<h1>' . $page->title . '</h1>';
$content = $page->body;

and so on...

hope that helps! good luck :)

  • Like 3
Link to comment
Share on other sites

Hi Henrik,

I'm not sure and in a hurry, but regarding 1), it can have to do with outputformating state. Please try and compare both possibilities explicitly set:

$nlPage = $wire->pages->get("/newsletter/");
$newText_with_outputformatting = $nlPage->of(true)->text_without_p_tags;
$newText_without_outputformatting = $nlPage->of(false)->text_without_p_tags;

My guess is, that without setting it to true, in bootstraped mode it is set to false, = unformatted. This way no textformatter is supplied. Whereas the Template-Scope is the only scope where outputformatting is set to true automatically. Everywhere else, you have to explicitly check or set it.

Regarding 2: you can define what you want to have as result, array or singleimage i the settings (advanced settings?) of the image field. But this also is only supplied if the outputformatting is ON. If you are in bootstrap mode, and access to it, you get unformatted return, what always is an array for all fields that can handle multiple items.

Please refer to the docs for multiples and outputformatting to get more information. Once you have read it, you will get how it works, and that it all makes sense. :)

  • Like 1
Link to comment
Share on other sites

Hi Bernhard, Hi Horst,

thank you for your responses! After some days of travelling I've now time to look at this again.

I understand that these issues have to do with the fact that when bootstrapping PW there is no outputformatting.

Thanks for the clarification!

I followed Berhards 1. suggestion and it is working.  I thought I have to put these ajax-files in the root but obvioulsy thats not the case. I can put them in the templates directory - if I also create a page for that. (That I missed earlier).

Thank you both :)

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

×
×
  • Create New...