Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-04-26 11:09:42

phual
Member
From: UK
Registered: 2008-02-19
Posts: 26
Website

Integrating software - a variable scope problem

I’ve been working to get Plogger (gallery software) working with Textpattern, and whilst making progress, keep hitting problem after problem. My latest I believe is the result of PHP not being executed in the global scope (which I only vaguely understand from <a href=“http://textpattern.com/faq/34/how-do-i-use-php-code”>this article</a>).

To use plogger, you need to add three lines of code to your page: <?php require("plogger.php"); ?> at the top of the page, <?php the_plogger_head(); ?> in the <head> element, and <?php the_plogger_gallery(); ?> in the <body>. This would give a very simple page as being:

<?php require('plogger.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="<?php echo $language; ?>" lang="<?php echo $language; ?>" xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php the_plogger_head(); ?>
</head>
<body>
<?php the_plogger_gallery(); ?>
</body>
</html> 

Of course, because these are split across the head and body, I’m running into the problem of variables defined in earlier code snippets not being available to the later ones. The following errors are typical results:

Tag error:  <txp:php> ->  Notice: Undefined index:  theme_url  on line 108
Tag error:  <txp:php> ->  Notice: Undefined index:  embedded  on line 467
Tag error:  <txp:php> ->  Notice: Undefined index:  charset  on line 471
_
Tag error:  <txp:php> ->  Notice: Undefined index:  baseurl  on line 1371
Tag error:  <txp:php> ->  Notice: Undefined index:  basedir  on line 595
Tag error:  <txp:php> ->  Notice: Undefined index:  basedir  on line 633
Tag error:  <txp:php> ->  Notice: Undefined index:  baseurl  on line 1371
etc. 

Has anyone got any clever solutions that will allow me to access the variables without having to define a fist-full of variables as global before each call?

Stuart

Offline

#2 2009-04-26 11:39:53

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,453
Website GitHub

Re: Integrating software - a variable scope problem

Yeah, it’s a bit of a pain when integrating stuff like this because you need to define globals for everything and import them every time. First off, use <txp:php>...</txp:php> tags instead of raw <?php ... ?>. It just saves you getting warnings on your page or having to mess around in Advanced Prefs.

The only thing I can think of is to switch the approach to an entire block of PHP and then render the page parts using heredoc or echo statements. e.g:

<txp:php>
   require('plogger.php');

   echo <<<EOHTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="{$language}" lang="{$language}" xmlns="http://www.w3.org/1999/xhtml">
<head>
EOHTML;

   the_plogger_head();
   echo '</head><body>';
   the_plogger_gallery();
   echo '</body></html>';
</txp:php>

It’s untested but it should work. Does that get you going or give you any other ideas?


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2009-04-26 17:46:30

phual
Member
From: UK
Registered: 2008-02-19
Posts: 26
Website

Re: Integrating software - a variable scope problem

Doesn’t more me forward I’m afraid. The example I gave was a very basic example to show how plogger works and is nothing compared to the complexity of my real site, which is several hundred lines of html interspersed with textpattern tags.

Not being a programmer (beyond very simple stuff like ‘hello world’ and tweaking code that exists), I wondered about whether a couple of ideas had any legs. The first was creating a file to temporarily hold the variables, but is this really any simpler than declaring the variables for each of the three segments.

My next revolves around the way plogger seems to get these particular variables. It appears from my untrained eye, that all of the variables referenced in the many errors are defined within an array that operates as

$config['theme_url']=something
$config['base_url']=something else

If this is the case, might I be lucky enough for this to work:

<txp:php>global $config; require('plogger.php');</txp:php>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<txp:php>global $config; the_plogger_head();</txp:php>
</head>
<body>
<txp:php>global $config; the_plogger_gallery();</txp:php>
</body>
</html>

Offline

#4 2009-04-26 18:12:24

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,453
Website GitHub

Re: Integrating software - a variable scope problem

phual wrote:

The example I gave was a very basic example to show how plogger works

Figured as much. Was worth a try though.

are defined within an array that operates as $config['theme_url']=something

In that case your theory about a single global $config; injection should do the trick.

For reference, in case you want to ever call TXP tags from within PHP (sometimes it’s just easier if you have a load of other code around a single tag) you can use the tag’s name as a function:

<txp:related_articles section="blog" limt="6" wraptag="div" />

is the same as:

echo related_articles(array(
            "section" => "blog",
            "limit" => "6",
            "wraptag" => "div"
         ));

and if you have any tags that take no attributes you just pass an empty array: echo site_url(array());.

Sorry I couldn’t help.

Last edited by Bloke (2009-04-26 18:13:39)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB