Jump to content

Where do I put functions specific to a profile?


alkahest
 Share

Recommended Posts

This is pretty basic, possibly more PHP-related than PW:

Where do we put generic functions that are specific to the installation? In WordPress, this was in the functions.php theme file, where if you put functions there, they become accessible throughout the application.

I've made an init.php file that I include before the header/footer for all the templates in the /site/templates folder, but functions I define in there don't seem to be accessible to files included in those templates. (So like, page.php includes init.php, header.php, callout.php, and footer.php, but when I call someFunction() defined in init.php from callout.php, it tells me the function is undefined).

I have a group of common functions I'm using across multiple sites defined in a module as static public functions of a single class, and that's working everywhere. Do I need to make a module for my functions specific to the installation?

Link to comment
Share on other sites


basic-page.php

_______________________________

01|<?php |

02|include("./inc/variables.php");|

03|include("./inc/func.php"); |

04| |

05|$content = "PHP VooDoo Magic"; |

06| |

07| |

08|include("./inc/out.php"); |

09|_______________________________|

./inc/out.php

_______________________________

01|<!DOCTYPE html> |

02|<html> |

03| |

04|<?= $content ?> |

05| |

06| |

07| |

08| |

09|_______________________________|

Link to comment
Share on other sites

I don't know why your init.php file doesn't work if it's included before all other files. Anyway, have you seen this in the config file?

/**
 * prependTemplateFile: PHP file in /site/templates/ that will be loaded before each page's template file
 *
 * Uncomment and edit to enable.
 *
 */
// $config->prependTemplateFile = '_init.php';
 
  • Like 1
Link to comment
Share on other sites

I don't know why your init.php file doesn't work if it's included before all other files. Anyway, have you seen this in the config file?

/**
 * prependTemplateFile: PHP file in /site/templates/ that will be loaded before each page's template file
 *
 * Uncomment and edit to enable.
 *
 */
// $config->prependTemplateFile = '_init.php';
 

Sweet, I had no idea about that. I'll try that... Not sure what's going on either.

Link to comment
Share on other sites

You can make a simple WireData module to put functions specific to your project you can do all sorts of things like hooks, your own helper markup generating functions for use in template or other modules, or whatever else.

See the HelloWorld.module that comes with the default profile.

It comes down much to preference but I do that often, and instead of including a functions.php in template it will be available everywhere.

You could even go as far as setting a new API var for use in the system.

In the init() of such a autoload module you would call:

$this->set->fuel("projects", $this);

And you'd have a $projects API var that you could call methods from your class. Or set properties to existing API vars to maybe have a $user->fullname. Possibilities are endless.

  • Like 2
Link to comment
Share on other sites

You can make a simple WireData module to put functions specific to your project you can do all sorts of things like hooks, your own helper markup generating functions for use in template or other modules, or whatever else.

See the HelloWorld.module that comes with the default profile.

It comes down much to preference but I do that often, and instead of including a functions.php in template it will be available everywhere.

You could even go as far as setting a new API var for use in the system.

In the init() of such a autoload module you would call:

$this->set->fuel("projects", $this);

And you'd have a $projects API var that you could call methods from your class. Or set properties to existing API vars to maybe have a $user->fullname. Possibilities are endless.

It sounds like this would be way more widely accessible than prepending a functions file before each template, I think I'll go this route. 

Link to comment
Share on other sites

Also take a look at the Foundation site profile that uses both $config->prependTemplateFile and $config->appendTemplateFile options. It also takes the approach of defining a bunch of shared functions, similar to what you were trying to do. I have no idea why yours didn't work though, as if you include a file with functions defined in it, then those functions should be available to any code that follows it, regardless of what file the code is in. I think that either the Skycrapers or Foundation profiles would be good examples to look at. 

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