Jump to content

Modules with class names? Programming and naming schemes ...


thomas
 Share

Recommended Posts

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

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

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

  • Like 3
Link to comment
Share on other sites

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

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

×
×
  • Create New...