Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest sNews - sNews 1.7 - with its own forums - for discussion and user mods.

Pages: [1] 2 3 ... 9

Author Topic: [MOD] Different templates wherever you want (version independent)  (Read 42224 times)

Joost

  • Guest

'Different templates wherever you want', can do exactly what it says. You start with one template set for the whole site: I've called it everythingelse.php (makes sense ;) ). Separate templates can be added later.

There has to be at least a 'new' index.php and a default template. I've named it 'everythingelse.php'. A template looks like an ordinary sNews template, but without:
Code: [Select]
<?php session_start(); include('snews.php'); ?>That code is already present in the new index.php

The new index.php:
Code: [Select]
<?php session_start(); include('snews.php');

function 
templates (){
$uri $_SERVER['REQUEST_URI'];
switch (true):
 
 
//-------------templates for pages should be listed beneath this comment.----------

 # If index.php is in a subdirectory, insert that name as well, like: case ($uri=='/subdirectory/'):
case ($uri=='/'):
include ('frontpage.php'); break;

 
# If index.php is in a subdirectory, insert that name as well, like: case ($uri=='/subdirectory/category/a-special-page/'):
case ($uri == '/category/a-special-page/'):
include ('that-particular-page.php'); break;

//-------------TEMPLATES FOR CATEGORIES MUST BE LISTED BENEATH THIS COMMENT.----------

case (ereg('/a-specific-category/'$uri)): 
include ('show-off.php');break;

        case (
ereg('/another-category/'$uri)): 
include ('cool-template.php');break;

// Default template for everything else (always last):
default:
include ('everythingelse.php');
endswitch;
}
templates ();
?>

The mod uses the part of the url that starts right after the domainname, like this:
/ = homepage
/category/styled-page/ = a specific page

So for pages (including "home") you need everything after the domainname, starting with a slash "/"

Important: If index.php is in a subdirectory, make sure the name of that subdirectory is in the string as well.
Subdirectory-example:
/subdirectory/ = homepage
/subdirectory/category/styled-page/ = a specific page

The order of priority in which the templates are applied, is the following:
1 - pages, articles
2 - categories
3 - Everything without a specified template gets the default template.


This means that a template set for, lets say my-special-category cannot change the look of a special styled article within that same category.

You can find the path to the page in the address-bar of your browser. If you find it difficult, you can past the following code in 'everythingelse.php'. It will show the path from the current page
Code: [Select]
<?php  echo  $_SERVER['REQUEST_URI'];?>
The names of the templates don't matter, name it the way you want. When using a lot of templates, use names that make sense. Of course you can use any template found on the forum.

A general note:

The goal of this mod is making it easy for website developers with a xhtml/css background (sounds like me ;)), to add different styles to one sNews installation, without touching the engine.
Easy template switching for administrators, editors or even visitors requires other methods/mods, posted elsewhere on the forum.
« Last Edit: December 03, 2007, 05:39:46 pm by Joost »
Logged

pwendel

  • Full Member
  • ***
  • Karma: 8
  • Posts: 106
    • sNews 1.6 in Danish
Re: [MOD] Different templates wherever you want
« Reply #1 on: October 16, 2007, 10:03:43 am »

Uhhhh, nice!

I'll put my node-type template mod on ice then, this is exactly the same, only more flexible. That was probably what I missed the most from Drupal, not necessarily alot of different templates but just an option to structure out put with several minitemplates to make it more modular (makes porting easier too).

Really great work :)
Logged
"Opportunity is missed by alot of people because it comes dressed in blue overalls and looks like hard work" -- Thomas Edison (1847-1931)

mlab

  • Newbie
  • *
  • Karma: 1
  • Posts: 13
    • mlab.name
Re: [MOD] Different templates wherever you want
« Reply #2 on: October 16, 2007, 10:13:31 am »

Thanks a lot for this MOD.
That's exactly what I was looking for  8)

Just 2 1questions :
1- I think that these code doesn't take into account the international tool of sNews  ??? (because the declared URI are hardcoded and not variable) (of course, I can be wrong - atm I'm just a newb) I've found an answer  ;)
2- to avoid to change or modify the original "skeleton" of sNews, wasn't it possible to begin by declaring a different homepage in the standard admin of sNews and then applying your MOD to these new homepage ?

But your MOD is already a very good idea  ;)
« Last Edit: October 16, 2007, 10:54:30 am by mlab »
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
Re: [MOD] Different templates wherever you want
« Reply #3 on: October 16, 2007, 12:50:09 pm »

Quote
   case ($uri == '/category/a-special-page/'):
      include ('that-particular-page.php'); break;

//-------------TEMPLATES FOR CATEGORIES MUST BE LISTED BENEATH THIS COMMENT.----------

   case (ereg('/category/', $uri)):
      include ('show-off.php');break;
Was this tried? Doesn't work for me if applied as described for now to default sNews 1.6.
Logged

Joost

  • Guest
Re: [MOD] Different templates wherever you want
« Reply #4 on: October 16, 2007, 08:38:17 pm »

@Codetwist
Yes, it is tested before publishing and once more after your post. It works.
I guess you are referring to the category template? Maybe I wasn't clear. It is the setting for a specific category, wit everything in it (except for articles that have their own template).
I am not in denial ;) and do appreciate some feedback on this, if any problems do exist.

@mlab
1- Still wanna make a remark to that.
The uri is derived from the page title and or category title.
The Dutch page title 'Fijne pagina' would create the uri: fijne-pagina
The German page title 'Tolle seite' would create the uri: tolle-seite
Furthermore, you can add language related text and tags to the template.

2- That depends how you look at it. Everything besides everythingelse.php (default) is optional. Deleting the code for frontpage.php, would make everythingelse.php the template for home. The 'new' index.php could basically start with this (not very useful) code:

Code: [Select]
<?php session_start(); include('snews.php');

function 
templates (){
$uri $_SERVER['REQUEST_URI'];
switch (true):

default:
include ('everythingelse.php');
endswitch;
}
templates ();
?>
« Last Edit: October 18, 2007, 08:12:11 am by Joost »
Logged

codetwist

  • Hero Member
  • *****
  • Karma: 50
  • Posts: 940
Re: [MOD] Different templates wherever you want
« Reply #5 on: October 16, 2007, 08:53:34 pm »

@Joost : My fault ... thought that category means category as type not specific one. So, if those are just ole seftitles then it should work just fine ;)
Logged

Joost

  • Guest
Re: [MOD] Different templates wherever you want
« Reply #6 on: October 16, 2007, 09:10:14 pm »

Thanks Codetwist. I will make changes to the first post. :)
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: [MOD] Different templates wherever you want
« Reply #7 on: October 17, 2007, 02:17:50 pm »

Hey, i cant include in templates something like this
Code: [Select]
<? include ("banners/rotation.php"); ?>
Logged

Joost

  • Guest
Re: [MOD] Different templates wherever you want
« Reply #8 on: October 17, 2007, 05:45:20 pm »

Wrong path perhaps? The path is relative to the current working directory (index.php). Not relative to the template.
check out http://nl3.php.net/include/
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: [MOD] Different templates wherever you want
« Reply #9 on: October 17, 2007, 06:08:33 pm »

ok here is what i do
normal index.php
i have somewhere inside <? include ("banners/rotation.php"); ?>

with this mod i make html folder and i have all templates inside
so is like this
Code: [Select]
function templates (){
$uri = $_SERVER['REQUEST_URI'];
switch (true):
 //-------------templates for pages should be listed beneath this comment.----------
 
case ($uri=='/'):
include ('html/frontpage.php'); break;

in frontpage.php i have <? include ("banners/rotation.php"); ?>
but like this is not working

if i try like this is working
Code: [Select]
<?php
if (($_SERVER['REQUEST_URI']) == '/' ) {
include(
'html/frontpage.php');
}

« Last Edit: October 17, 2007, 06:10:26 pm by sibas »
Logged

Joost

  • Guest
Re: [MOD] Different templates wherever you want
« Reply #10 on: October 18, 2007, 04:16:50 am »

I don't quite understand your last post (what's working, what's not).
The include path is always relative to index.php.
So if rotation.php is in a directory named banners directly beneath index.php it should be included. Are you sure the script itself is functioning? Looking at its name it is a bannerrotator (yes, I am that smart  8) ).
Check if there is a function call in the script or in frontpage.php, where you probably want to show it, and check the html paths to the banners.

You can also test the include path, by including a textfile. Thus, you make sure it is not a disfunctioning script.




 
Logged

Ken Dahlin

  • Full Member
  • ***
  • Karma: 30
  • Posts: 139
    • http://www.kendahlin.com/
Re: [MOD] Different templates wherever you want
« Reply #11 on: October 18, 2007, 09:41:08 am »

Oh man, I've been jumping through hoops to make this happen and your solution is so simple. Thank you for this mod, but how dare you make me feel so stupid? I'm going to karma you up 1 for the elegant solution, but then I'm going to de-karma you 1 for letting me know how dumb I am.
Logged

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Re: [MOD] Different templates wherever you want
« Reply #12 on: October 18, 2007, 12:57:40 pm »

@Joost
Thank you for your reply, your mod is working and you have karma for all your help that you give me.
Now i try to explain better,
I have in folder banner this script from http://lastrss.oslab.net/

(Some friends we change banners from rss)
Code: [Select]
<?php
function ShowOneRSS($url) {
    global 
$rss;
    if (
$rs $rss->get($url)) {
         echo 
'<ul><li><h3 title=""><a href="'.$rs['link'].'">'.$rs['title']."</a></h3></li></ul>\n";
            echo 
'<ul">';
            foreach (
$rs['items'] as $item) {
                echo 
'<li><a href="'.$item['link'].'" title="'.$item['link'].'">'.$item['title'].'</a></li>';
               }
            if (
$rs['items_count'] <= 0) { echo "<li>No items found in the RSS file :-(</li>"; }
            echo 
"</ul>\n";
    }
}

// include lastRSS
include "lastRSS.php";

// Create lastRSS object
$rss = new lastRSS;

// Set cache dir, cache interval and character encoding
$rss->cache_dir '/tmp';
$rss->cache_time 900;
$rss->cp 'UTF-8';
$rss->date_format 'M d, Y g:i:s A';

// List of RSS URLs 
$rss_left = array(
'http://example.com/rss'
);

$rss_right = array(
'http://example.com/rss2'
);

// Show all rss files
echo '<div class="stoprss1">';
foreach (
$rss_left as $urlShowOneRSS($url);
echo 
'</div>';

echo 
'<div class="stoprss1">';
foreach (
$rss_right as $urlShowOneRSS($url);
echo 
'</div>';
?>


I have try it in two ways
Inside in html/frontpage.php i have include the above script like this
<? include ("banner/rotation.php"); ?>
And is not working with following setup.
1: This is index.php
Code: [Select]
<?php session_start(); include('snews.php');?>
-- other includes goes here--
<?php
function templates (){
$uri $_SERVER['REQUEST_URI'];
switch (true):
 
//-------------templates for pages should be listed beneath this comment.----------
 
case ($uri=='/'):
include ('html/frontpage.php'); break;

case ($uri == '/contact/'):
include ('html/contact.php'); break;

//-------------TEMPLATES FOR CATEGORIES MUST BE LISTED BENEATH THIS COMMENT.----------

case (ereg('/category/'$uri)):
include ('show-off.php');break;

// Default template for everything else (always last):
default:
include ('html/main.php');
endswitch;
}
templates ();
?>


But if i change it like this is working.
2:
Code: [Select]
<?php session_start(); include('snews.php');?>
-- other includes goes here--
<?php
if (($_SERVER['REQUEST_URI']) == '/' ) {
include_once(
'html/frontpage.php');
else if ((
$_SERVER['REQUEST_URI']) == '/contact/' ) {
include_once(
'html/contact.php');
}
else{
include_once(
'html/main.php');
}
}

I try to find out why this happen but my php skills is not very good.  :-\
« Last Edit: October 18, 2007, 01:01:49 pm by sibas »
Logged

mlab

  • Newbie
  • *
  • Karma: 1
  • Posts: 13
    • mlab.name
Re: [MOD] Different templates wherever you want
« Reply #13 on: October 21, 2007, 01:57:31 am »

In my index.php, I had to add :
Code: [Select]
<?php
just between the line 1:
Code: [Select]
<?php session_start(); include('snews.php');and the line 2 :
Code: [Select]
function templates (){ to be able to use your code.

Is it something that I missed or ... ?
« Last Edit: October 21, 2007, 02:00:38 am by mlab »
Logged

Joost

  • Guest
Re: [MOD] Different templates wherever you want
« Reply #14 on: October 21, 2007, 03:17:46 am »

@mlab

If the code you're using now is working, leave it. Sibas has got problems with including a second file that includes a third file. You're still with me ? :)
Responding to his post (or any post at all) wasn't smart anyway, considering my feverish condition (flue) the past few days.
Although the code might do the same, I am gone delete that post to avoid further confusion.
Logged
Pages: [1] 2 3 ... 9