Jump to content

How to use a file inside templates folder outside of PW


Juergen
 Share

Recommended Posts

Hello @ all,

I have a general function file which is located in site/templates/ directory (path: site/templates/_functions.php). This file includes several functions which will be used across the site. It will be loaded in site/templates/_init.php with the following command:

require_once('./_functions.php');

This works.

Now I want to use a function of this file outside the templates folder. This folder is located in site/ajax directory (path: site/ajax/emailvalidation.php).

To use variables from PW outside the templates folder I bootstrap the index file like this:

include("../../index.php"); //bootstraping index.php

So this works, but unfortunately the _functions.php will not be fetched. So I have tried to include it via include but then I always the following error message.

Cannot redeclare ProcessWire\debug() (previously declared in /home/.sites/24/site1275/web/site/templates/_functions.php:59)

So it seems that _functions.php will be loaded. But if I remove this line than I get an error message that the function is unknown????

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

So how can I use a function of a file inside the templates folder outside the templates folder. In this case it is a function for translateable strings.

Best regards

Link to comment
Share on other sites

Yes, that was one part of the problem. Including the same namespace in the file outside the PW folder removes the previous error messages, but the function do not work. Maybe I should remove the namespace in the _functions.php?

Link to comment
Share on other sites

Or is there a possibility to use namespace in the _functions.php (inside templates folder) and no namespace in the file outside the template folder (emailvalidation.php).

Something like "use \Processwire" at the beginning of emailvalidation.php?

Link to comment
Share on other sites

1 hour ago, Juergen said:

To use variables from PW outside the templates folder I bootstrap the index file like this:


include("../../index.php"); //bootstraping index.php

It's a bit tricky, without knowing more. Since the error msg says "cannot redeclare", did you try to bootstrap with include_once instead of include?

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...