Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: PHP Template Switcher

  1. #1
    Join Date
    Dec 2003
    Posts
    410

    Default

    There have been several requests for a function to change templates. Here is a quick php function. The orignal idea came from "Build a PHP Switcher" found at http://www.alistapart.com/discuss/phpswitch/4/. There, the switch was used to change site style sheets. I modified the code to work with OR templates. Please feel free to make additions and improvements.


    First create switcher.php file with the following code and upload to your realty directory.

    Code:
    <?php
    $set = $_GET&#91;'set'&#93;;
    $set = preg_replace&#40;'#&#91;^a-zA-Z0-9_-&#93;#', '', $set&#41;;
    setcookie &#40;'site_template', $set, time&#40;&#41;+31536000, '/', 'yoursite.com', '0'&#41;;
    header&#40;'Location&#58; '.$_SERVER&#91;'HTTP_REFERER'&#93;&#41;;
    ?>
    Second make the following changes in common.php

    Code:
    ///////////////////////////////////////////////////
    	// setup to change templates
    
    	// set default template 
    
    	//get cookie if  it exists
         	$template= $HTTP_COOKIE_VARS&#91;"site_template"&#93;;
    	//set default
     	if&#40;$template=="" &#41;&#123;
            $template = "default_template";  // replace default_template with your default template 
         	&#125;  
    	//set template directory 
            $template = "/template/".$template;
    
    	// set template path
    	$config&#91;template_path&#93; = $config&#91;basepath&#93;.$template; // leave off the trailing slashes
    	$config&#91;template_url&#93; = $config&#91;baseurl&#93;.$template; // leave off the trailing slashes
    ///////////////////////////////////////////////////


    Last - put code to change templates in web pages where you want to let users change templates (some sample code) will also work with a drop down menu box
    // replace template_? with the name of your templates

    Code:
    <ul>
      <li><a href="./switcher.php?set=template_1">click here to change to template_1 style!</a></li>
    
      <li><a href="./switcher.php?set=template_2">click here to change to template_2 style!</a></li>
    
      <li><a href="./switcher.php?set=template_3">click here to change to template_3 style!</a></li>
    
      <li><a href="./switcher.php?set=template_4">click here to change to template_4 style!</a></li>
    </ul>
    Have fun,
    Frank

  2. #2
    Join Date
    Apr 2003
    Location
    Charleville / Australia
    Posts
    1,421

    Default

    Sorry if i sound dumb but is this a site event or user?

    ie: you go to my site and change the template useing the above method. you now have a new look. But does the guy that entered 2 seconds before you also have this new look over the top of his newly chosen ?

    I had at some point made something to do this however the problem was with people accessing it the site became unstable in trying to please everyones desired setup.

  3. #3
    Join Date
    Dec 2003
    Posts
    410

    Default

    Its a user evernt. Switcher.php sets a cookie for the user. Each user can chooses their own template.

  4. #4
    Join Date
    Apr 2003
    Location
    Charleville / Australia
    Posts
    1,421

    Default

    This may be of some use to those who use the above or maybe help someone to improve it or something.

    All this does is reads the template directory collecting the folder names(template folders) and puts them into a select box makeing it easy to select a new template without the need to alter any code every time you add a new template.

    This is currently in use for myself for selection from the admin area but it should easy be able to be used in this case as well

    Code:
    //Code to get template folder names into a select box
    echo '<select name="change_template">';
    	$template_directory = "./template";
    	$template = opendir&#40;$template_directory&#41; or die&#40;"fail to open"&#41;;
    	while&#40;!&#40;&#40;$design = readdir&#40;$template&#41;&#41;===false&#41;&#41;&#123;
         	if&#40;is_dir&#40;"$template_directory/$design"&#41;&#41;&#123;
         	if &#40;$design =='.'&#41;&#123;
         	&#125;elseif &#40;$design =='..'&#41;&#123;
         	&#125;else&#123;
               echo '<option value="'.$design.'">'.$design.'</option>';
            &#125;
         &#125;
    &#125;
    echo '</select><br>';
    	closedir&#40;$template&#41;;
    	//End of code to get template folder names into a select box

  5. #5
    Join Date
    Dec 2003
    Posts
    410

    Default

    Thanks. Your code would be an improvement but the user would still need to set the cookie when making a selection. I am a novice at php. Can the switcher function be incorporated in your code so when a selection is made it will set that cookie to that template?

  6. #6
    Join Date
    Apr 2003
    Location
    Charleville / Australia
    Posts
    1,421

    Default

    Untested. Use at your Own risk.

    Add to language file
    Code:
    $lang&#91;'select_theme'&#93; = "Please select your Theme";
    Place where template selector is wanted.
    Code:
    //Code to get template folder names into a select box
    echo '<form action="switcher.php">
    	'.$lang&#91;'select_theme'&#93;.'
    	<br>
    	<select name="set">
    	';
       $template_directory = "./template";
       $template = opendir&#40;$template_directory&#41; or die&#40;"fail to open"&#41;;
       while&#40;!&#40;&#40;$design = readdir&#40;$template&#41;&#41;===false&#41;&#41;&#123;
            if&#40;is_dir&#40;"$template_directory/$design"&#41;&#41;&#123;
            if &#40;$design =='.'&#41;&#123;
            &#125;elseif &#40;$design =='..'&#41;&#123;
            &#125;else&#123;
               echo '<option value="'.$design.'">'.$design.'</option>';
            &#125;
         &#125;
    &#125;
    echo '</select></form><br>';
       closedir&#40;$template&#41;;
       //End of code to get template folder names into a select box
    The last code could also be made as a function to save having so much code on a template page i guess but this should work

    Note: The select system may be a little fussy about folder names so for that reason it pays to not attempt to use folders with spaces or weird charactors in the name

  7. #7
    Join Date
    Apr 2003
    Location
    Charleville / Australia
    Posts
    1,421

    Default

    On a side note:
    This could easily be adapted to also set the language if wanted useing the same systems as above but for language files.

  8. #8
    Join Date
    Sep 2003
    Location
    London UK
    Posts
    1,654

    Default

    Hi All

    It's funny how this has now come up.

    As I have been asking about this for some time.
    http://www.open-realty.org/phpBB2/viewtopic.php?t=1872

    Being told it could not be done!!..with OR 1.1.4 :?

    As of my http://www.open-realty.org/phpBB2/viewtopic.php?t=2422
    it seems that people do want a template switcher.
    And has planted a seed for OR to take this on for 2.0 and 114 and 115

    Any thing is pos with OR

  9. #9
    Join Date
    Apr 2003
    Location
    Charleville / Australia
    Posts
    1,421

    Default

    Albert:
    I was away at the time of the post you linked above or i could of given you code to do that.
    The code above in this post is pretty much what i usedat that time but it was an admin only system where as frobn is making it available for everyone.
    I cant see any reason why this would not work and with a little work it could easily be a drop in mod with an extra box above the common menu .

    Several sites i have visited useing or backend have got template changing abilitys so i dont know why you did not get a response or told how it could be done. Anyway that seems to be the way or is going (all for me and none for all)


    What will people do when the developer decides to join them on their insistant campaign to rip off his work....

  10. #10
    Join Date
    Sep 2003
    Location
    London UK
    Posts
    1,654

    Default

    [quote="RealtyOne"]ie: you go to my site and change the template useing the above method. you now have a new look. But does the guy that entered 2 seconds before you also have this new look over the top of his newly chosen ?quote]

    Hi RealtyOne

    this is a very good point.

    I think would it be better to ad the changer into a user signup and then in his db section.
    When he changes the template his design that he wants to see comes from the db.

    And keep a default template for none signups.

    will that work?

    regards
    Albert

Similar Threads

  1. Template Changer
    By RealtyOne in forum Mods (CLOSED)
    Replies: 47
    Last Post: 05-26-2005, 03:23 PM
  2. Generic Template Prob still there in 1.1.4
    By Anonymous in forum HELP (CLOSED)
    Replies: 3
    Last Post: 10-23-2003, 04:17 PM
  3. Fixed Width Template - Left Align
    By chemaster in forum HELP (CLOSED)
    Replies: 1
    Last Post: 07-14-2003, 05:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •