Jump to content

including Php in template


rocket
 Share

Recommended Posts

Noob to Processwire. Trying to convert some older sites to Pwire. I am having trouble including a legacy form class into my templates:

If I embed the php directly in the template, it works. But if I use include statements, the form object returns null.

This works:

    use formbuild\Form;
    session_start();
    $form = new Form(init1, 'init2', ...');

    $form->param1 = ...;

    $form->param2 = ...;

    $form->param3 = ...;

 

    $form->render();

-----------------------------------------------------------------------------------------

This fails:

    include_once ($config->paths->templates."forms/form1.php");

    $form->render();

____________________________________________________________________

My referencing is OK, no error message. The only code that cannot reside in the include file are the following lines:

    use formbuild\Form;
    $form = new Form(init1, 'init2', ...');

My PhP is rusty, maybe that is the problem, but it could be something about Pwire, namespace maybe. I have tested it outside Pwire and no problems.

Please advise. Thanks.

 

 

Link to comment
Share on other sites

 

Hello,

1 hour ago, rocket said:

This fails:

    include_once ($config->paths->templates."forms/form1.php");

    $form->render();

Try to add at the top of your included form1.php this:

namespace ProcessWire;

Regards.

Link to comment
Share on other sites

When I try adding to the top of include file

namespace ProcessWire;

and moving

 use formbuild\Form;
 $form = new Form(init1, 'init2', ...'); 

back into the file, I get " Unable to complete this request due to an error. - Error has been logged. "

But there is no log file I can find with a current entry in it, even doing a global search on the server. ( I am running locally on WAMP. )

Where do I look?

 

Link to comment
Share on other sites

use formbuild\Form;
use formbuild\Validator;
session_start();
include ($_SERVER['DOCUMENT_ROOT']. '/formbuild/Form.php');
$form = new Form('test-form', 'horizontal', 'novalidate');
/***********************All forms must have above header *************************************/

If I add this as a header to the file I include in my template [ eg. include ($config->paths->templates.'forms/test-form.php');], it works.

If I separate it into a file and add it to test-form.php as an include [eg. include ($config->paths->templates.'forms/_form_header.php'); ], it  fails.

Screen says an error was logged, but it shows up in no log files. Adding 'namespace ProcessWire' makes no difference.

Link to comment
Share on other sites

OK, I found a workaround. I added a field 'formID' to my ' form_page' template . In the template, I have a single include in the body which looks like this:

include ($config->paths->templates.'forms/'. $page->formID .'.php');

All my forms go into the forms directory. To render a form on a page which uses the form_page, all I need to do is set the page's 'formID' field to the form's filename.

As long as I don't include any other includes within the form file above.  I can live with that.

 

I am enjoying Processwire.

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