|
Cookbook /
ViewModesSummary: show selected content according to user choice (using a cookie)
Status: Stable
Version: 2006-10-28
Prerequisites: pmwiki-2.1.beta15
Maintainer:
QuestionHow can I add the capacity to use view modes in PmWiki? AnswerInstall views.phpΔ to the cookbook folder and add to config.php The script adds several tools to PmWiki:
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 View names included are:
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 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 (: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 sitesNote: 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
Notes
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
CommentsI found that when I set 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:
(: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 You could add this to the views.php script:
$Views = 1;
$Conditions['views'] = "\$GLOBALS['Views']==\$condparm";
and use this markup in the page:
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:
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:
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 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
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']");
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? ContributorsUser 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. |