Search code examples
zend-frameworkloopsskin

LoadSkin loop error


This is my LoadSkin view helper. I use this class to call the CSS files within an XML. See below:

public function loadSkin($skin)
{
    $skinData = new Zend_Config_Xml('./skins/' . $skin . '/skin.xml');
    $stylesheets = $skinData->stylesheets->stylesheet->toArray();

    if(is_array($stylesheets))
    {
        foreach($stylesheets as $stylesheet)
        {
            echo $this->view->headLink()->appendStylesheet('/skins/' . $skin . 
                    '/css/' . $stylesheet);
        }       
    }
}

But i have one problem, i have 3 CSS files:

  • default.css
  • text.css
  • form.css

But the loop is wrong. First he takes the default.css. After he takes default.css and text.css. And finally he takes default.css, text.css and form.css. But i need only one loop working correctly.

Look below what's happen:

https://i.sstatic.net/P0iBA.png

Someone can help with this?


Solution

  • That's because you are echoing inside foreach loop . HeadLink view helper is container in each iteration of loop you had appended/added css int it , as a result in each echo you are echoing the whole container which contains all the previous added css hence do this instead

     if(is_array($stylesheets))
        {
            foreach($stylesheets as $stylesheet)
            {
                 $this->view->headLink()->appendStylesheet('/skins/' . $skin . 
                        '/css/' . $stylesheet);
            }       
        }
    

    Then in your layout.phtml inside do

    <head>
    <?echo $this->headLink() ?>
    </head>