Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

ViewModes

Summary: show selected content according to user choice (using a cookie)
Status: Stable
Version: 2006-10-28
Prerequisites: pmwiki-2.1.beta15
Maintainer:
Categories: Layout Skins CMS
Download: views.phpΔ

Question

How can I add the capacity to use view modes in PmWiki?

Answer

Install views.phpΔ to the cookbook folder and add to config.php

     include_once("$FarmD/cookbook/views.php");

The script adds several tools to PmWiki:

  • $View global variable
  • ?setview=viewname cookie switcher
  • ?view=viewname switcher
  • (:if view viewname :) conditional markup
  • {$View} page variable
  • (:if !enabled views:) conditional markup

These tools can be used by skin authors and administrators to add view mode features, which will give a user the choice to select from various views, to give the best skin and control interface for their current needs.

View switching can be disabled by setting $EnableViewSwitching = 0; in config.php, prior to loading the script.

View names included are:

  • standard, for visitors, readers
  • author, for authors, editors
  • admin, for site administrators
  • display, for visitors and readers without showing any editing controls

Other view names may be added.

These names may change still, as this is a first experimental release.

Details on usage:

$View global variable: this can be used in config.php to set the default view, in a skin.php for various conditional settings, or in a skin's template, for instance to load a skin config page via

   <!--wiki:Site.ActionLinks-$View Site.ActionLinks--> 

with a set of ActionLinks-standard, ActionLinks-author etc pages to give view-specific skin configuration.

?setview= viewname cookie switcher: Create action links to switch the view mode, like [[{$Name}?setview=author| Author view]]

(:if view viewname :) conditional markup: Use to display certain links etc if the view is selected.

{$View} page variable: Display the current view name in the page (or top bar, side bar, footer)

(:if !enabled views:) shows content only if the script is not installed.

Use for multi-language sites

Note: although the following instructions are correct, ViewModes has been evolved into the new multi-language recipe MultiLanguageViews, which is more specialised and better equipped for the purpose. - HansB

Redefine the $ViewList array, before including the script in config.php, like this:

$ViewList = array(
        'en' => 'en',
        'de' => 'de',
        'gr' => 'gr',
        'it' => 'it',
        );
$View = 'en'; //set default
include_once("$FarmD/cookbook/views.php");

Then use conditionals in the page content like this:

(:if view en:)
english text
(:if view de:)
deutscher Text
...
(:if:)
common text

In the sidebar, or header, put links for setting the language view:

[[{*$FullName}?setview=en| english]]
[[{*$FullName}?setview=de| deutsch]]

Or instead of text links use little flag images to click on:

[[{*$FullName}?setview=en| Attach:Site/Site/flag-en.gif"english"]]
[[{*$FullName}?setview=de| Attach:Site/Site/flag-de.gif"deutsch"]]

That takes care for multi language content. For changing the wiki interface language according to the language view add to conflig.php something like this, after including the views.php script:

if ($_COOKIE['view'] == 'de') {
  XLPage('de','Site.XLDePageCookbook');
  XLPage('de','Site.XLDeLocalPage');
  XLPage('de','Site.XLDePage');
}
etc.

The default for pmwiki is English, so no XL page need to be specified for English. The XL translation term pages get loaded according to the cookie setting. Make sure you got them in the right locations.

Updates

  • 2006-10-28: Added $RecipeInfo
  • 13 January 2006: Added $CookiePrefix to avoid possible cookie conflicts. $CookiePrefix is set in config.php to give a site unique cookie names. Changed {$View} to be a page variable.
  • 31 August 2005: Added possibility to use (:if !enabled views:) to show content if script is not loaded.
  • 9 August 2005: Changed 'stealth' to 'display'. Added $EnableViewSwitching. HansB

Notes

  • This recipe was last tested on PmWiki version: 2.1.beta21
  • This recipe requires at least PmWiki version: 2.1.beta15

For versions 2.0 to 2.1.beta14 replace

      ## add {$View} page variable
      $FmtPV['$View'] = '$GLOBALS["View"]';

with

      ## {$View} markup 'variable' replacement
      Markup('{$View}', '>{$fmt}', '/{\\$View}/e', "\$GLOBALS['View']");    

See Also

  • The group ViewModes/ for how this works in a practical example

Comments

I found that when I set $View = 'author'; in config.php, that I could not switch views even though view switching was enabled. But, if I changed it to SDV($View,'author');, view switching worked. Bronwyn

I have not seen this, how exactly is the configuration?. - If you set $View in config.php, you need to do so before include_once("FarmD/cookbook/views.php");. - If you load views.php from a skin.php then you need to add a bunch of global declarations. ~HansB

I had the include before the $View declaration. Swapping them fixed it. Thanks! Bronwyn


Would be nice if ?setview=admin would only be allowed if you are auth as admin.Is there a way to do this ? Marc Seibert August 18, 2005, at 03:46 PM

The view modes concept was to allow any user the choice to switch modes (or none). You could use (:if auth admin:)....(:if:) to display admin stuff when logged in as admin. But be careful since we can't nest conditions within each other. - I am trying to think of yet another way... ~HansB

Views work by making the links available, and then authorizing if required. You want to do it the other way round. Since we can't currently do both in one step, give this a try:

  1. Create a page that requires the admin password to read it, e.g. PmWikiAdmin.Login. (As far as I know, you can't require the admin password per se, but you can certainly set the page's read password to match it.)
  2. In your sidebar, skin, or whatever, make a discreet admin login link that links to said page, e.g. [[PmWikiAdmin.Login | Admin Login]]
  3. On PmWikiAdmin.Login, have something like,
(:if !auth admin:)
%red%Somehow, you are not logged in as admin, even though you are reading this page.  
Did you change the admin password without changing this page's read password to match?%%
(:if auth admin:)
>>green<<
You're now logged in as admin.

[[Switch to admin view -> {$Name}?setview=admin]]
>><<
(:ifend:)

I find it both frustrating and amusing that people want to turn the concept of views inside out, but so long as it's not the default or the stated purpose, I'll not mind. Bronwyn


Is there a good way to have a block of content show if views are disabled, but not show when views are enabled? I tried (:if !view:) which didn't work. Bronwyn

You could add this to the views.php script:
           $Views = 1;
           $Conditions['views'] = "\$GLOBALS['Views']==\$condparm";
and use this markup in the page:
                (:if !views 1:)Views are not installed (:if:)
Note this adds another global variable $Views which is set to 1, and adds conditional markup to check it. ~HansB
PS: Another solution which avoids setting another variable: Use conditional markup:
               (:if !enabled View:)This content shows only if $View is not defined(:if:)
This checks if the global variable $View is set. Unfortunately the enabled condition needs to be defined, and it is not at present part of pmwiki's default set of conditions shipped in the distribution. It would be nice if Pm could put it into the core. The definition of (:if enabled VARIABLE:) is:
                $Conditions['enabled'] = "(boolean)\$GLOBALS[\$condparm]";

Great! Could one of these be added to the views script? I see it being useful for a lot of people. I'd also make a small refinement for ease of use:

           $Views = TRUE;
           $Conditions['views'] = "\$GLOBALS['Views']==\$condparm";

That way, it's more obvious that (:if !views TRUE:)Views are not installed (:if:). Or would that just cause confusion when people tried to use (:if !views true:)Views are not installed (:if:) and it mysteriously didn't work? Bronwyn

I added to views.php:
          # show content if views are not installed with (:if !enabled views:)
              $views = 1; 
              $Conditions['enabled'] = "(boolean)\$GLOBALS[\$condparm]";
So use for instance
          (:if !enabled views:)Views are not installed!(:ifend:)
Note this adds a variable $views (small letters) and the 'enabled' condition. It is better to check a new variable with (:if enabled ... :) , since $View may be defined in config.php, even though the views.php script may not be loaded. ~HansB

I have view modes working, except for the display of the current view using the variable in curly brackets {$View}. I just end up with the code displaying in the page. Otherwise, I can switch between modes just fine. What should I be looking for to fix this? ~StaceyM?

Do you use a pmwiki version prior to 2.1.beta15? In 2.1.beta15 page variables were introduced, and {$View} is defined now as a page variable. You can define it instead with this markup:
           ## {$View} markup 'variable' replacement
           Markup('{$View}', '>{$fmt}', '/{\\$View}/e', "\$GLOBALS['View']");

~HansB

I had been using the latest stable release 2.0.13 so upgraded to the beta 2.1.22. This fixed the problem with the display of the current view, but seemed to introduce new problems with my use of AuthUser and RequireAuthor, showing warnings for lines 478 and 1112 in pmwiki.php. So I may end up reverting back and using the replacement markup. Thanks.~StaceyM?


Contributors

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.

Edit - History - Print - Recent Changes - Search
Page last modified on September 10, 2011, at 11:42 AM