“global” keyword not supported in EE templates?
Posted: 28 February 2008 11:26 AM   [ Ignore ]  
Summer Student
Total Posts:  23
Joined  05-21-2006

In one of my EE templates, I’ve included a script using

<?php include() ?>

.  When using an EE template, some of the scripts functionality stops working.  I created a standalone PHP file with the same exact code as the EE template and everything works fine. 

To the best of my debugging ability, I’ve narrowed the broken functionality to the use of

global $var1, $var2, $var3;

For example, the basic structure of the script looks like this:

$var1 = "hello";

function
foobar($item) {
   
global $var1;
   
   if(
$var1 == 'hello') {
      
// This code never gets executed within an EE template
      // because it seems like the previous call to "global $var1;"
      // doesn't get executed properly within an EE template.
   
}
}

I hope I’ve explained this clearly enough.  The script I’m trying to use can be found here.

Thanks,
-Alex

P.S.  On a random side note, any ideas on implementing “tell-a-friend, evite” functionality within EE?  The Freeform module from Solspace doesn’t seem to support the functionality which is why I’m using the script above (with minor modifications).

Profile
 
 
Posted: 28 February 2008 11:47 AM   [ Ignore ]   [ # 1 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  25625
Joined  05-20-2002

Hm- before I start poking- would the stock Tell-a-Friend form do the trick?

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 28 February 2008 12:02 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  23
Joined  05-21-2006

Interesting.  I didn’t know this existed.  Unfortunately, it is not quite flexible enough for our needs.  I need to be able to send a different invite based on a user-selected dropdown.  And the invite is not based on a specific weblog entry. 

It doesn’t look like the stock Tell-a-Friend will work.  But thanks for the heads up.  This may come in handy in the future.

Profile
 
 
Posted: 28 February 2008 01:41 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  23
Joined  05-21-2006

To try and remove any potential sources of error, I removed the script from the equation and did a simple test. 

<html>

<
body>

<?php

$var1
= "test";

function
foobar() {
    
global $var1;
    
    if(
$var1 == "test") {
        
echo "<h3>Hello World!</h3>";
    
}
}

foobar
();

?>

</body>

</
html>

I put the above block of code in an EE template and one in a standalone PHP file.  The EE template did _not_ output anything whereas the standalone PHP file worked as expected.

Profile
 
 
Posted: 04 March 2008 09:30 AM   [ Ignore ]   [ # 4 ]  
Summer Student
Total Posts:  23
Joined  05-21-2006

Anybody?  wink

Profile
 
 
Posted: 04 March 2008 10:02 AM   [ Ignore ]   [ # 5 ]  
Moderator
Avatar
RankRankRankRankRankRankRank
Total Posts:  25625
Joined  05-20-2002

Check out this thread and the linked kblog entry.  PHP in templates is processed via the eval()- thus you’re running into a scope issue in the above.  See if that helps.

And I’d probably look at Freeform again- there are hooks in there and I suspect it would be cleaner to try and make use of that rather than a pure custom code.  If I was custom coding it- I’d shoot for making it an EE plugin.

 Signature 

AKA rob1

Help Request TipsPro Network

Profile
 
 
Posted: 04 March 2008 12:53 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  23
Joined  05-21-2006

Aha!  The KB article you linked to was a winner.  Thanks. 

I will eventually need a more robust solution, so I will take a look at the Freeform hooks a bit later on.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1743, on December 02, 2009 03:47 PM
Total Registered Members: 120423 Total Logged-in Users: 65
Total Topics: 126520 Total Anonymous Users: 56
Total Replies: 665279 Total Guests: 366
Total Posts: 791799    
Members ( View Memberlist )