Difference between revisions of "Using the Page Class Suffix in Template Code"

From Joomla! Documentation

(Update red link)
 
(5 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Sometimes web designers need to access a [[Tutorial:Using_Class_Suffixes_in_Joomla!_1.5#Page_Class_Suffix|page class suffix]] from directly within a template. A common reason to do this is to assign unique styles to individual pages linked from a menu (e.g. to change the dominant color on that page). By default Joomla applies the page class suffixes only to limited areas of a page, which often doesn't offer enough flexibility. However, by making some simple changes to your template you can take advantage of the cascading nature of stylesheets to apply unique styles to any element on a page.
+
{{version|1.5}}
 +
Sometimes web designers need to access a [[Using_Class_Suffixes#Page_Class_Suffix|page class suffix]] from directly within a template. A common reason to do this is to assign unique styles to individual pages linked from a menu (e.g. to change the dominant color on that page). By default Joomla applies the page class suffixes only to limited areas of a page, which often doesn't offer enough flexibility. However, by making some simple changes to your template you can take advantage of the cascading nature of stylesheets to apply unique styles to any element on a page.
  
 
== Load Suffix ==
 
== Load Suffix ==
Line 20: Line 21:
 
To load the page class suffix associated with the active menu item, add this to the top of the index.php file: (For sub-pages with no active menu item, this will load the page class suffix for the default menu item.)
 
To load the page class suffix associated with the active menu item, add this to the top of the index.php file: (For sub-pages with no active menu item, this will load the page class suffix for the default menu item.)
 
<source lang="php">
 
<source lang="php">
 +
 
<?php
 
<?php
  $menu = &JSite::getMenu();
+
  $menus      = &JSite::getMenu();
  $active = $menu->getActive();
+
  $menu      = $menus->getActive();
  $pageclass = "";
+
  $pageclass   = "";
  if (is_object( $active )) :
+
 
    $params = new JParameter( $active->params );
+
  if (is_object( $menu )) :
    $pageclass = $params->get( 'pageclass_sfx' ));
+
  $params = new JParameter( $menu->params );
  endif;  
+
  $pageclass = $params->get( 'pageclass_sfx' );
 +
  endif;  
 
?>
 
?>
 +
 
</source>
 
</source>
 
(credit: [http://forum.joomla.org/viewtopic.php?p=1353495#p1353495 Page Class Suffix in template code])
 
(credit: [http://forum.joomla.org/viewtopic.php?p=1353495#p1353495 Page Class Suffix in template code])
===Secruity===
+
 
 +
The above code will throw an exception in 2.5 and higher. Code below works:
 +
<source lang="php">
 +
 
 +
<?php
 +
  $app = JFactory::getApplication();
 +
  $menu = $app->getMenu()->getActive();
 +
  $pageclass = '';
 +
 
 +
  if (is_object($menu))
 +
    $pageclass = $menu->params->get('pageclass_sfx');
 +
?>
 +
 
 +
 
 +
</source>
 +
 
 +
===Security===
 
You should always use htmlspecialchars() in your code before writing something into an HTML attribute, else you open up an attack vector to inject script code into your page.
 
You should always use htmlspecialchars() in your code before writing something into an HTML attribute, else you open up an attack vector to inject script code into your page.
 +
 
== Insert Suffix ==
 
== Insert Suffix ==
 
The next step is to use the page class suffix somewhere in the template.  
 
The next step is to use the page class suffix somewhere in the template.  

Latest revision as of 02:43, 17 May 2013

Sometimes web designers need to access a page class suffix from directly within a template. A common reason to do this is to assign unique styles to individual pages linked from a menu (e.g. to change the dominant color on that page). By default Joomla applies the page class suffixes only to limited areas of a page, which often doesn't offer enough flexibility. However, by making some simple changes to your template you can take advantage of the cascading nature of stylesheets to apply unique styles to any element on a page.

Load Suffix[edit]

First, we need to find out what the page class suffix is for the page we are visiting. To do this, you will need add some code to your template:

  1. Open your template's index.php file (located in /templates/*template-name*/)
  2. Find the <head> tag in the index.php, near the top area of the template
  3. Above this, insert one of the following code blocks (Either of the following options will work fine in most cases. However they work slightly differently, so in certain cases your needs may dictate a specific choice.)

By Itemid[edit]

To load the page class suffix associated with the current Itemid, add this to the top of the index.php file:

<?php
  $itemid = JRequest::getVar('Itemid');
  $menu = &JSite::getMenu();
  $active = $menu->getItem($itemid);
  $params = $menu->getParams( $active->id );
  $pageclass = $params->get( 'pageclass_sfx' );
?>

By Active Menu Item[edit]

To load the page class suffix associated with the active menu item, add this to the top of the index.php file: (For sub-pages with no active menu item, this will load the page class suffix for the default menu item.)

<?php
   $menus      = &JSite::getMenu();
   $menu      = $menus->getActive();
   $pageclass   = "";
   
   if (is_object( $menu )) :
   $params = new JParameter( $menu->params );
   $pageclass = $params->get( 'pageclass_sfx' );
   endif; 
?>

(credit: Page Class Suffix in template code)

The above code will throw an exception in 2.5 and higher. Code below works:

<?php
  $app = JFactory::getApplication();
  $menu = $app->getMenu()->getActive();
  $pageclass = '';

  if (is_object($menu))
    $pageclass = $menu->params->get('pageclass_sfx');
?>

Security[edit]

You should always use htmlspecialchars() in your code before writing something into an HTML attribute, else you open up an attack vector to inject script code into your page.

Insert Suffix[edit]

The next step is to use the page class suffix somewhere in the template.

In the Body Tag[edit]

The more common method would be to apply the page class suffix as an id or class to the <body> tag. Find the <body> tag (below the </head> tag) and replace it with this:

<body id="<?php echo $pageclass ? htmlspecialchars($pageclass) : 'default'; ?>">

To Load a Page-Specific Stylesheet[edit]

The second method would be to load a stylesheet unique to the page in question. Instead of modifying the <body> tag, look for the stylesheet link within the <head></head> tags and add the following line directly beneath it:

<link rel="stylesheet" href="templates/<?php echo $this->template ?>/css/<?php echo htmlspecialchars($pageclass) ?>.css" type="text/css"/>

Benefits[edit]

Using this approach to implement page class suffixes rather than the default approach has several benefits:

  • leaner CSS
  • much greater flexibility in styling any element on the page
  • compliments and simplifies template overrides