Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-03-27 11:37:44

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Example code for a plugin that does this...

Hi,

Just wondering if any of ya’ll could give me a few tips on creating a plugin that does something similar to this:

<txp:mah_my_new_plugin>
<p><txp:plugin_replaces_this_tag /></p>
<ul>
<li><txp:plugin_replaces_this_tag_too /></li>
</ul>
<txp:this_tag_also_gets_replaced />
</txp:mah_my_new_plugin>

Can you think of any good plugins that I could look at for learning purposes? Maybe something not overly complex?

In general, does anyone have tips for setting up such a plugin? I mean, I have some good ideas, but it would be cool to look at examples whilst developing. :)

Many thanks in advance!
Cheers,
Micky

Last edited by mhulse (2008-03-27 11:39:19)

Offline

#2 2008-03-27 11:44:19

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Example code for a plugin that does this...

Why not replace those tags in the template itself. Much more efficient?

Offline

#3 2008-03-27 11:49:11

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Example code for a plugin that does this...

In example, any tag could be a function:

function mah_my_new_plugin($thing) {
	return $thing; // basic container plugin. Returns thing.
}
function mah_my_new_tag($atts) {
	extract(lAtts(array(
		'echo'        => 'my text'
	),$atts));
	return $echo; // returns my text
}

It’s hard to tell more. Plugins basically are just functions, classes etc, and in most cases all tags are functions. In example <txp:this_tag_also_gets_replaced /> is parsed to this_tag_also_gets_replaced() that will call the specific function. Else is just php or other nested languages, all that you do is based on what you know about those languages and Textpattern itself.

And as ruud says, if your not planning to release a plugin, then it would be better to replace those tags in the template.

Last edited by Gocom (2008-03-27 11:50:29)

Offline

#4 2008-03-27 15:09:57

hakjoon
Member
From: Arlington, VA
Registered: 2004-07-29
Posts: 1,634
Website

Re: Example code for a plugin that does this...

If you want to replace the innards of the tag you run thing through parse.

function mah_my_new_plugin($atts, $thing) {
                    extract(lAtts(array(
		                        //list of attributs
	                       ),$atts));

	return parse($thing); // run contents of tag through parse() to handfe other tags
}

function plugin_replaces_this_tag($atts) {
              return "I have been replaced";
}

function plugin_replaces_this_tag_too($atts) {
              return "I have also been replaced";
}

function this_tag_also_gets_replaced($atts) {
              return "this one too";
}

if you want the other tags to only be used inside mah_my_new_plugin you can create a global which says it’s running inside the tag

function mah_my_new_plugin($atts, $thing) {
                    extract(lAtts(array(
		                        //list of attributs
	                       ),$atts));
          $GLOBALS['in_mah_new_plugin'] = true;       
	$out = parse($thing); 
          $GLOBALS['in_mah_new_plugin'] = false;     

       return $out;
}

function this_tag_also_gets_replaced($atts) {
            global $in_mah_new_plugin;

             if ($in_mah_new_plugin) {
              return "this one too";
            }
}

something like that. Now everyone can tear apart my samples :)

Last edited by hakjoon (2008-03-27 15:11:28)


Shoving is the answer – pusher robot

Offline

#5 2008-03-27 19:31:26

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Re: Example code for a plugin that does this...

Hi Ruud, Gocom, and hakjoon! Many many many thanks for your quick replies, I greatly appreciate all of your help. :)

ruud wrote:

Why not replace those tags in the template itself. Much more efficient?

Ah, yikes! Sorry if I was not clear… That is what I wanted to do… My code example was meant to be a template code example.

Gocom wrote:

It’s hard to tell more. Plugins basically are just functions, classes etc, and in most cases all tags are functions. In example <txp:this_tag_also_gets_replaced /> is parsed to this_tag_also_gets_replaced() that will call the specific function. Else is just php or other nested languages, all that you do is based on what you know about those languages and Textpattern itself.

Ah, perfect explanation!!! It makes so much more sense now. :)

Basically, I am trying to convert an image/multimedia plugin I wrote for Expression Engine, but the approach for EE plugins is a bit different than TXP… But your explanation now makes things very clear for me. Thank you!

hakjoon wrote:

something like that. Now everyone can tear apart my samples :)

Sweeeeeeeeeet!!!!! Thank you for the example! That rocks!

Oh, man, I am excited! I think my next plugin I hope my next plugin will be helpful for folks… Either way, it will be a great learning experience.

I love txp!

I am sure I will be back in a few hours with more questions. :D

Thanks again all!
Cheers,
Micky

Last edited by mhulse (2008-03-27 19:41:29)

Offline

#6 2008-03-27 23:09:56

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Re: Example code for a plugin that does this...

So, quick question:

What would be the best way to pass variables between methods?

Is it possible to set class variables outside of the plugin methods?

Or, is setting globals the way to go? :)

Thanks again all!!! Much appreciated.

Cheers,
Micky

Offline

#7 2008-03-28 10:12:50

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Example code for a plugin that does this...

mhulse wrote:

What would be the best way to pass variables between methods?

Is it possible to set class variables outside of the plugin methods?

Or, is setting globals the way to go? :)

Meaning between functions etc? In most cases TXP’s core doesn’t use classes nor OOP, but you could do it that way. PHP5 requirement for plugin isn’t that bad afterall, althought txp uses in most cases globals and sometimes some wierd oldschool tactics but they work also. With OOP there is couple good plus points, like the code in most cases is shorter and more up-to-day (or so some say), but it’s harder to write too.

I wonder why only couple plugins use classes or OOP… ;)

Last edited by Gocom (2008-03-28 10:13:48)

Offline

#8 2008-03-28 11:42:16

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: Example code for a plugin that does this...

I don’t really see the advantage of using OOP in TXP. It’s not by definition better than not using OOP.

Offline

#9 2008-03-29 03:17:10

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Re: Example code for a plugin that does this...

Thanks again Gocom and ruud!!!

I will play around with using class variables… Seems to be a pretty decent way of passing data between methods. I will let you all know how it goes.

Many thanks to you all, I greatly appreciate the pro advice.

I will probably be back soon with more q’s!

Cheers,
Micky

Offline

#10 2008-03-29 03:59:22

jm
Plugin Author
From: Missoula, MT
Registered: 2005-11-27
Posts: 1,746
Website

Re: Example code for a plugin that does this...

I wonder why only couple plugins use classes or OOP… ;)

/hides

mhulse, check this out. Basically make your instance global, so you can get and set variables between functions. It’s pretty simple and clean :).

Offline

#11 2008-03-29 04:05:42

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Re: Example code for a plugin that does this...

jm wrote:

mhulse, check this out. Basically make your instance global, so you can get and set variables between functions. It’s pretty simple and clean :).

Oh, nice! I was just starting to scratch my head on how I could instantiate a class from within a plugin… this is a cool example! Thanks!!! :)

Offline

#12 2008-03-29 05:52:10

mhulse
Plugin Author
From: Eugene Oregon
Registered: 2005-01-21
Posts: 200

Re: Example code for a plugin that does this...

Nice! This is an interesting start:

On page template:

<txp:mah_test_plugin path="/path/from/root/">
Path: <txp:mah_replace_this_tag />
</txp:mah_test_plugin>

Plugin:

class Mah_Test_Plugin {
	// Class variables:
	var $atts_path = "";
	// Constructor:
	function Mah_Test_Plugin($atts) {
		extract(lAtts(array(
			'path' => ''
		), $atts));
		$this->atts_path = $path;
	}
	// Class method:
	function doStuff() {
		return $this->atts_path;
	}
}
function mah_test_plugin($atts, $thing) {
	global $instance; // Instantiate global.
	$instance = new Mah_Test_Plugin($atts);
	$out = parse($thing);
	return $out; 
}
function mah_replace_this_tag() {
	global $instance;
	return $instance->doStuff();
}

Kinda seems hackish, but it works. :)

(Note: My host does not have PHP5 installed yet… so I have to use PHP4.)

I will be back later with an update. ;)

Thanks all!
Cheers,
Micky

Last edited by mhulse (2008-03-29 05:53:21)

Offline

Board footer

Powered by FluxBB