Jump to content

Strange behavior


Nick Belane
 Share

Recommended Posts

Hi everybody!

The following code is an include in different templates and works fine:
 

<?php if ($page->id != 1) { ?>
<div class="container">
	<div class="breadcrumbs hidden-xs hidden-sm" role="navigation" aria-label="<?= __('Sie sind hier'); ?>:">
		<span class="blue"><?= __('Sie sind hier'); ?>:</span>
		<?php
			foreach($page->parents() as $item) {
				echo "<span><a href='$item->url'>$item->title</a></span>"; 
			}
			echo "<span>$page->title</span>"; 
		?>
	</div>
</div>
<?php } ?>

 

But if I remove the first if-statement "$page->id != 1" it throws an error:
"Uncaught Error: Call to undefined function __()"

Probably it has to do with the foreach loop.
I am not the best in PHP :-)

Thanks in advance

 

Link to comment
Share on other sites

Assuming you're on >PW v3.0 It's most likely a namespace issue. Before if statement open a new PHP tag (<?php ?>) and specify namespace like so

<?php namespace ProcessWire; ?>
<?php if ( /* ... */) { ?>

 

Link to comment
Share on other sites

@abdus is absolutely right. Just to expand a little, what is happening when you do that is saying to PHP 'in this file the variable "item" to be treated as "ProcessWire\item" (for example)'. That means that if you were to find some code on e.g. GitHub that you wanted to include(), so long as it is namespaced, it can use an "item" variable (which will be treated as e.g. "ClassName\item") and can't conflict with your "item" variable.

I'm currently developing a personal site that uses an image processing library I found on GitHub and it was fairly straightforward (even to a thickhead like me), to sort out the namespacing stuff, thus ensuring no naming conflicts.

FWIW I thought namespacing was an unnecessary faff when Ryan first started on PW 3.n, but having seen the benefits, and considering the only downside is one extra line of PHP at the top of every file, it's not really worth leaving it out.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...