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

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:

Parse error: parse error in /path/to/forum/includes/template.php(127) : 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 they have not recently altered this file. Before you can learn how to fix this error, you must first come to understand what it means.

Understanding the Error

The key to this error is the later part of the message: eval()'d code on line XX. By including this, the software is trying to tell you something specific about the problem that was encountered. It is saying that the cause of the error is not in template.php itself, but some code from another file that is being processed in template.php.

To understand where this error originates, first you need to know something about how template.php works. A large part of this is reading .tpl template files in and storing the contents of those files in variables. The variables are then sent through compiling, a process which replaces template variables like {L_INDEX} with a value and adds special commands for displaying the page. After being compiled, the variables might contain something similar to this:

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 does this by parsing a template. Parsing a template actually means that the variables created from a template file are evaluated - or eval()'d, as the error message says. So, the cause of the error is actually in one of the template files used by the page that displays the error.

Locating the Error

So how do you fix this error? Since the error message does not tell you which template file causes the problem, you need to check each file used by the page showing the error. The first step is to compile a list of all the template files the page uses. Here is a brief checklist that can narrow the suspects down considerably.

  1. Has the error started to appear after editing some template files?
    If so, one of those files causes the error.
  2. Does the error appear on every page of the forum?
    Only two template files are used on every single page. These are overall_header.tpl and overall_footer.tpl. Some modifications may add the use of extra template files; if so, these files should be mentioned by name somewhere in includes/page_header.php or includes/page_tail.php.
  3. Does the error appear in the Admin Panel?
    When this happens, one of the template files in the admin folder of your template is the cause. If the error is on every page of the Admin Panel, check page_header.tpl and page_footer.tpl, the Admin Panel versions of the overall template files.

When this checklist doesn't help you locate the file causing the problem, you will need to check through the .php file of the page displaying the error for names of template files. Look for the line below. Usually, there will be some nearby lines that name template files used by the page. This may happen in several places in the file, so be sure to check thoroughly.

$template->set_filenames(array(

So now that you've found the template 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 template file, but this is often not the case. The line number refers to the evaluated code, which can often have many more lines than the template file. You can't rely on the line number in the error message, so instead you'll have to look through the template file manually to find the problem.

Repairing the Cause of the Error

There are a few common errors that you might find while examining the template files. Let's look at the errors that are found most often and how to fix them.

First, check for badly formed template variables. Template variables are words, usually in all uppercase letters, surrounded by curly brackets, such as {USERNAME} or {L_INDEX}. Sometimes, one of the curly brackets might be missing, resulting in something like {USERNAME or USERNAME}. That creates a parse error that can be fixed by adding the missing bracket.

Template switches are another potential source of problems. Template switches are a special feature phpBB uses for loops and conditional display of page elements. They look like HTML comments, but have a few specific rules to separate the two. Here are some examples of template switches.

<!-- 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 "loop switch."
	Loop switches are used for displaying repeating
	blocks of content, like posts in topics.
<!-- END postrow -->

Earlier, I mentioned that template switches have certain rules that separate them from normal HTML comments. Breaking these rules results in parse errors, so you need to check your template file for any switches that do not follow the rules. As a side note, the eXtreme Styles modification loosens these rules and will prevent most parse errors caused by template switches.

The first rule is, "Each opening and closing switch line must be on a line containing no other code." Any of the example switch usages below would cause a parse error. Did you notice, in the example above, that each switch has a BEGIN line, one or more lines of content, and then an END line? That is the correct way to write a switch.

<!-- 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 -->

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 allowed characters, exactly one space, and end with -->. Almost all characters other than spaces are allowed in switch names. It's easy to leave out or add extra spaces; since switches look so much like HTML comments, they might still look valid to your eye. These are not valid switch statements, though:

<!-- 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 -->
 
<!-- BEGIN  bad_switch -->
	Two spaces between BEGIN and bad_switch.
<!-- END bad_switch -->

Incomplete switches are another guaranteed problem. If a template file has the BEGIN line of a switch, but is missing the matching END line, then that will definitely cause a parse error. The reverse is also true: if there is an END line that doesn't have a BEGIN line, that causes an error. The BEGIN line must also be placed before the END line.

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.

<!-- BEGIN first_switch -->
	<!-- BEGIN second_switch -->
		<!-- BEGIN third_switch -->
			We have 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 -->