We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Accessing global variables and such from plugins

Development and Programming

jlundell's avatar
jlundell
18 posts
16 years ago
jlundell's avatar jlundell

How does one access global variables from plugin php code? I figure there must be an array of them somewhere, but I can’t find it.

       
Derek Jones's avatar
Derek Jones
7,561 posts
16 years ago
Derek Jones's avatar Derek Jones

Can you be more specific on which ‘global variables’ you’re specifically after? Application/PHP global variables and objects, EE user defined template variables, path.php global variables, etc.

       
jlundell's avatar
jlundell
18 posts
16 years ago
jlundell's avatar jlundell

My immediate need is to generate the equivalent of {path='x/y'} from the plugin. It’d also be nice to access user-defined global and template variables, but that’s not what I need today.

       
Derek Jones's avatar
Derek Jones
7,561 posts
16 years ago
Derek Jones's avatar Derek Jones

Path variables are never stored in memory, they are simply parsed and replaced along with other user-defined template globals at the very end of template parsing. If your plugin uses its own path variables, have a look at $FNS->extract_path() and $FNS->create_url(). Post back if you need additional help.

       
jlundell's avatar
jlundell
18 posts
16 years ago
jlundell's avatar jlundell

That’s actually what I was looking for, thanks, and I’m sorry for not phrasing it more clearly.

So now I have a working plugin, and that leads to to my second question. The plugin functions fine from a template, but not when I invoke it from a weblog field. No doubt that’s just the way it works, but I’m wondering how to do something that way.

       
Derek Jones's avatar
Derek Jones
7,561 posts
16 years ago
Derek Jones's avatar Derek Jones

When used as a field formatting plugin, the contents of the field are passed to the plugin as the first argument. So your plugin must accept the string. Here’s a common method:

function foo($str = '')
{
    global $TMPL;
    $str = ($str == '')  ? $TMPL->tagdata : $str;
    ...
}

Essentially, if a string is passed, you want to use it, otherwise, you’ll use the tagadata.

       
jlundell's avatar
jlundell
18 posts
16 years ago
jlundell's avatar jlundell

Thanks, and I can see some interesting uses for that approach, but I can also see that I’m still not being clear about what I’m trying to do. Let me back up for a moment.

I’m using the Structure module to create a set of static pages. They all share a common template, and they get their content from five or six top-level weblogs, one for each major group of pages (parent and children and grandchildren).

A weblog feeding these static pages has a few fields, including an h1 title and a body-text field.

I also have a glossary (template and weblog) that glosses a set of technical terms used throughout the site. I want to gloss terms that appear the body-text field I’m referring to above. To gloss a term, I can use something like this:

<a class='gloss'path="resources/glossary">Some Term</a>

(I hope that comes through OK. I wish this forum had a preview function. I’ll repost if necessary.)

That’s awfully clumsy to type for every term I want to gloss, so I wrote a plugin that accepts this format and translates it to the above:

{exp:gloss}Some Term{/exp:gloss}

It works fine if I stick it in a template, but that doesn’t do me any good, since my body text is coming from a weblog field. But if I code it into a field, the parser ignores it, and I see the tags instead.

I can see one approach, namely to code my glossary references as (say)

<gloss>Some Term</gloss>

and then pass the whole field to a plugin that translates that pattern to the one I actually want.

An aside: there’s a ‘parse order’ page in the wiki that would be <strike>very</strike> even more useful if it were a little more complete. For example, it doesn’t show plugins at all.

       
Derek Jones's avatar
Derek Jones
7,561 posts
16 years ago
Derek Jones's avatar Derek Jones
(I hope that comes through OK. I wish this forum had a preview function. I’ll repost if necessary.)

I edited it for you, no worries. On these forums, you can use [code][/code] for code samples. And there is a preview, if you use the Post Reply as opposed to the Fast Reply form.

It works fine if I stick it in a template, but that doesn’t do me any good, since my body text is coming from a weblog field. But if I code it into a field, the parser ignores it, and I see the tags instead.

Ok I think I understand now - you have the plugin tag inside the entry itself. Content in entries is protected so that EE tags and PHP are not parsed. There’s a plugin “Allow EE Code” that will circumvent this for you, but I don’t personally recommend it unless you’re the only author of content on this site.

I can see one approach, namely to code my glossary references as (say)
<gloss>Some Term</gloss>
and then pass the whole field to a plugin that translates that pattern to the one I actually want.

Yep, that would work, or even some pseudo markup like [gloss]foo[/gloss]. Instead of a plugin, however, you’d probably want to code it as an extension of the Typography parser, so that they are always handled when text is ran through Typography. If you search the forums for a Footnotes extension, or a YouTube extension you’ll see some existing samples that do something quite similar.

An aside: there’s a ‘parse order’ page in the wiki that would be <strike>very</strike> even more useful if it were a little more complete. For example, it doesn’t show plugins at all.

Plugins are included under:

# Main Parsing - Process all {exp:…} tags in order encountered.

Hope that helps!

       
Lisa Wess's avatar
Lisa Wess
20,502 posts
16 years ago
Lisa Wess's avatar Lisa Wess

If I may add:

I also have a glossary (template and weblog) that glosses a set of technical terms used throughout the site. I want to gloss terms that appear the body-text field I’m referring to above. To gloss a term, I can use something like this:

You might consider the Acronym Module from Solspace.com, it seems like that might fit the bill unless there are other features you are after.

       
jlundell's avatar
jlundell
18 posts
16 years ago
jlundell's avatar jlundell

Thanks, yes, Acronym is very close to what I’m after. I think I want an explicit, browsable glossary page (or set of pages), though, to which end Acronym could no doubt be adapted. I’ll play around with it.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.