| Topic: | Theme Add-in: Individual Blocks on Certain Pages & Modul |
|---|---|
|
nate_02631
Professional
Posts: 3055 Posted: |
Hello themers! Do you want a simple way to have certain blocks appear only on certain pages or modules on your PostNuke website? If you are using the "classic" style of PostNuke theming (with a theme.php), below is some cut-n-paste code that you can add to the block display section to let you customize your block layout! For example, if you only wanted the "category" block to appear on "News" pages, or different Polls to appear with different stories or modules, you can do it easily with the add-in below. It will work with any PN 7.x "classic" theme.php -- think of it as a "lazy man's AutoTheme" ;). How does it work? To specify custom blocks on your website, you simply edit a single array variable at the top of your theme's themesidebox() function. The array's "keys", on the left, dictate which specific blocks or block type you want to show only on certain pages or modules. The array that follows the "key" value is a comma-separated list of the modules and/or the story/topic/category/section/article id(s) where you want to display that block... The code is well commented at the top (in fact, is mostly comments as it's simple code!) but some notes and things to consider are also at the end of this post. Here's the Code: Just inside: Codefunction themesidebox($block) { of your theme.php, add: Code// THEME BLOCK MOD :: For PostNuke theme.php (v 7.x) by Nate Welch // The following mod can be added into any theme.php and lets you specify // blocks to appear on specific pages or modules of your Postnuke Website. // The $show_block array below specifies which specific blocks or block types // are shown on specific modules, topic or section TOC's or articles and stories. // If an existing block type or title is not specified, it will show on all pages. // Blocks are shown in the order specified in the Blocks section of the Admin. // NOTE: If your theme has logic to not show certain block groups, that logic will // override any blocks you may have set below that belong to that group . For example, // many themes use a "if ($GLOBALS['index'] == 1)" to specify only showing certain // blocks groups (right & center) only on the home page. // The keys (left value) of the associative array below is either the title of an // existing block or the name of the block type (see the Blocks Admin). Blocks are // processed in the order they are in the array, so specify specific blocks before // general block types. The values in the array can be the name of the module(s) // that you want the block to appear on, surrounded by single quotes and separated // by commas. You can specify as many modules as you like for each block. You can // also specify specific topics, articles, sections, stories and categories that // you would like a block to appear on. The variables are the same as seen in the // (long) URLs when surfing a PN site and include: sid - a news article id; topic - // a news topic id; secid - a section id; artid - a section article id; catid - a // category of articles; cid - A Web Links Category; lid - a Web Links detail page // id - a Reviews page, PollID - a poll results page. // After the identifying variable, include one or two numbers // indicating either a specific id # or a range of id #'s starting at the first // number specified and ending with the last (inclusive). You can specify multiple // ranges for the same type of id, for example: "'sid',1,4,'sid',7,'sid',15,18" // which would include the block with news stories whose sid is 1 through 4, 7 or // 15 through 18. If you wanted to include a block with *every* news story, you could // simply specify the 'News' module in the array value. To specify a block for the // home page, use the name of the module that you have the home page set to. $show_block = array('The First Poll' => array('catid',1,'NS-Admin','Recommend_Us','Reviews','Members_List'), 'The Second Poll' => array('sid',1,'FAQ','Top_List','News','NS-Admin','Search'), 'poll' => array('FAQ','Sections','Reviews','Members_List'), 'stories' => array('sid',1,4,'FAQ','Members_List'), 'thelang' => array('Sections','Web_Links'), 'category' => array('News','Sections'), 'Another Block' => array('secid',1,'sid',1,4,'sid',9,11,'Downloads','Members_List'), 'online' => array('Reviews','FAQ','Members_List') ); // Loop through the $show_block array to check for matches while (list($key, $val) = each($show_block)) { // Check if a match for either the specific block or the block type if ($block['bkey'] == $key || $block['title'] == $key) { while (list($key2, $val2) = each($show_block[$key])) { if ($show_block[$key][$key2] == 'sid' || $show_block[$key][$key2] == 'topic' || $show_block[$key][$key2] == 'catid' || $show_block[$key][$key2] == 'secid' || $show_block[$key][$key2] == 'artid' || $show_block[$key][$key2] == 'lid' || $show_block[$key][$key2] == 'cid' || $show_block[$key][$key2] == 'id' || $show_block[$key][$key2] == 'pollID') { if (pnVarCleanFromInput($show_block[$key][$key2]) == $show_block[$key][$key2+1] || (pnVarCleanFromInput($show_block[$key][$key2]) >= $show_block[$key][$key2+1] && pnVarCleanFromInput($show_block[$key][$key2]) <= $show_block[$key][$key2+2] && intval($show_block[$key][$key2+2]) != 0)) break 2; } elseif ($show_block[$key][$key2] == $GLOBALS['ModName']) break 2; } // End of second while loop return; } // End of matched block } // End of first while loop // END THEME BLOCK MOD ... then the rest of your themesidebox() function. The only part you have to change to customize your website's blocks is the $show_block array variable at the top. Notes on Usage The left values in the array represent the different blocks (either individual or block types) whose display you want to restrict to certain pages or modules. The value of these "keys" will be either the "Title" of the block you want to customize (as seen in the header of the block itself or under the "Title" column of the "view blocks" in blocks admin) or the "Name" of the block (seen under the "Name" column of the "view blocks"). Specifying an existing "Title" for the block will customize that specific block only, and if you change the Title of the block you must also change it in this array. Specifying the "name" of the block will customize an entire block type, such as Polls. If you are specifying both an individual Poll block (i.e., "Poll of the Day") and the general Poll block type ("poll"), you should specify the individual block first in the array, otherwise it will be overridden by the general block type. You may only specify an individual block or block type once in the array. If an active block is not specified in the array, it will show up on every page it would normally show up. The sub-array for each of the "key" values (right of the "=>") determines which modules or pages the block or block type specified before the "=>" will show up on. The values in this sub-array are separated by commas and you can have as many as you want for each block or block type specified. To have a certain block appear on all the pages of a specific module, put the Module name(s) in this array, i.e. "News","Downloads","NS-Admin". The module names can be seen by listing them in the Admin->Modules part of PostNuke or by browsing the /modules directory. Note: not all modules have "content pages" on your PostNuke Website. If you want to see what pages use a particular module, check out the "long" URL or echo $GLOBALS['ModName'] somewhere in your theme and surf your website! You can also specify specific pages on your website to display certain blocks. First, specify the identifier of the page item - for News articles it's "sid", for example. Then, specify either one or two numbers representing the ID numbers of the articles/topics/etc... that you want to include the block on. If you specify one number after the identifier it will include the block on just the page with an identifier matching that number. If you include two numbers after the identifier, you will specify a range of pages matching the first through second values you specified. A list of supported identifiers is in the comments of the script above. For example, to include a block on a news item whose identifier, sid, is "5", you would add this to the array: CodeTo include the block on news stories with an id of 10 through 20: CodeYou can include non-sequential and multiple identifiers, too: CodeWhich would add the block whose title is "My Block" to news article pages whose sid is 5 through 10, 14 or 20 through 30 *and* topic pages whose topic id is 2. Things to Consider: Many themes have logic in them to not show certain block groups often when you're not on the home page, such as the right blocks, etc... If this is the case, modify your theme as necessary or any blocks in those areas will not show regardless of what you put in the array. CASE-SENSITIVITY is in effect. Take special note of the case of letters when entering Module names, identifiers (sid, catid, etc...) block types and specific block titles. They are all case-sensitive! If you are not familiar with PHP arrays, take special note of the format of the $show_block array when editing. This is a "two-dimensional" array where each of the blocks you specify has an array of its own. The values within both the "outer" array (the blocks you are customizing) and the "inner" array (the parameters for each block) are separated by commas. Text values are surrounded by single quotes and with numerals the quotes are optional. Take note also of the closing parenthesis of the "outer" array and the semicolon that ends the PHP line. The above code does not add any new "areas" or "zones" to your theme. However, the above mod is irrespective of zones so should work with any other "hacks" you may have made to the themesidebox() function in your theme.php. This mod can be mixed with some of my and other's theme block mods, but keep in mind if you use any mods that include parsing out extra data in the block title (via a delimeter, etc...) take that into consideration as the code above checks for $block[title] (you may want to change that to your parsed title, etc...) That's It! I hope you enjoy - if you use your creativity, I think you can get a lot of mileage out of this simple code... Any feedbacks or comments are welcome! -- Get PhotoGallery, PayPalCart, Dynamenu, Enhanced Blocks & other mods Cape Cod Travel Info... |
|
alondon
Freshman
Posts: 15 Posted: |
I'm surprised noone has replied.. this is great. Thanks for this post. |
|
nate_02631
Professional
Posts: 3055 Posted: |
They are all speechless in awe ;) Seriously though, you're welcome. It's a bit of a quick hack really for those whose just need some simple block placement and don't want to use a theming system. Note: As far as performance, PN is actually looping through all the active blocks and presumably sending them to the theme; they are just not being displayed in the theme... Don't know what that might mean if you, say, had *100* left/right/center blocks defined... I'm open to any feedback on that point. :) P.S. Search the forums on my username for more theme tidbits... -- Get PhotoGallery, PayPalCart, Dynamenu, Enhanced Blocks & other mods Cape Cod Travel Info... |
|
AbraCadaver
Professional
Posts: 1383 Posted: |
I'll look at the PN code because as I recall, only active blocks are returned from the query. Instead of calling the blocks() function in the commercial AutoTheme, I wrote a new one that only queries the DB if that position is supposed to be displayed and then only quries on active blocks. Maybe you are talking about the actual block PHP files? In that case I'll look at it so I can write a new function if necessary! -Shawn -- Get the Revolutionary AutoTheme HTML Theme System! Currently for PostNuke, PHP-Nuke, MD-Pro, CRE Loaded, osCommerce and Wordpress! |
|
nate_02631
Professional
Posts: 3055 Posted: |
Hi Shawn, Strictly speaking, in the code above (& in AutoTheme?) any block displayed on *any* page would be *active* and PN doesn't know whether the block should or should not be displayed until it gets to the theme. In which case it's already queried the DB for the block info, etc... I might have two or three dozen blocks each of which I want on a distinct page. They all must be active to appear at all, so PN goes about fetching each block's content and putting it in the $block[content] (a merging of data and the block's PHP output), going to the theme to see if it should be output then going back for the next one, etc... Dunno if AT-Lite works the same way, but I imagine this is somewhat mitigated by the presence of extra zones? -- Get PhotoGallery, PayPalCart, Dynamenu, Enhanced Blocks & other mods Cape Cod Travel Info... |
|
AbraCadaver
Professional
Posts: 1383 Posted: |
Actually, now that I look, here is the sequence of events: 1. the theme calls block($side), $side being l r or c 2. blocks() queries the DB for blocks only for that position (side) and only ones that are active: WHERE $column[position]='".pnVarPrepForStore($side)."' AND $column[active]=1 3. blocks() calls pnBlockShow(); for each block returned from query 4. pnBlockShow() includes the needed block file and calls the func in that file to display the block 5. the block functions builds title and content and calls themesideblock() in the theme Seems pretty efficient from here... -Shawn -- Get the Revolutionary AutoTheme HTML Theme System! Currently for PostNuke, PHP-Nuke, MD-Pro, CRE Loaded, osCommerce and Wordpress! |
|
alexminneapolis
Freshman
Posts: 2 Posted: |
First off, I am used to XOOPs where turning off blocks for certain pages/modules was fairly straightforward. I've been searching for days to find this feature. I haven't tried it because I am complete PHP newb, but I am grateful that someone has pointed to the light. Repitition of the same boring blocks page after page is tiring. Anyhow, now I just have to figure permissions and blocks (so that certain blocks won't display based on group membership) and how hide things in the modules menu/block. But I've only just begun to meander through forum. Thanks for your awesome. This community just amazes me. It would be cool if there was a GUI/checkbox module that allowed for "relative" block display based on group membership and specific page/module. |
|
AbraCadaver
Professional
Posts: 1383 Posted: |
QuoteIt would be cool if there was a GUI/checkbox module that allowed for "relative" block display based on group membership and specific page/module. Hmmm... That would be cool. Check my sig -Shawn [edit] Block display based on group is easily configured in the PN permissions [/edit] -- Get the Revolutionary AutoTheme HTML Theme System! Currently for PostNuke, PHP-Nuke, MD-Pro, CRE Loaded, osCommerce and Wordpress! |
|
nate_02631
Professional
Posts: 3055 Posted: |
** ABOVE SCRIPT EDITED 11/19/2003: BUGFIX The conditional statement just before the first "break 2" statement was appended with a "&& intval($show_block[$key][$key2+2]) != 0" to correct a slight bug when comparing page-specific ID # ranges. Apologies to anyone who already copied the script... -- Get PhotoGallery, PayPalCart, Dynamenu, Enhanced Blocks & other mods Cape Cod Travel Info... |
|
spucp
Softmore
Posts: 73 Posted: |
Nate, you're frighteningly awsome and gifted. Thanks for this great contribution and, when I can get my head around it, I'll be giving this a whirl. Thanks again. |
|
ac07080
Freshman
Posts: 10 Posted: |
I'm still learning from your starter theme. Thank you very much!! Using your starter theme, how can I hide the left side blocks for certain modules? Let's say a user clicks on the Gallery, I would like them to view it fully without the left/right blocks displayed. Once they switch to another module, 'News', then I want them to view both left and right blocks. Thanks for all your tutorials. Just wish there was an easier way (PHP for dummies? |
|
nate_02631
Professional
Posts: 3055 Posted: |
Hi ac07080, In the context of the mod above, the array specifies which blocks to *show* only on certain pages, thus if a blocks/module combo is not specified it will show on *all* pages (wit the provisos stated above). I guess to "hide" a block on certain pages, you would just specify the name of the block you wanted to "hide" and then only the modules where you wanted in to show on in the array above (if you are really new to PHP, I might look for a primer on the web re: array syntax). I do have a Block Module mod you might be interesed which allows both "Show on Only:" and "Hide on These:" pages & modules option that is integrated right into the Blocks Admin. Take a look at this post and/or PM me with an email to send you the files. Or you could try the newly developed Xanthia Templating engine (search forum) which allows similar placement. -- Get PhotoGallery, PayPalCart, Dynamenu, Enhanced Blocks & other mods Cape Cod Travel Info... |
|
barbados
Freshman
Posts: 15 Posted: |
How does it work if there are only conditions? There are only returns and nothing else. I still can not understand the idea of this trick - how should I modify themesidebox($block) function? |
|
nate_02631
Professional
Posts: 3055 Posted: |
The mod above is to be placed inside the themesidebox() function *above* the code/HTML (and/or conditional) logic you have to display different block types. What the conditional and returns do is check to see if a block should be shown on that page or not. If a match is found for a given block and page set, it exits the conditional and outputs the block (the "break" statements). If a match is found on the block but does not match the page/module specified, a return is used to skip outputting that block. If the block is not specified in the array, it skips checking altogether and just outputs the block. The part of the script you customize is the $show_block array. Check out the comments for proper usage. P.S. I also came up with this core module modification to the block part of PN which allows you to specify SHOW or DONT SHOW on specific pages and/or modules that is tightly integrated into the blocks edit screen and slightly more efficient. PM me with your email & I can send you the modded files or try the mods yourself :) Blocks Module Mod fo…ement w/PN themes -- Get PhotoGallery, PayPalCart, Dynamenu, Enhanced Blocks & other mods Cape Cod Travel Info... |
|
Dragonuke
Freshman
Posts: 18 Posted: |
I wish this becomes part of the core programming. |
|
nate_02631
Professional
Posts: 3055 Posted: |
Thank Dragonnuke, Actually this would be more for adding to any theme, which is easy to do, whether it's a "classic", Xanthia or AutoTheme. Not really a "core" mod... I actually did a mod to the core Blocks mode (downloads & info available on my site: http://www.natewelch.com ) which lets you simply check-off which modules or pages you want to show a particular block on right in the block edit screen. Pretty slick I thought, but the devs decided to go a different way re: Xanthia, where I think you have to go to each module and check offf this or that block as visible - and not sure if page-specific placement is possible(?) My way's better ;) -- Get PhotoGallery, PayPalCart, Dynamenu, Enhanced Blocks & other mods Cape Cod Travel Info... |