Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 19997

How to Separate PHP Generated List Item - Magento

I have some php generated magento top-links with a screenshot below:

enter image description here

My goal is to separate the "Log In" link and have it floated to the left of the same row.

I am hoping for an efficient way to select the last generated list item element and apply some CSS to it.

The code is below:

<ul class="links pull-right"<?php if($this->getName()): ?> id="<?php echo $this->getName() ?>"<?php endif;?>>
    <?php foreach($_links as $_link): ?>
        <?php if ($_link instanceof Mage_Core_Block_Abstract):?>
            <?php echo $_link->toHtml() ?>
        <?php else: ?>
            <li<?php if($_link->getIsFirst()||$_link->getIsLast()): ?> class="<?php if($_link->getIsFirst()): ?>first<?php endif; ?><?php if($_link->getIsLast()): ?> last<?php endif; ?>"<?php endif; ?> <?php echo $_link->getLiParams() ?>><?php echo $_link->getBeforeText() ?><a href="<?php echo $_link->getUrl() ?>" title="<?php echo $_link->getTitle() ?>" <?php echo $_link->getAParams() ?>><?php echo $_link->getLabel() ?></a><?php echo $_link->getAfterText() ?></li>
        <?php endif;?> 
        <?php if (! $_link->getIsLast()):?>|<?php endif;?>
    <?php endforeach; ?>
</ul>

Any ideas would be greatly appreciated!

Upvotes: 0

Views: 426

Answers (3)

James Cowie
James Cowie

Reputation: 464

Evening All,

Id try and limit the amount of business logic you are adding to your templates. As what you are looking to achieve is custom to this instance of magento I would create a very basic module. I would then look to either implement a new block or just a helper function that will return the data you want.

If your working on the block function ensure your class extends the Magento navigation class. ( Sorry I have not checked what this is ) Then create the action: E.g.

public function getNavigation()
{
     $links     = $this->getLinks();
     $linkArray = array();
     $linkCount = count($links);
     $i;

     foreach($links as $link) {
          if($i == $linkCount) {
              $last = true;
          } else { 
               $last = false;
          }
          $linkArray[] = 'link'   => $link->getLink()->toHtml(),
                         'isLast' => $last 

          $i++;
     }
     return $linkArray();

}

Your block would then have minimal logic applied. Mainly just iterating through the result set.

Hope that makes sense, If not let me know and I will get you what you require.

Upvotes: 1

cimmanon
cimmanon

Reputation: 68329

CSS offers a way to style the last-child of a collection, no tinkering with PHP required.

http://tinker.io/926d2

ul.links.pull-right :last-child {
    margin-left: 2em;
}

Upvotes: 1

Kevin Lynch
Kevin Lynch

Reputation: 24723

I answered a similar question not long ago. This adds the class "last-item" to the last item processed.

<?php list($parent) = split('/', $this->url); ?>
<?php  $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php $count = count($last_articles); $num = 0; ?>
<?php foreach ($last_articles as $article): ?>
<li <?php if($num == $count-1){ ?> class="last-item" <?php } ?>>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php $num++ ?>
<?php endforeach; ?>
</ul>

Upvotes: 1

Related Questions