Jump to content
rocket

including Php in template

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.

 

 

Share this post


Link to post
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.

Share this post


Link to post
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?

 

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

×
×
  • Create New...