Knowledge Base

What’s the difference between “input” and “output” for PHP parsing?

When a Template is rendered for a site, EE goes through several steps.  Among those steps are these three, in this order:

1. PHP parsing on “input”
2. Parsing/rendering the EE tags and variables
3. PHP parsing on “output”

So, when you select input or output, you’re telling EE whether the PHP should be parsed before or after EE parses and renders the various EE tags and variables.

That’s all well and good, but what’s the point?  How is this useful?  For that, let’s try two examples, one showing how PHP can be used successfully at each stage:


INPUT

For the input stage, let’s assume that we’re taking a PHP variable and we want to use it as the value for a parameter inside one of our EE tags.

<?php

$weblog
= "myweblogname";

?>
{exp
:weblog:entries weblog="<?php echo $weblog; ?>" limit="10"}

...content...

{/exp:weblog:entries}

Now, because PHP is parsed before EE does anything with EE tags and variables, when EE gets to that step, it sees this:

{exp:weblog:entries weblog="myweblogname" limit="10"}

If you need to use the same weblog name in a lot of different places within the same Template, then this could be useful for you.  Set the variable in one place and if you ever change it all the places you use it are automatically changed.


OUTPUT

What if you want to conditionally display something based on the content of an EE weblog field or maybe a global variable.  Let’s say you have a field called “private” in one of your weblogs.  You want to check the value of that field and conditionally display content depending on what the value is.

In order for PHP to even be able to see the content, the EE tags and variables have to be parsed first and thus PHP must be parsed on “output” so that it is after the EE tags and variables.

{exp:weblog:entries weblog="privatejournal"}
<?php

$ifprivate
= "{private}";

if (
$ifprivate == "private")
{
?>
  {if logged_in}

    
<h2>{title}</h2>

    
{body}

  {
/if}
  {if logged_out}

    You must be a member
and logged in to view this entry.

  
{/if}
<?php
}
else
{
?>

  
<h2>{title}</h2>

  
{body}

<?php
}
?>
{
/exp:weblog:entries}

This one wasn’t, perhaps, the most functional example, but it does illustrate how PHP would have to be set to “output” in order to be able to view the contents of the {private} weblog field.

Last Updated on Feb 27, 2007 at 10:15   ( Permalink )
Category: Templates, PHP