sNews Forum

Previous sNews versions => sNews 1.5 Final => Mods/addons => Topic started by: Fred K on April 06, 2007, 10:55:02 pm

Title: [MOD] Adding id to the <body> tag (sNews 1.5)
Post by: Fred K on April 06, 2007, 10:55:02 pm
Prompted by this post (http://snewscms.com/forum/index.php?topic=3957.0) 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
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: mosh on April 06, 2007, 11:37:33 pm
thank you agent & philmoz
as always smart solutions.

awesome day ppl
mo  :cool:
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: David on April 06, 2007, 11:40:18 pm
Thank you Fred and Philmoz for this interesting mod! :)
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: Keyrocks 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?
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: Fred K 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. ;)
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: philmoz 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
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: sevenupcan 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.
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: Fred K 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.
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: piXelatedEmpire on April 10, 2007, 01:31:34 am
wow this looks very promising! Can't wait to play around with this cheers guys!
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: centered 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?
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: Fred K 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.
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: maxiso 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
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: centered 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/', '');
Title: [MOD] Adding id to the tag (sNews 1.5)
Post by: maxiso on August 01, 2007, 08:50:39 pm
@centered;

Thanks for the advise everything's working perfectly now!!
 ;)
Title: Re: [MOD] Adding id to the <body> tag (sNews 1.5)
Post by: ifunky2 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.
Title: Re: [MOD] Adding id to the <body> tag (sNews 1.5)
Post by: Keyrocks on April 10, 2008, 03:45:39 am
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?

Welcome to sNews Funky-too. You've come to the right place... the sNews Forums that is... best place on earth to learn and cut your teeth as a new developer... if you fall into that category. We experiment with just about anything and sometimes come up with surprising results.  :)

There are several options for template, layout or theme switching... you might call sNews the McSwitchy CMS... and you'll find the latest ones by cruising the Mods & Addons Forum in the 1.6 section near the top of the main Forums page. We've got template switchers that swicth templates site-wide... a layouts system that gives the viewer a different layout for each area (category) in the site... even on a page-only basis. Then we have template switchers that work from within the Admin Panels... you choose which one to use when making a category or a page.

And... to top it off... we have a new one in the works being tested (now) with an sNews 1.7RC project that does everything you desire. It will, I believe... also be applicable to sNews 1.6... our current stable version. Stay tuned for good news coming our way soon.  ;D
Title: Re: [MOD] Adding id to the <body> tag (sNews 1.5)
Post by: Sven on April 10, 2008, 01:40:09 pm
Amazing what can be done with body id tag! :)
You desserve a karma for having wrtitten this Mod, Fred and Phil.
Title: Re: [MOD] Adding id to the <body> tag (sNews 1.5)
Post by: Sven on April 10, 2008, 02:16:28 pm
A small bug  ;D
When on the 404 page the body id is, of course, 404.
but W3org Markup Validation Service said:
Quote
I much regret Inspector Sven...
value of attribute "id" invalid: "4" cannot start a name
:-\
Title: Re: [MOD] Adding id to the <body> tag (sNews 1.5)
Post by: Fred K on April 10, 2008, 06:11:22 pm
How'd you get the validator to be so personal?! Ah, wait, it's the hat, right? ;D 8)
There's not much that can be done about that "error" of course, but thank you for reporting it. I'll just note that the error isn't critical... ;)
Title: Re: [MOD] Adding id to the <body> tag (sNews 1.5)
Post by: Sven on April 10, 2008, 06:55:46 pm
How'd you get the validator to be so personal?! Ah, wait, it's the hat, right? ;D 8)
There's not much that can be done about that "error" of course, but thank you for reporting it. I'll just note that the error isn't critical... ;)
Well, there's only Google who visit my 404 page. He did indexed it!
And Google Webmaster Tools said there is an error when trying to visit it.
He said http://mysite.com/404 drives to 404. :o ??? ;D
How'd you get the validator to be so personal?! Ah, wait, it's the hat, right? ;D 8)
No, the french mustache, Fred, the mustache!
Title: Re: [MOD] Adding id to the <body> tag (sNews 1.5)
Post by: d3signr on December 03, 2008, 03:50:41 pm
Can anyone confirm that this works for 1.7?

I've tried adding the changes but end up with an empty site. No page show at all.
On viewing the source for the page I see everything from the html doctype
declaration right down to where the body starts, but it doesn't complete. I get
this:

<body id="

and that's where it ends. Rest of the page is gone.

Any pointers?

Thanks in advance
Title: Re: [MOD] Adding id to the <body> tag (sNews 1.5)
Post by: Keyrocks on December 04, 2008, 02:29:52 pm
Can anyone confirm that this works for 1.7?

I've tried adding the changes but end up with an empty site. No page show at all. On viewing the source for the page I see everything from the html doctype declaration right down to where the body starts, but it doesn't complete. I get this:

<body id="

and that's where it ends. Rest of the page is gone. Any pointers? Thanks in advance

When display ends like this, it's because parsing (reading of the code) has stopped there so there's an error keeping it from parsing further.
This mod should work with 1.7 but I have not tried it. I did try it with 1.6 sometime last year but I'll have to dig back in project files to see what I had...
The version of this mod presented in this thread works for the old 1.5 versions of sNews. You want to use the sNews 1.6 version (http://snewscms.com/forum/index.php?topic=5807.0) as your reference... as it is applied a little differently... and it should work with 1.7 as well.