I am confused as to how to get a plugin to return data in its tags.
I have a plugin that takes file path and (is supposed to) returns the width and height of the image referenced.
I only have the constructor, so here is my code:
$tagdata = $TMPL->tagdata;
if ($tagdata)
{
foreach ($TMPL->var_single as $key => $val)
{
if ($val == "width")
{
echo "in width";
$tagdata = $TMPL->swap_var_single($val, $width, $tagdata);
}
if ($val == "height")
{
echo "in height";
$tagdata = $TMPL->swap_var_single($val, $height, $tagdata);
}
}
return $tagdata;And when I try it out in my template:
{exp:image_properties src="http://thedalzells.org/photos/September/IMG_2669.JPG" }
height is {height}; width is {width}
{/exp:image_properties}No data is returned.
What am I doing wrong?
K, now I am getting one value back, but with both tags in the template {width} and {height} I would expect to see both values.
As I borrowed this code, I don’t really understand it…can some one point me to a good example that does a similar thing? Take in one param then output some tag data (but no HTML)?
EDIT: in fact, I am not seeing the text in my tags, I am only seeing some output from the plugin.
Sigh…so confused.
I am finding the documentation not quite complete, which makes this a confusing process
Emphasis on the part from where I pointed that answers the question. I’m not sure how it could be more complete:
“Your new class is useless unless it can return a value. There are two ways to return a value depending on whether your tag has three segments or two…The above tag only has two segments therefore it only utilizes a constructor. Since constructors cannot return a value directly, we will assign it to a variable called: $return_data.”
The example that follows those statements shows how to use it correctly. Are you setting $this->return_data ?
$this->return_data = $tagdata;OK, I got it to work, but I can’t say I completely understand how the whole tagdata thing works, nor do I completely understand swap_single_var as the docs aren’t complete for it.
I was also thrown off by the fact that no where does it say that $return_data is the mandatory way to do it. Perhaps I am just being picky with the english, but the phrase, “Since constructors cannot return a value directly, we will assign it to a variable called: $return_data.” doesn’t tell me that I MUST have a variable called that.
Regardless, thanks for your help it is much appreciated.
I understand what you’re saying, Erin. Part of the hurdle you are crossing seems to also be that your PHP Fu is still in its infancy as well. Stick with it, it will eventually gel and you can get your blue belt.
Explanation of $TMPL->tagdata $TMPL->swap_var_single() explained
I would heartily recommend opening core.template.php and looking at the swap_var_single() method, and it should make sense right away. Most of EE’s code is self documented and the fastest way to learn is to just examine it directly. You certainly don’t have to use that method to replace your variables, it just makes it a bit easier for you, not having to worry about the brackets, and of course, if in some alternate dimension we ever change variable syntax in ExpressionEngine, using that method would automatically work instead of having to modify your add-on.
Ya, I would agree…my PHP is extremely weak…but my C, C++ and Java used to be excellent!!!
I did see both of those sections, but for the swap_var_single, I was expecting a function spec or something. As for the tagdata, I guess I just didn’t get it upon initial reading…and I am still a little confused. I must admit, I don’t usually think about looking at the source as I am used to dealing with closed libraries and function docs.
I do complete understand using the EE functions instead of doing it myself…hence my other questions in the plugin forum about how to do specific things.
Thanks again for your patience.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.