开发者

CakePHP: Using a variable on the layout when user is changing from page to page

开发者 https://www.devze.com 2023-02-09 23:56 出处:网络
I\'ve set a variable so it can be used from the controller to pass the value \"current\" to where the user browses to. It\'s working fine but I need a conditional programming to compensate with other

I've set a variable so it can be used from the controller to pass the value "current" to where the user browses to. It's working fine but I need a conditional programming to compensate with other pages. Let's say I browse to the page category, the category controller will pass the variable $current to my layout but hell went wrong we will have two links with that special id, e.g.

<li id="<?php echo "$current";?>"><?php echo $this->Html->link('Home', '/'); ?></li>    
<li id="<?php echo "$current";?>"><?php echo $this->Html->link('Category', '/categories'); ?></li> 

I know a conditional required, can someone write that up? I'm not sure if I can use if else, because the other pages claim undefined variable. This is a bit tricky, maybe someone can do an ole working programming logic for this one?

UPDATE: Hi I'm trying to make this work:

<?php if($current==NULL) {
                    <?php echo "<li id="current">";?>
                    <?php echo $this->Html->link('Home', '/'); ?>
                    <?php echo "</li>"; ?>
                    }
<?php else: ?>{
                    <?php echo "<li>";?>
                    <?php echo $this->Html->link('Home', '/'); ?>
                    <?php echo "</li>"; ?> 
              }
<?php endif ?>

I am getting an error on parsing right at the <li> tags. Does anyone know how to wrap up a <开发者_如何学C;li> tag in PHP ?

P.S. I've tried all suggested possible solutions below, couldn't get it work for me. I thought this one currently I'm trying seem to be the easiest? thanks.

I'm not sure it will work though even after wrapping up the tags in PHP.


If the other replies are correct, that you are trying to highlight the current page in the navigation, check out the following helper: http://www.richardathome.com/blog/cakephp-smarter-links

It extends the built in link helper to add two bits of functionality:

-Links should automatically add a class to themselves when they are a) pointing to a resource in the same controller and b) pointing to themselves.

-Able to show/hide themselves.

The helper works fine, I've used it on a couple of projects.

In response to update

If that is your actual PHP, there are a couple of formatting errors. This line is invalid:

<?php echo "<li id="current">";?>

You need to escape the inner quotes, like so:

<?php echo "<li id=\"current\">";?>

But your braces are also a bit out of whack. Try the following:

<?php 
if($current==NULL) {
    echo "<li id=\"current\">";
    echo $this->Html->link('Home', '/');
    echo "</li>";
}
else {
    echo "<li>";
    echo $this->Html->link('Home', '/');
    echo "</li>";
}
?>

Update 2

As mentioned in the comment, I suspect $current is simply never actually NULL. In PHP an empty string is not NULL. I suggest instead of testing for NULL, test for emptiness.

The empty() function will return true if a value is null, 0, false, "", or an empty array. See here: http://au.php.net/manual/en/function.empty.php

So your code becomes:

<?php 
if(empty($current)) {
    echo "<li id=\"current\">";
    echo $this->Html->link('Home', '/');
    echo "</li>";
}
else {
    echo "<li>";
    echo $this->Html->link('Home', '/');
    echo "</li>";
}
?>

Or even better (simply less duplication):

<?php 
if(empty($current)) {
    echo "<li id=\"current\">";
}
else {
    echo "<li>";
}
echo $this->Html->link('Home', '/');
echo "</li>";
?>


I'm not sure of CakePHP specifics, but you'd need something to the effect of:

$current = 'category';  // in controller

if($current=='category') { echo 'current' } // in view

for each link.

If it isn't already built in.


I guess you're trying to build a menu where the current page on the menu is styled differently? If this is not the case then my answer will need adapting to your particular case.

If this is the case, then Ross is quite correct, although I'd probably write it slightly differently to suit my personal aesthetics:

<li class="<?php ($current=='category') ? echo 'active' : echo 'inactive' ?>">

Note also that I used class rather than id as this would be the better way to write up the CSS. If you don't recognize the syntax, it's the ternary operator - a kind of shorthand for a conditional 'toggle'.

Edit: There are some problems with logic ($current==null??), syntax, style and efficiency. I hope I have corrected these below.

Unless a string contains a variable or special character (newline etc.) you should use single (') quotes. PHP parses double (") quotes more slowly. Your use of double quotes in the first <li> line was syntactically wrong.

You had braces outside of <?php ... ?> blocks.

The whole code block can be enclosed in one pair of <?php .. ?>.

<?php
if($current) 
    {              
        echo '<li id="current">';
        echo $this->Html->link('Home', '/');
        echo '</li>';
    }
else
    {
        echo '<li>';
        echo $this->Html->link('Home', '/');
        echo '</li>';
    }
?>


Are you trying to make a nav that can automatically know which page the user is on, and highlight the navigation (tab?) accordingly?

If that is the case, then I have some thoughts for you.

If you want the $current variable to be set for all controllers, then you probably want to put that in the app_controller. You may not have one, it isn't in the cake deployment by default. You would add it at CAKE_HOME/app/app_controller.php

it would look something like

<?php
class AppController extends Controller {
  function beforeFilter() {
    $this->set('current', $this->name . '.' . $this->action);
  }
}
?>

The alternative, which is something that I do in my apps, is to not use the variable $current, but to use the built in variables $this->name (the controller name) and $this->action ( the action name )

<?php $tmp = $this->here . '/'. $this->action; ?>
<li class="<?php ($tmp == '/') ? e('active-tab') : e('tab'); ?>">
    <?php echo $this->Html->link('Home', '/'); ?>
</li> 
<li class="<?php ($tmp == '/categories/index') ? e('active-tab') : e('tab'); ?>">
    <?php echo $this->Html->link('Category', '/categories'); ?>
</li> 

The benefit is that you don't have to explicitly set the $category variable in each controller action. All the display logic is in the layout or views, where they should be. I like anything that lets me do less coding.

0

精彩评论

暂无评论...
验证码 换一张
取 消