Juergen Posted December 9, 2017 Share Posted December 9, 2017 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 More sharing options...
dragan Posted December 9, 2017 Share Posted December 9, 2017 What is that line? _functions.php:59 Do you have PW namespace in _functions.php? <?php namespace ProcessWire; 1 Link to comment Share on other sites More sharing options...
Juergen Posted December 9, 2017 Author Share Posted December 9, 2017 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 More sharing options...
Juergen Posted December 9, 2017 Author Share Posted December 9, 2017 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 More sharing options...
dragan Posted December 9, 2017 Share Posted December 9, 2017 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? 1 Link to comment Share on other sites More sharing options...
Juergen Posted December 10, 2017 Author Share Posted December 10, 2017 Using namespace Processwire in both files solves the problem. A writing mistake leads to that this approch did not work on the first attempt. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now