Nick Belane Posted August 24, 2017 Share Posted August 24, 2017 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 More sharing options...
abdus Posted August 24, 2017 Share Posted August 24, 2017 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 More sharing options...
Nick Belane Posted August 24, 2017 Author Share Posted August 24, 2017 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? Link to comment Share on other sites More sharing options...
abdus Posted August 24, 2017 Share Posted August 24, 2017 It needs to be set on every template file. Although there's template compiler that does it for you, I am not satisfied with how it performs. Here's the relevant blog post: https://processwire.com/blog/posts/processwire-3.0.14-updates-file-compiler-fields-and-more/#file-compiler-updates 3 Link to comment Share on other sites More sharing options...
Nick Belane Posted August 24, 2017 Author Share Posted August 24, 2017 "I am not satisfied with how it performs. " Why? Link to comment Share on other sites More sharing options...
DaveP Posted August 24, 2017 Share Posted August 24, 2017 @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. 1 Link to comment Share on other sites More sharing options...
Nick Belane Posted August 24, 2017 Author Share Posted August 24, 2017 Thank you DaveP! This was really the first good explanation for the namespace-stuff for me as a newbie... Great community here! Link to comment Share on other sites More sharing options...
DaveP Posted August 24, 2017 Share Posted August 24, 2017 Just trying to save you the head scratching I had to go through to get even that kind of rudimentary understanding! 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now