thomas Posted November 27, 2012 Share Posted November 27, 2012 Hello everyone, my oop knowledge is pretty limited so please don't stone me for the forthcoming question: I have a rather large project which I'm moving from symfony 1.4 to PW (finally). I'm rebuilding most of the functions in modules with Page hooks, which at one point (now) leads to a naming problem. LIke a countMedia() method can be neccessary for many different contextes. - Is there a way to give a module it's own class name? So I can use $channel->countMedia(), $tag->countMedia(), both referring to different modules? - how do YOU deal with a large set of user functions that are needed in many different templates? Thanks, thomas Link to comment Share on other sites More sharing options...
Nico Knoll Posted November 27, 2012 Share Posted November 27, 2012 Well, looks like you want to name instances of a class: # 1. Option $channel = new channel(); $tag = new channel(); # 2. Option $channel = new channel(); $tag = new tag(); In this case "channel" is one class and "tag" is another and you're creating instances of it with "new ...()". Link to comment Share on other sites More sharing options...
Soma Posted November 27, 2012 Share Posted November 27, 2012 This is an simple example module one can create within minutes to add a custom template variable "$channel" you can use in templates if you load the module (note it's not autoload). The important part here is the $this->fuel->set("channel",$this); in the init() function, which creates the template variable (like $pages) and attaches the class to it with $this. Just a simple $modules->get("ModuleName"); will do it in the start of you main template or, you could also make it autoload if you really want to load in on every request (front and backend) Create a module MyChannels.module with something like this, install it and you're set. You could create also one module for tags. class MyChannels extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'My Channels', 'version' => 100, 'summary' => 'Useful functions for templates.', 'href' => '', 'singular' => true ); } public function init() { $this->fuel->set("channel", $this); } public function countMedia() { $count = $this->pages->count("template=media-channel"); return $count; } } To load the module in a template $modules->get("MyChannels"); // load the module // now you can use echo $channel->countMedia(); Another method would be to have a helper module, that also isn't autoload, but you instance it on a variable and use it to call different module public methods. Same code but slightly different in that you only have one module with lots of functions and use it slightly different. class MyHelpers extends WireData implements Module { /** * getModuleInfo is a module required by all modules to tell ProcessWire about them * * @return array * */ public static function getModuleInfo() { return array( 'title' => 'My Helpers', 'version' => 100, 'summary' => 'Useful functions for templates.', 'href' => '', 'singular' => true ); } public function init() { // maybe good to add hooks here if needed in the project } public function countMediaChannel($options = null) { $count = $this->pages->count("template=media-channel $options"); return $count; } public function countMediaTag() { $count = wire("pages")->count("template=media-tag"); return $count; } } To load this module in a template $mediahelper = $modules->get("MyChannels"); // load the module // now you can use it echo $mediahelper->countMediaChannel(",include=all"); I do this a lot, and sometimes only because it's fun and feels good, but also helps. Everybody can do it, even I. This is only some pseudo code, but there's nothing else involved. If you familar with PW API in templates you can code modules. Also take a look at the HelloWorld.module that comes with the basic-profile install. There's also more examples like properties 3 Link to comment Share on other sites More sharing options...
ryan Posted November 27, 2012 Share Posted November 27, 2012 how do YOU deal with a large set of user functions that are needed in many different templates? The methods Soma mentioned are great best practices. Admittedly I'm not always that complete, especially on smaller sites where there's not much benefit [for me at least] in pursuing objects and modules. In these cases, I'll just make a /site/templates/functions.inc file that includes procedural functions for everything I need, like renderNewsItems($items); renderSubnav($items); etc. In some cases I might create a couple of different include files that have groups of related functions that I can selectively include where needed. Then the template files that need any of these functions just includes functions.inc (or other include file) at the top. This is sometimes the path of least resistance. /site/templates/news-index.php <?php include_once("./functions.inc"); $page->body .= renderNewsItems($page->children); include("./main.inc"); Link to comment Share on other sites More sharing options...
thomas Posted November 28, 2012 Author Share Posted November 28, 2012 Thanks a lot for the answers! Soma, that is just what I was looking for. I have a bunch of modules already, and this will help me put them in context! - thomas 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