Jump to content

Template Switching


Silveredge9

Recommended Posts

I'm relatively new to PHP, and I'm wondering if at all it would be possible to allow the user to select an include file via a link or drop down menu, and then have that include file automatically applied to the website and then saved so that choice doesn't reset for them when they leave the page.

 

I'm asking because my website's template is defined by an include in the websites header. For example:

 

<?php include("layout_default.php")?>

 

And I want the user to be able to choose from a list of available templates (aka: include files) and then have that template applied to their particular version of the website and then saved until they select another one.

 

Thanks for any help in advance, and if what I've written doesn't make any sense, feel free to point that out. :P

Link to comment
https://forums.phpfreaks.com/topic/143947-template-switching/
Share on other sites

I've checked out cookies, and have come up with the following code:

 

<?php

$expire=time()+60*60*24*9990;

$set = $_GET['set'];

if(isset($_COOKIE['style']))
{
$set = $_COOKIE['style'];
}
else 
{
switch ($set)
{
case 1:
$set = "layout_darkness.php";
break;
case 2:
$set = "layout_default.php";
break;
default:
$set = "layout_default.php";
break;
setcookie('style', $set, $expire);
}
}
?>

 

And I've added the links to my site:

 

<a href="indextest.php?set=1">Darkness</a> <a href="indextest.php?set=2">Default</a>

 

When the links are clicked, the chosen template is successfully applied to the website, but it isn't saved. Could someone take a look and my code and correct it?

Link to comment
https://forums.phpfreaks.com/topic/143947-template-switching/#findComment-755431
Share on other sites

You should not be passing the value of the cookie through the URL - you dont need to.

You are better organising your templates in a better directory structure.

 

Lets say I select a view on your website. The option values are 1,2, and 3. If I select 2 then a cookie is set with the value of 2.

 

So lets organise your templates in the following way:

templates/

1/layout_default.php

2/layout_default.php

3/layout_default.php

 

Now in your include function I use this value.

include('templates/'.$_COOKIE['value'].'/layout_default.php');

 

I would also recommed using a template engine such as smarty as it will make things a lot easier.

http://smarty.net

Link to comment
https://forums.phpfreaks.com/topic/143947-template-switching/#findComment-755883
Share on other sites

So how - using what you suggested as a means of applying a template - would the cookie responsible for assigning templates be set from a link or drop down menu?

 

I'm sure it's something relatively simple, but like I said, I've only recently began learning PHP.

Link to comment
https://forums.phpfreaks.com/topic/143947-template-switching/#findComment-756151
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.