Jump to content

Recommended Posts

Posted

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

 

Posted

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 ( /* ... */) { ?>

 

Posted

Ah, thank you. Now it works.
But I don't get it. I thought the namespace must defined on the top of the page. Just one time.
I have set this in the header include.

Do I have to set the namespace in every include?

Posted

@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
Posted

^-^Just trying to save you the head scratching I had to go through to get even that kind of rudimentary understanding!

  • Like 2

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
  • Recently Browsing   0 members

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