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

Problem using expression engine tags as parameters for a php function

Development and Programming

gabe's avatar
gabe
63 posts
16 years ago
gabe's avatar gabe

Hey,

So I have placed a working php script at the beggining of an included template. The aim of the script is to build vcs files on the fly based upon data stored in a weblog entry. I call it in this way:

<li>{event_location} {city}<?=CreateVcs('Test title', 'test description', "{event_location}", time(), '6:30pm');?></li>

The test data is passed as expected but the value for {event_location} doesn’t get passed to the php function. I have placed the {event_location} tag before the php function just to make sure that it does have a value.

I changed php parsing from input to output. When it was input the the actual text {event_location} got passed to the php function instead of the value it contains. When I change it to output nothing is passed to the function.

So I guess this is to do with the rendering order but I am not sure how to troubleshoot it. Any ideas anyone?

Moved to Plugins: Technical Assistance by Moderator

       
Sue Crocker's avatar
Sue Crocker
26,054 posts
16 years ago
Sue Crocker's avatar Sue Crocker

gabe, how are you parsing PHP in the EE template? Input or output? If you’re in an exp:weblog:entries loop, use output.

       
gabe's avatar
gabe
63 posts
16 years ago
gabe's avatar gabe
gabe, how are you parsing PHP in the EE template? Input or output? If you’re in an exp:weblog:entries loop, use output.

If it is input then I get the {event_location} passed as a string instead of the value. If I use output then I get a blank value.

It is within a exp:weblog:entries loop and I concluded that ouput is the right way to do it, but I can’t seem to get any value from the {event_location} variable tag this way.

       
Sue Crocker's avatar
Sue Crocker
26,054 posts
16 years ago
Sue Crocker's avatar Sue Crocker

Ignoring the PHP for right now.. are you getting what you expect when you echo out {event_location} in your exp:weblog:entries loop?

You may want to consider writing a plugin to get back what you were going to use the PHP code for.

       
gabe's avatar
gabe
63 posts
16 years ago
gabe's avatar gabe
Ignoring the PHP for right now.. are you getting what you expect when you echo out {event_location} in your exp:weblog:entries loop? You may want to consider writing a plugin to get back what you were going to use the PHP code for.

Yes in my exp:weblog:entries loop {event_location} current displays London which is the correct value.

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
16 years ago
Pascal Kriete's avatar Pascal Kriete

Can you confirm that the PHP function is getting called at all? How are you including the function?

       
gabe's avatar
gabe
63 posts
16 years ago
gabe's avatar gabe
Can you confirm that the PHP function is getting called at all? How are you including the function?

Yes the php function is getting called. The validation writes to a file and returns

<a href="http://foo.vcs">Add to outlook</a>

This all functions as expected but the expression engine tag parameter doesn’t get passed. The php function sits at the top of the php enabled template file.

       
Pascal Kriete's avatar
Pascal Kriete
2,589 posts
16 years ago
Pascal Kriete's avatar Pascal Kriete

Hmm, ok, let’s reduce this a bit. Can you try this for me, please:

<?php
$test = "{event_location}";
echo $test;
?>

Put that into the same spot and see if you get any output at all.

       
gabe's avatar
gabe
63 posts
16 years ago
gabe's avatar gabe
Hmm, ok, let’s reduce this a bit. Can you try this for me, please:
<?php
$test = "{event_location}";
echo $test;
?>
Put that into the same spot and see if you get any output at all.

Yes that snippet echos the expected value. So {event_location} works in this situation.

If I echo out the value of the function’s $location parameter I get the value:

<?php
function CreateVcs($event_title = null, $event_description = null, $location = null, $event_datestart = null, $event_time = null) {
  echo $location
}

However when it gets written to a text file it reverts to {event_location} which is very strange. It even does this if I assign another variable to it first which is completely bemussing. I have deleted all created files to make sure that it is not reading a previous file.

       
Greg Aker's avatar
Greg Aker
6,022 posts
16 years ago
Greg Aker's avatar Greg Aker

Gabe:

What if you assign the variable like:

<?php $location = "{event_location}"; ?>
<li>{event_location} {city}<?=CreateVcs('Test title', 'test description', $location, time(), '6:30pm');?></li>
       
gabe's avatar
gabe
63 posts
16 years ago
gabe's avatar gabe
Gabe: What if you assign the variable like:
<?php $location = "{event_location}"; ?>
<li>{event_location} {city}<?=CreateVcs('Test title', 'test description', $location, time(), '6:30pm');?></li>

Right the tag value does in fact get passed to the php function but it reverts to {event_location} at the point which it writes to a file. I have broken this code right down for clarity:

<?php $location = "{event_location}";?>
{event_location} - Returns London

<?php echo CreateVcs($location); ?>

<?php
function CreateVcs($location) {        
        
        echo $location; // returns London

        $content .= "LOCATION;ENCODING=QUOTED-PRINTABLE: $location \n";
        
        echo $content; // returns LOCATION;ENCODING=QUOTED-PRINTABLE: London

//NOW WE WRITE THE VCS FILE TO THE SERVER. IF THE FILE EXISTS WE OVERWRITE IT. - Added some checking
        if (($fp=@fopen($filename, "w")) === FALSE) {
                return null;
        } else {

                //NEXT WE WRITE THE CONTENT INTO THE FILE
                if (@fputs($fp, $content) === FALSE) { @fclose($fp); return null; }
                // Writes LOCATION;ENCODING=QUOTED-PRINTABLE: {event_location} to $filename

                //FINALLY WE NEED TO CLOSE THE FILE
                @fclose($fp);
                
                $output = "<a >Add to Outlook</a>"; 
                return $output; 
       }
}

In this scenario the vcs file created contains LOCATION;ENCODING=QUOTED-PRINTABLE: {event_location} instead of picking up the value. When I use an fputs it suddenly reverts to the actual string {event_location} instead of using its value. However it doesn’t do this with the variables $location or $content. I can’t see why it would be any different.

       
Sue Crocker's avatar
Sue Crocker
26,054 posts
16 years ago
Sue Crocker's avatar Sue Crocker

gabe, debugging PHP code is a bit more than we do in regular Technical Support. I’m going to move this to a more appropriate forum where members of the community can chime in.

       

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.