Themed Folder Classification I am trying a very simple method of getting the theme information via a theme.xml within each theme folder.

What it should do:


Scan the theme directory ../themes
Return the names of all the theme folders (array)
Use the name of the theme folder returned to create a link to search in file_exists()
Find the theme.xml in every folder
Parse every theme.xml with simplexml_load_file()
Return an array ready for output in a foreach()
If !file_exists(), output "theme.xml Not Found in (themename)"


I tried to make this in 2 functions but it would always output the first or last themes info, so this is what I'm doing now, can anyone suggest a way to help separate the function from the output. 

Get available theme folder names function

 public function getAvailableThemes() {
    $path = '../themes';
    $themeDir = glob($path . '/*' , GLOB_ONLYDIR);
    return $themeDir;
}


Output in wcx_themes.php file

    &lt;?php 
    $themeDir = $backend-&gt;getAvailableThemes(); 
    foreach($themeDir as $themeDir) {
        $preview = $backend-&gt;getThemePreview($themeDir);
        $xmlfile = $themeDir.'/theme.xml';
    if(file_exists($xmlfile)) {
        $xml = simplexml_load_file($xmlfile);
        $themename = $xml-&gt;themename;
        $themedescription = $xml-&gt;themedescription;
        $themeversion = $xml-&gt;themeversion;
        $themeauthor = $xml-&gt;themeauthor;

    echo '&lt;div class="themewrap"&gt;
    &lt;div class="themepreview"&gt;
    &lt;img src="'.$preview.'"/&gt;
    &lt;/div&gt;
    &lt;div class="themeinfo"&gt;
    &lt;div class="themename"&gt;Name:&amp;nbsp;'.$themename.'&lt;/div&gt;
    &lt;div class="themedescription"&gt;Description:&amp;nbsp;'.$themedescription.'&lt;/div&gt;
    &lt;div class="themeversion"&gt;Version:&amp;nbsp;'.$themeversion.'&lt;/div&gt;
    &lt;div class="themeauthor"&gt;Author:&amp;nbsp;'.$themeauthor.'&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="themebar"&gt;
    &lt;div class="themebarname"&gt;'.$themename.'&lt;/div&gt;
    &lt;div class="themebutton"&gt;
    &lt;a href="../controllers/themecontrol.php"&gt;INSTALL&lt;/a&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;';
    } 
    else {
    echo ' 
    &lt;div class="themewrap"&gt;
    &lt;div class="themepreview"&gt;
    &lt;img src="'.$backend-&gt;getThemePreview($themeDir).'"/&gt;
    &lt;/div&gt;
    &lt;div class="themeinfo"&gt;
    &lt;div class="themenoexist"&gt;THEME.XML DOES NOT EXIST&lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;';    
    }
    }
    ?&gt;


Get theme preview image

    public function getThemePreview($theme) {
    $ext = '.png';
    $preview = '../themes/'.$theme.'/preview'.$ext;
    if(file_exists($preview)) {
       return $preview;
    }
    else {
      $preview = 'images/nopreview'.$ext;
    return $preview;    
   }
   }


Theme.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;
 &lt;theme&gt;
  &lt;themename&gt;Default&lt;/themename&gt;
  &lt;themedescription&gt;This is the Default Theme&lt;/themedescription&gt;
  &lt;themeversion&gt;1.0&lt;/themeversion&gt;
  &lt;themeauthor&gt;WCX&lt;/themeauthor&gt;
 &lt;/theme&gt;
Yes you can move the output away from the code, however that's usually difficult to do with this type of procedural code. The code is extremely tightly bound, which makes separation harder than it should be!

The two function you call are fine as they are. So lets focus on those for now.

getAvailableThemes()

This function, since it's only being called once, is what I believe to be clutter. I don't see a need to separate it from the main logic. Doing so only increases the amount of times we need to look back at the code.

Also, I see no reason to cut of "Directory" in $themeDir. Spell it out for clarity.

getThemePreview()


Is the return value a "preview" or a "file"? Perhaps a better name would be getThemePreviewFile.
We can refactor the code a lot:

$preview = '../themes/'.$theme.'/preview.png';

if(!file_exists($preview)) {
    $preview = 'images/nopreview.png';
}
return $preview;    


I shortened it, and I added the extension into the literal because it doesn't look like the extension will change.


Now for the main file!

wcx_themes.php

First off, indentation should be key here. Indentation is also missing here! Readability is very important.


foreach($themeDir as $themeDir) - I don't like this at all. Collections should be pluralized.
The immediate solution to separating the HTML and the PHP that I can think of, is a templating library. Other than that, you could store the HTMl in a string, and then use sprintf or another form of replacement to build the output.