Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2

Author Topic: [MOD] Adding id to the <body> tag (sNews 1.5)  (Read 25386 times)

Fred K

  • sNews Dude
  • ULTIMATE member
  • *****
  • Karma: 134
  • Posts: 2806
[MOD] Adding id to the <body> tag (sNews 1.5)
« on: April 06, 2007, 10:55:02 PM »

Prompted by this post I decided to find a solution to how we can set a dynamic id to the body tag in index.php, thereby allowing us to set different styles for each Page or Category as well as the entire Admin interface. I fudged about with snews.php but got almost nowhere, so I asked philmoz for help. Here is his working solution:

/* UPDATED WITH NEW CODE */
/* SMALL ERROR FIXES, Aprll 7 */

1. In snews.php add this function somewhere (I have placed mine directly after the title function)
Quote
// BODY FUNCTION by Agent Smith & philmoz
function bodyname() {
$categorySEF = get_id('category');
$pageSEF = get_id('article');
$pageId = (empty($categorySEF)) ? 0 : retrieve('position', 'articles', 'seftitle', $pageSEF);
$catList = explode(',',l('cat_listSEF'));
$pageList = in_array($categorySEF,array('home','archive','contact','sitemap'))?true:'';
$categorySEF2= in_array($categorySEF,$catList)?true:'';
switch (true){
    case empty($categorySEF)||$categorySEF==s('home_sef') && empty($pageSEF) : $id= s('home_sef'); break;
    case !empty($categorySEF)&& empty($pageSEF)||$pageId!=3 : $id=$categorySEF; break;
    case !empty($pageSEF)&&$pageId==3 : $id=$pageSEF; break;
    default:$id=s('home_sef');
}
if($_SESSION[db('website').'Logged_In'] == token()){
    switch(true){
        case !$pageSEF&&$categorySEF2==true&&$pageList!=true: $id="administration";break;
        case $_GET['action']:$id="administration";break;
        default:$id;
    }
}echo "<body id=\"$id\">";   // <-- Fix 7.4.2007
}
And then go to function login() and find the first line starting with html_input. Change it to the following:
Quote
html_input('form', '', 'post', '', '', '', '', '', '', '', '', '', 'post', db('website').'administration/', '');
2. Usage -- In index.php, exchange the opening <body> tag with this code:
Quote
<?php bodyname(); ?>
3. What it does
With this mod in place, the generated body tag for Home, when not logged in, will look like this:
Quote
<body id="home">
For a category named Test, like this:
Quote
<body id="test">
For a page named Testpage, like this:
Quote
<body id="testpage">
4. Considerations
Naming conventions. Since you will get body id's with same names as your home sef title, category names, page names and administration, make sure to not use those names for other id's on your site -- it will break validation and can upset styling in unwanted ways.

If you use this mod and like it, please feel free to donate a beer or chocolate chip cookie to philmoz... :D
« Last Edit: August 21, 2007, 02:31:45 PM by Patric Ahlqvist »
Logged

mosh

  • Dev Invitee
  • Hero Member
  • *****
  • Karma: 78
  • Posts: 522
  • Awesome day :)
[MOD] Adding id to the tag (sNews 1.5)
« Reply #1 on: April 06, 2007, 11:37:33 PM »

thank you agent & philmoz
as always smart solutions.

awesome day ppl
mo  :cool:
Logged

David

  • sNews Dude
  • Hero Member
  • *****
  • Karma: 7
  • Posts: 580
[MOD] Adding id to the tag (sNews 1.5)
« Reply #2 on: April 06, 2007, 11:40:18 PM »

Thank you Fred and Philmoz for this interesting mod! :)
Logged

Keyrocks

  • Doug
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 451
  • Posts: 6290
  • Semantically Challenged
    • snews.ca
[MOD] Adding id to the tag (sNews 1.5)
« Reply #3 on: April 07, 2007, 12:27:11 AM »

Awsome indeed, Agent and Phil... if it does what I think it does... this opens up what looks like a very flexible way to have control over different CSS styling for the body tag. (thinking aloud, slowly but surely...) In other words... we could have a set of body styles named according to cat-names, etc... and each cat would then display in its respective body-style... and we could have all these tags in the existing style.css or a 2nd css file... are we close?
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU

Fred K

  • sNews Dude
  • ULTIMATE member
  • *****
  • Karma: 134
  • Posts: 2806
[MOD] Adding id to the tag (sNews 1.5)
« Reply #4 on: April 07, 2007, 01:23:11 AM »

close, key? you're standing right on it! ;)

What happens with the mod in place is that when you are on the Home page --as an example--, the body gets id="home". So, in your css you put a style for #home and it will be picked up only on category Home "pages".

Click another category and your home style "disappears" (because the category you are now visiting generates the body id corresponding to its name).

If you go someplace that is not home, a page, a category or part of the Admin interface, the body gets no id added to it (that will afaik never happen, but it's the default action, for your info).

As I said in the original post, naming control is necessary to avoid validation and (possibly) other problems.

On another note: Phil has been busy and made improvements to the previously posted code. I have updated the original post with the new code, so if you've already copied the original -- try the new one instead. We think it's even better. ;)

philmoz

  • High flyer
  • Administrator
  • ULTIMATE member
  • ******
  • Karma: 161
  • Posts: 2001
    • fiddle 'n fly
[MOD] Adding id to the tag (sNews 1.5)
« Reply #5 on: April 07, 2007, 02:26:26 AM »

to use this as a full style sheet switcher, that is, to affect the line
Quote
some small changes are required, and a style specific small function to use the output of the above.
First, the changes to the above MOD...
this
Quote
}echo "";
becomes this
Quote
}return $id;
If you still want to use it in index.php as well, then usage is
Quote
>
but to switch style sheet completely, this function goes into snews.php
Quote
function switchStyle($exclude_cat){
   $exclude_cat=explode(',',$exclude_cat);
   $id= bodyname();
   if (!in_array($id,$exclude_cat)){$style=$id.".css";}else{$style="style.css";}
   return $style;
}
$exclude_cat is a comma delimited list of sef's that are to be excluded from using independant style sheets, allowing any small changes in area to be contained in the default style.css .. and hence in the body tag :)
To make it a list of include sefs, alter
Quote
if (!in_array
to
Quote
if (in_array
so, the style line from above (from index.php) then becomes
Quote
Using the switchStyle() aswritten, exclusions are added like
Quote
switchStyle('home,contact,administration')
... I think that is all  :P
Logged
Of all the things I have lost, it is my mind that I miss the most.

sevenupcan

  • Newbie
  • *
  • Karma: 0
  • Posts: 20
[MOD] Adding id to the tag (sNews 1.5)
« Reply #6 on: April 07, 2007, 11:15:07 AM »

This is great stuff guys. Not only were you able to solve my problem of styling individual pages dynamically but you were able to take it further. Great stuff, thank you agentsmith, philmoz and codetwist for answering my questions. When I've implemented these functions into a fully functional site I'll have to show you.
Logged

Fred K

  • sNews Dude
  • ULTIMATE member
  • *****
  • Karma: 134
  • Posts: 2806
[MOD] Adding id to the tag (sNews 1.5)
« Reply #7 on: April 07, 2007, 01:41:38 PM »

Small errata: The line in the original mod post that said echo } ", e.g without quote marks, which causes validation to fail. So it should look like this:
Quote
}echo "";
Original post has been updated, along with the redundant info about admin bits.

piXelatedEmpire

  • MIA
  • Thread Moderator
  • ULTIMATE member
  • *****
  • Karma: 37
  • Posts: 1454
  • currently MIA
[MOD] Adding id to the tag (sNews 1.5)
« Reply #8 on: April 10, 2007, 01:31:34 AM »

wow this looks very promising! Can't wait to play around with this cheers guys!
Logged
my apologies to the sNews crew, but I will be MIA for the forseeable future

centered

  • Guest
[MOD] Adding id to the tag (sNews 1.5)
« Reply #9 on: July 26, 2007, 06:17:23 PM »

Will this work in 1.6 as well or is there a newer mod for 1.6?
Logged

Fred K

  • sNews Dude
  • ULTIMATE member
  • *****
  • Karma: 134
  • Posts: 2806
[MOD] Adding id to the tag (sNews 1.5)
« Reply #10 on: July 26, 2007, 07:13:17 PM »

Yeah, I think it works in 1.6. I haven't tested it fully, but generally speaking most mods that work with 1.5(.31) work in 1.6 as well. The simplest way to find out is to test it after you've made a backup of your file, naturally. ;) I don't have time to test it myself until possibly this weekend... No guarantees, but it should work.

maxiso

  • Newbie
  • *
  • Karma: 0
  • Posts: 9
[MOD] Adding id to the tag (sNews 1.5)
« Reply #11 on: August 01, 2007, 02:45:50 PM »

I've tried this one in sNews1.6 and it works perfectly. But (here's the catch :rolleyes: )...
I can't login anymore??? :o

Anyone else has the same problem?

Chua

Max
Logged

centered

  • Guest
[MOD] Adding id to the tag (sNews 1.5)
« Reply #12 on: August 01, 2007, 06:38:30 PM »

Maxiso,

The mod works for me fine:

This line:
html_input('form', '', 'post', '', '', '', '', '', '', '', '', '', 'post', db('website').'administration/', '');

Does not need to be added as this is already in 1.6

1.6 shows:
echo html_input('form', '', 'post', '', '', '', '', '', '', '', '', '', 'post', db('website').'administration/', '');
Logged

maxiso

  • Newbie
  • *
  • Karma: 0
  • Posts: 9
[MOD] Adding id to the tag (sNews 1.5)
« Reply #13 on: August 01, 2007, 08:50:39 PM »

@centered;

Thanks for the advise everything's working perfectly now!!
 ;)
Logged

ifunky2

  • Guest
Re: [MOD] Adding id to the <body> tag (sNews 1.5)
« Reply #14 on: April 10, 2008, 03:27:49 AM »

1. would like to have next to the include button, an 'include_once' button
2. Would like a way to detect page request before page assebly:
  A. So that individual/custom page layouts can be served.
      depending on part of the site the user requests,
      determining the layout they are served.
       (such as side col content)
  B. So that individual/custom stylesheets can be served.
      (yes this is done now, but after page assembly)


My ponder in short:
How do I make it so I do not see any side col in the admin panel/area ?
How do I serve up a different page layout and/or stylesheet as per site area from which page was requested?

What I intitially want to do is simply:
-Never show any side col (and style width accordingly) while in any of the admin area of the site.
-Never show any of the 'Categories', 'Articles' or 'Comments'  in side bar, unless you are on the 'Articles' home or sub pages menu.

My unsuccessul attempts at getting what I want out of sNews:
I tried to take the includes for 'Categories', 'Articles' and 'Comments' out of the index.php.
I created a file 'articles_menu.inc.php' and then using this as a peice of 'extra content' I called it into the side col of only the articles page.
This did not work because it seemed to loop through and display multiple times in the side col of the rendered page.
I thought perhaps I could, ha ha.. find a way to make a button in the 'wisywig' that would allow input 'include_once' but I was completely unsuccessful there.

I cruised the forum and the closest I think I came, was this thread

However the method described deals with the CSS and all it controls, after the page is constructed.
I could use CSS to 'display:none" the side col in any of the admin area, but that is messy code. Better to be able to start from the start and actually server up  stylesheets and side col content as per page request.
Logged
Pages: [1] 2