Talk martial arts at KarateForums.com

Bookmark and Share

Parse error in template.php: eval()'d code

Parse error in template.php: eval()'d code

Postby Thoul » April 22nd 2004, 5:03 pm

There are a lot of hacks that include instructions for modifying template files. Since every template released can be very different from others and many people customize their installed templates, there are a lot of potential problems that can arise when applying a hack. One of them is an error message like this one:
Code: Select all
Parse error: parse error in /path/to/forum/includes/template.php(XXX) : eval()'d code on line XX


Most people who see this error get very confused, because it seems to say that there is a parse error in template.php, but the hack they were installing didn't call for any edits to that file. :crazy: The key to this error is the text eval()'d code; with this text, PHP is trying to tell us something specific about the problem that was encountered.

To understand what this error really says, first you need to know something about how template.php is used in phpBB. A large part of this is reading tpl files in and storing the contents of those files in string variables. The string variables are then compiled, which replaces template variables like {L_INDEX} with their assigned value and transforms each string variable by adding PHP commands like echo to them. After being compiled, the string variables might contain something similar to this:
Code: Select all
echo '<html></body>';
echo 'I am a phpBB page... or at least I will be!';
echo 'All of this is PHP code that can be executed to print out the actual page.';
echo '</body></html>';


When a portion of phpBB sends text output to the browser, it usually does this by "parsing a template." Parsing a template actually means that the string variables created from a tpl file are evaluated - or eval()'d, as the error message says.

So, now we're getting somewhere. The Parse Error you received doesn't mean there's an error in template.php, but rather that there is an error in the code it tried to evaluate. So how do you fix it? Since the evaluated code originally comes from the tpl files, it's a good bet that there is a mistake in one of the tpl files used on the page to display the message. :D

Now I can hear you asking, which tpl file has the mistake? :bigwink: If the error starting appearing recently, it's likely a tpl file that was recently edited, so that should narrow the possibilities. If the error message is at the top of most pages, then the problem is probably in overall_header.tpl or another tpl file mentioned in includes/page_header.php. When the error is at the bottom of most pages, a good place to check is overall_footer.tpl or other tpl files referenced in includes/page_tail.php. Usually, the error is in the middle of a specific page; when this happens, a relatively easy way to find the possible tpl files is to check through the PHP file of the page displaying the error (viewtopic.php when viewing a topic or post, for example) for the text ".tpl". The names of the tpl files used on this page should end with that text.

So now that you've found the tpl files that might contain the mistake, what is the actual problem in the file? This is where things can get difficult. There are several potential causes of the parse error. If you go back to the error message, you'll see that it says eval()'d code on line XX, where XX is some line number. You may think that the mistake is on this line in the tpl file, but this is usually not the case. Instead, this is the line number of the eval()'d code that created the parse error. The difference is that some tpl files that are used to display pages with multiple repeating blocks of content - memberlist_body.tpl, viewforum_body.tpl, and viewtopic_body.tpl are good examples of these. The tpl files used in the manner may have fewer lines than the eval()'d code created from them. You can't rely on the line number in the error message, so instead you'll have to look through the tpl file(s) manually.

There are a few common problems that you might find while examining the tpl files.
  1. There might be a malformed template variable. Template variables are words, usually in all uppercase letters, surrounded by curly brackets, such as {USERNAME} or {L_INDEX}. Sometimes the parse errors are caused by unmatched curly brackets, like {USERNAME or USERNAME}. Try closing these curly brackets or removing the unmatched bracket to see if this fixes the parse error.
  2. Template switches are another potential source of problems, especially in overall_header.tpl. Template switches look like HTML comments, but they are a little more specific. Here are some examples.
    Code: Select all
    <!-- BEGIN switch_user_logged_in -->
    	This text is displayed only to logged in users.
    <!-- END switch_user_logged_in -->
    
    <!-- BEGIN switch_user_logged_out -->
    	This text is displayed only to logged out users.
    <!-- END switch_user_logged_out -->
    
    <!-- BEGIN postrow -->
    	The postrow switch is a "block switch". Block switches are used for displaying repeating blocks of content, like individual posts.
    <!-- END postrow -->


    Template switches can cause parse errors if they don't follow a general set of rules. If you've installed the eXtreme Styles hack on your board, count your blessings, because it will prevent most parse errors related to template switches.
    1. Each opening and closing switch line must be on a line by itself. Any of these switch usages would cause a parse error:
      Code: Select all
      <!-- BEGIN bad_switch -->I'll break your board!
      <!-- END bad_switch -->
      
      <!-- BEGIN bad_switch -->
      Mwuahahahahahaha<!-- END bad_switch -->
      
      <!-- BEGIN bad_switch -->Kiss it goodbye, sucker!<!-- END bad_switch -->
    2. Switch statements are case and space sensitive. This means they must begin with <!-- followed by exactly one space, the text BEGIN or END in all uppercase, exactly one space, a switch name containing any characters, exactly one space, and end with -->. It's easy to leave out the first or last space; since switches look so much like HTML comments, they might still look valid to your eye. These are not valid switch statements, though:
      Code: Select all
      <!-- BEGIN bad_switch-->
      	Missing space on the first line after bad_switch.
      <!-- END bad_switch -->
      
      <!-- BEGIN bad_switch -->
      	Missing space on the last line before END.
      <!--END bad_switch -->
    3. Incomplete switches are a guaranteed problem. If a tpl file has the BEGIN line of a switch, but is missing the matching END line, then that will definitely cause a parse error. The same is true if the is an END line that doesn't have a BEGIN line. The BEGIN line must also be before the END line.
    4. Switches can be nested, meaning that you can have switches inside other switches. When nested, switches must be closed in the same order that they were opened. The following example shows nested switches that are closed in the wrong order.
      Code: Select all
      <!-- BEGIN first_switch -->
      	<!-- BEGIN second_switch -->
      		<!-- BEGIN third_switch -->
      			We've opened three switches, but tried to close the second one before the third. Welcome to Parse Error, population: you.
      	<!--END second_switch -->
      		<!--END third_switch -->
      <!--END first_switch -->


Hopefully, this explanation will help you find and fix the parse error you've encountered. If you find other causes for your parse error, let us know so we can consider expanding the list of common causes.
User avatar
Thoul
Admin/Webmaster
 
Posts: 18551
Joined: July 30th 2002, 11:30 am
Location: USA

Return to phpBB 2: Troubleshooting

Who is online

Users browsing this forum: No registered users and 0 guests