Customizing your template based on the current page

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Customizing your template based on the current page

Post by calguy1000 »

Hi all.  This has been asked recently, and well.... I was finally fooling around with my own personal site, and encountered a need for this functionality.

What I wanted to do, was use one page template for as much as possible, without having numerous similar templates lying around.  However, on my change settings page for FEU, I did not want to have search capabiliteis.  Similarly, on my 'forgot password' page, I did not want to have the login stuff showing up.  Here's what I did to work around the problem.

a) When editing a page, under the options tab (CMS 1.0.5) is a metadata field, where you can entere various meta tags,
    etc, etc.  On my change settings page, I added a few strings like:

Code: Select all

{assign var='hidefeu' value='1'}
{assign var='hidesearch' value='1'}
{assign var='privatepage' value='1'}
b) I marked this page as 'not cachable' (i.e.: I cleared the checkbox in the options tab when editing the page).

c) In my page template, I could then use smarty logic to control wether certain parts of my template were displayed.  In the example below, I can hide the call to the FrontEndUsers module.  As well, I can hide the search form, if the user is not logged in or the search form is not useful on specific pages.

Additionally, I can hide page content, if the page is marked as private and theuser is not loggedin.  However, if the page is not marked as private, then the content is displayed.

For example:

Code: Select all

      {if !isset($hidefeu)}
         <div class="login">{cms_module module='FrontEndUsers'  nocaptcha='1'}</div><!-- end of feu -->
      {/if}
      {if $customcontent_loggedin > 0 && !isset($hidesearch)}
         <div class="search">{search}</div><!-- end of search -->
      {/if}

      <!-- Start Content Area -->
      <div id="main">
        {if isset($privatepage)}
           {if $customcontent_loggedin > 0}
              {content}
           {else}
               <div class="bigerror">Private Content.  Authorization Required</div>
           {/if}
        {else}
            {content}
        {/if}
I hope this helps people, particularly those who are just starting with CMSMS.  Or, ar just starting to do conditional logic in their templates.

FYI: The example here (for the private content) used the FrontEndUsers and CustomContent modules.
Last edited by reneh on Tue Sep 23, 2008 9:03 pm, edited 1 time in total.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Customizing your template based on the current page

Post by Nullig »

Excellent! This is SO helpful.

Thanks,
Nullig
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Customizing your template based on the current page (More Tips)

Post by calguy1000 »

When working with multiple groups in FEU the problem got a bit more complex.  I wanted the following setup:

i) A group of pages for public/anonymous users
ii) A group of pages for members of group1 (that public/anonymous users could not see)
iii) A group of pages for members of group2
iv) the possibility that a particular user may be a member of both group1 and group2
v) The menu to behave properly for the above conditions.

And.... I didn't want anybody that was not logged in to be able to jump directly to a private page and see it's content.
Here's what I came up with:

in my pages that I wanted private to members of group1
a) set the page alias to group1_
b) set the following code in the metadata field of the options tab:

Code: Select all

{assign var='content_is_visible' value='0'}
{if $customcontent_loggedin > 0}
   {if isset($customcontent_memberof_group1)}
       {assign var='content_is_visible' value='1'}
   {/if}
{/if}
c) Modified my menu call like this
    (this is not complete, as it doesn't achieve step v above, but I'm working on it)

Code: Select all

         
{if $customcontent_loggedin > 0}
            {if isset($customcontent_memberof_group1)} 
               {cms_module module='menumanager' template='andreas01 : andreasmenu'   collapse="1" number_of_levels='2' excludeprefix='group2_'}
            {else}
               {cms_module module='menumanager' template='andreas01 : andreasmenu'   collapse="1" number_of_levels='2' excludeprefix='group1_'}
            {/if}
         {else}
            {cms_module module='menumanager' template='andreas01 : andreasmenu'   collapse='1' number_of_levels='2' includeprefix='public_'}
         {/if}
d) Modified my content code like this:

Code: Select all

        {if !isset($content_is_visible)}
            {* assume public content, unless the variable is already set *}
            {assign var='content_is_visible' value='1'}
        {/if}
        {if $content_is_visible == '1'}
           {content block='header'}
           {content}
        {else}
            <div class="bigerror">Private Content.  Authorization Required</div>
        {/if}
So now I'm almost there.  the only problem I have is if a user is a member of group1 and group2, some pages would be hidden.  This problem could be worked around using a template that had two menus, but I don't want that.  so I'll sleep on it again and see what I can come up with.

Note, that this logic can work in conjunction with the 'hidesearch' stuff I did above, and works well by the way.
Last edited by calguy1000 on Tue Apr 24, 2007 5:07 pm, edited 1 time in total.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Multiple User Groups, One Menu, and assuring private access

Post by calguy1000 »

Here it is..... the latest howto as to how I solved this problem.  Here's the scenario:

Two user groups, each requiring independant content
One menu in the template
Users can be a member of both groups

Requirements:
CMS Made Simple 1.0.5 or greater
CustomContent 1.4.4 or Greater
MenuManager 1.4 or Greater
FrontEndUsers (version shouldn't matter much)

Steps:
1.  In the metadata field of each of your private pages, add code similar to this: (change your group names as required)

Code: Select all

{assign var='content_is_visible' value='0'}
{if $customcontent_loggedin > 0}
   {if isset($customcontent_memberof_group1)}
       {assign var='content_is_visible' value='1'}
   {/if}
{/if}
2.  Add this user defined tag, named:  groups_to_prefixes

Code: Select all

$postfix = '_';
if( isset($params['postfix']) )
{
   $postfix = trim($params['postfix']);
}

$adjustcase =1;
if( isset($params['noadjustcase']) )
{
  $adjustcase = 0;
}

if( isset($params['groups']) )
{
   $groups = explode(',',$params['groups']);
   for( $i = 0; $i < count($groups); $i++ )
     {
          if( $groups[$i] != '' ) 
          {
              if( $adjustcase == 1 )
                 {
                     $groups[$i] = strtolower($groups[$i].$postfix);
                 }
              else
                 {
                     $groups[$i] = $groups[$i].$postfix;
                  }
           }
     }

   return implode(',',$groups);
}
return '';
3.  Adjust your menu call in your template like this:

Code: Select all

         {assign var='includeprefix' value='public_'}
         {if $customcontent_loggedin > 0}
            {capture assign='includeprefix'}{$includeprefix},{groups_to_prefixes groups=$customcontent_groups}{/capture}
         {/if}
         {cms_module module='menumanager' template='andreas01 : andreasmenu'   collapse='1' number_of_levels='2' includeprefix=$includeprefix}
4.  Adjust your content area in your template like this:

Code: Select all

       {if !isset($content_is_visible)}
            {* assume public content, unless the variable is already set *}
            {assign var='content_is_visible' value='1'}
        {/if}
        {if $content_is_visible == '1'}
           {content block='header'}
           {content}
        {else}
            <div class="bigerror">Private Content.  Authorization Required</div>
        {/if}

5. Adjust the page alias of all of your pages as follows:

    Pages that anonymous/non logged in people can see should start with public_
    Pages that only members of group1 can see should begin with group1_  (change group1 to your group name of course)
    Similarly, pages that only members of group2 can see should begin with group2_

Conclusion:
Now, the menu should appear properly for anonymous users as well as for members of group1 and/or group2.  Also, anybody directly accessing a page their not entitled to see, should get the 'Authorization Required' message.

This solution should work for any number of groups, with menu.  I hope this helps everybody.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Customizing your template based on the current page

Post by calguy1000 »

Okay, continuing on this thread..... my next goal was to display the news article title in the titlebar of the detail page, without modifying any source..... and I got it:

Here's how:
1.  In your news detail template add this line:

Code: Select all

{assign var="pagetitle" value=$entry->title}
2.  Move the call to the news summary to the top of your page template (directly above the metadata tag), and surround it with a capture, like this:

Code: Select all

{capture assign='newssummary'}
{cms_module module="news" number="3" summarytemplate='summary2.tpl' detailpage='details'}
{/capture}
3.  In the spot where you had the news tag, replace it with:

Code: Select all

{$newssummary}
4. Move the call to {content} to the top of the page template and surround it with a capture, like this:

Code: Select all

{capture assign='dfltcontent'}{content}{/capture}
5. In the spot where you had {content}, replace it with

Code: Select all

{$dfltcontent}
6. Add this little bit of logic directly below the logic from step 4

Code: Select all

{if !isset($pagetitle)}
{capture assign='pagetitle'}{title}{/capture}
{/if}
7.  In your template, replace any call to {title} with

Code: Select all

{$pagetitle}
i.e:

Code: Select all

<title>{sitename} - {$pagetitle}</title>
And there you are.  Now, when I click on the more link of a news summary item, when the detail page is loaded, it shows the news article title in the frame at the top of the window, and in the page header area.

I've pasted my entire template here as a reference

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<__html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>

{capture assign='newssummary'}
{cms_module module="news" number="3" summarytemplate='summary2.tpl' detailpage='details'}
{/capture}
{capture assign='dfltcontent'}{content}{/capture}
<!-- title-->
{if !isset($pagetitle)}
{capture assign='pagetitle'}{title}{/capture}
{/if}
<title>{sitename} - {$pagetitle}</title>

{metadata}
{stylesheet}

{cms_selflink dir="start" rellink=1}
{cms_selflink dir="prev" rellink=1}
{cms_selflink dir="next" rellink=1}

</head>

</__body>

<div id="wrap">

    <!-- start accessibility skip links -->
    <ul class="accessibility">
      <li><a href="#menu_vert" accesskey="s" title="Skip to navigation">Skip to navigation</a></li>
      <li><a href="#main">Skip to content</a></li>
    </ul>
    <!-- end accessibility skip links -->

   <!-- Start Header, with logo image that links to the default start page -->
   <div id="header" class="clearfix">
        <img class="logo"  src="images/logo.jpg" alt="The Campbell Family Crest" />
           <h1>{sitename}</h1>
<blockquote><div>"Ne Obliviscaris"<br />(Never Forget)</div></blockquote>
<h2>{$pagetitle}</h2>
   </div>
   <!-- End Header -->

      <!-- Start Vertical Navigation -->
      <div id="avmenu">
         <h2 class="accessibility">Menu:</h2>
         {assign var='includeprefix' value='public_'}
         {if $customcontent_loggedin > 0}
            {capture assign='includeprefix'}{$includeprefix},{groups_to_prefixes groups=$customcontent_groups}{/capture}
         {/if}
         {cms_module module='menumanager' template='andreas01 : andreasmenu'   collapse='1' number_of_levels='2' includeprefix=$includeprefix}

<div class="announce">
{$newssummary}
</div>
{global_content name='calgary_weather'}

      </div>
      <!-- End Horizontal Navigation -->

	  
   <!-- Start Breadcrumbs -->
<!--
Breadcrumbs is hidden for now
   <div class="breadcrumbs">
        {breadcrumbs starttext='You are here' root='Home' delimiter='»'}
   </div>
-->
   <!-- End Breadcrumbs -->

   <!-- Start Content (Navigation and Content columns) -->
   <div id="content" class="clearfix">

      <div id="topbar">
      {if !isset($hidefeu)}
         <div class="login">{cms_module module='FrontEndUsers'  nocaptcha='1'}</div><br/><!-- end of feu -->
      {/if}
      {if $customcontent_loggedin > 0 && !isset($hidesearch)}
         <div class="search">{search}</div><br/><!-- end of search -->
      {/if}
      </div>

      <!-- Start Content Area -->
      <div id="main">
        {if !isset($content_is_visible)}
            {* assume public content, unless the variable is already set *}
            {assign var='content_is_visible' value='1'}
        {/if}
        {if $content_is_visible == '1'}
           {content block='header'}
           {$dfltcontent}
        {else}
            <div class="bigerror">Private Content.  Authorization Required</div>
        {/if}


         <!-- Start relational links -->
<!-- hidden
	 <div class="hr"></div>
	 <div class="right49">
	 	<p><a href="#main">^ Top</a></p>
	 </div>
         <div class="left49">
            <p>{cms_selflink dir="previous"} <br />
            {cms_selflink dir="next"}</p>
         </div>
-->
	 <!-- End relational links -->
 
      </div>
	  <!-- End Content Area -->

   </div>
   <!-- End Content -->

   <!-- Start Footer -->
   <div id="footer" class="clearfix">
Copyright © 2005 (Robert Campbell). Original Design by <a href="http://andreasviklund.com">Andreas Viklund</a>.<br/>
Powered by <a href="http://www.cmsmadesimple.org">{image src="cmsbadge2.png" alt="CMS Made Simple"}</a> {cms_version}
   </div>
   <!-- End Footer -->


</div><!-- end wrap -->


<__body>
</__html>
I hope this helps people out, and if you have any questions or comments, I'd be happy to hear them.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
marvanni

Re: Multiple User Groups, One Menu, and assuring private access

Post by marvanni »

calguy1000 wrote:
Steps:
1.  In the metadata field of each of your private pages, add code similar to this: (change your group names as required)

Code: Select all

{assign var='content_is_visible' value='0'}
{if $customcontent_loggedin > 0}
   {if isset($customcontent_memberof_group1)}
       {assign var='content_is_visible' value='1'}
   {/if}
{/if}
I tried this and works quite well. I wonderd if it is possible to create a user defined tag from

Code: Select all

{assign var='content_is_visible' value='0'}
{if $customcontent_loggedin > 0}
   {if isset($customcontent_memberof_group1)}
       {assign var='content_is_visible' value='1'}
   {/if}
{/if}
When i try this i get an error.

It would be much better if in the editting page of the contentitem was a selectbox, where you can set the page private. Is it somehow possible to insert the tag above with a checkbox?
jsmonzani
Forum Members
Forum Members
Posts: 54
Joined: Mon Apr 03, 2006 10:58 am

Re: Customizing your template based on the current page

Post by jsmonzani »

> I wonderd if it is possible to create a user defined tag from
> When i try this i get an error.

I haven't followed the discussion, but if you paste Smarty code in a User Defined Code, it won't work since it expects PHP. Maybe that's your problem? You can make a global content block instead.
An approach to mutli-lingual CMSMS using only content blocks:
http://forum.cmsmadesimple.org/index.ph ... 756.0.html
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm
Location: North West England

Re: Customizing your template based on the current page

Post by Russ »

calguy1000 wrote: Okay, continuing on this thread..... my next goal was to display the news article title in the titlebar of the detail page, without modifying any source..... and I got it:
First off very nice calguy1000, but at least in 1.1 RC3 /News 2.3.0.1 they can be a few problems with RSS and Search.

If you have a url something like: http://domain.dev/news/1/82/
Where '1' is the news article and '82' is the news page id. (Using friendly URL's.) 

1. RSS Failure
This fails in RSS feeds because it returns:
http://domain.dev/news/1
without the /82/. The 'current page' also appears to be home - which is also going to confuse many people and is pretty bad on the accessibility front.

If you alter the RSS template from this:
{$entry->link}
to this:
{$entry->link}82
It will work OK, and returns
http://domain.dev/news/1/82/

But obviously this is not a real solution as it requires a fixed id!

2. Search failure
Exactly the same happens with Search - although I have not found a way to fix this?

3. On a completely different subject, is there a way to have a url, like date/category/article_title. I know there used to be, but it doesn't look like it would work in the current version?

I have posted a bug report for this as it makes it quite inaccessible - if fixed of course news would become quite accessible!

Russ
User avatar
FantomCircuit
Forum Members
Forum Members
Posts: 75
Joined: Fri Nov 10, 2006 1:34 am
Location: Gold Coast, Australia

Re: Customizing your template based on the current page

Post by FantomCircuit »

great idea!

This should help me, as before I was using mutiple content blocks and modifying the content for each page of my site - this method should keep it much more tidy.
JoeGrinD
Forum Members
Forum Members
Posts: 15
Joined: Mon Feb 19, 2007 1:46 pm

Re: Customizing your template based on the current page

Post by JoeGrinD »

I have used something like this on some of my pages for simple inclusion/exclusions:

Code: Select all

{if $page == 'pagename'}
 do something for pagename
{else $page =='anotherpage'}
 do something else for another similar page
{/if}
moorezilla

Re: Customizing your template based on the current page

Post by moorezilla »

Can this be used if more than one news summary is put into a page? For example, we use the News system for both traditional news and for open teaching positions. We also use the News system to publish student articles, so there are times where we may need to call the news three times on one page. Can we simply have multiple captures in the head of the document, or is this basically designed only for situations where the News system is only called once?

cmsms 1.1.2
php5.2x
apache
mysql5

am
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Customizing your template based on the current page

Post by calguy1000 »

You can use capture as much as you want.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Caspar

Re: Customizing your template based on the current page

Post by Caspar »

calguy1000 wrote:

Code: Select all

{assign var='hidefeu' value='1'}
{assign var='hidesearch' value='1'}
{assign var='privatepage' value='1'}
Hi Calguy,
that's exactly what I need, but I can't get it to work. Here's what I did:
In one of my page's I added options->metadata:

Code: Select all

{assign var='whatever' value='1'}
In the template for that page I added:

Code: Select all

{if !isset($whatever)}<h5>Whatever</h5>{/if}
No effect. No error messages in the source code of the page neither.

I tried the following:
I cleared the cms cache and the browser cache.
I used the global metadata instead of the page's.

Are there any other settings I shoud take care of (config.php or so)?

Thanks!
Caspar
sig
Forum Members
Forum Members
Posts: 10
Joined: Mon Jul 12, 2004 6:46 pm

Re: Customizing your template based on the current page

Post by sig »

Caspar wrote: Hi Calguy,
that's exactly what I need, but I can't get it to work. Here's what I did:
In one of my page's I added options->metadata:

Code: Select all

{assign var='whatever' value='1'}
In the template for that page I added:

Code: Select all

{if !isset($whatever)}<h5>Whatever</h5>{/if}
No effect. No error messages in the source code of the page neither.
It's because you have set the value 'whatever' to 1, and it'll be echoed only if it's not set (!isset)

Thus you should use:

Code: Select all

{if isset($whatever)}<h5>Whatever</h5>{/if}
Now as the variable 'whatever' is set, the text 'Whatever' is printed.
Caspar

Re: Customizing your template based on the current page

Post by Caspar »

:-X (Sorry for being so stupid!)

THANKS!
Caspar
Locked

Return to “Tips and Tricks”