Filter Specific Content
Overview
As Magento store owner you can benefit using Filter Specific Content extension, as this extension improves both customer experience and search engine ratings:
- You can provider store customers with targeted information (ads, additional technical information, links to related pages) based on what they search.
- You can make your filtered pages more unique and avoid "duplicate content" issue.
- You can optimize specific search phrases by adding relevant keywords where they matter the most.
Which Content You Can Inject Into the Page
Filter Specific Content extension allows you to add a lot of filter specific content to pages powered by any of MANAdev layered extensions (and actually free version, Multiple Select In Layered Navigation (Filters) is included). Below is a list of places where you can put this content:
- Headings. You can customize page headings:
- Page title (
<h1>
element) - You can also add subtitle which is displayed right after page title (by default it is disaplayed as
<h2>
element).
- Page title (
- Descriptions. You can add filter specific description below category description and you can even alter category description based on which filters are currently applied.
- Meta data. You can modify page meta data important for search engine rankings:
- Page meta title (displayed in browser's tab caption)
- Meta keywords and meta description
- Exclude pages filtered by certain options from crawling (
robots
meta tag).
How It Works
In general, filter content is injected in 4 simple steps:
- Initial assignment. Initially default page content (which would be served without this extension) is put into a list of content variables.
- Modification based on currently applied filters. One or more filter content rules are processed based on currently applied filters. These rules modify content variables.
- Final modification. Final content rule is processed which renders currently applied filters.
- Injection. All content variables are injected in relevant places all over the page.
Below are several examples illustrating these steps in detail. In these examples the follwing page is discussed:
- Page:
Electronics
category page. - Applied filters:
Rating
:4 & Up
Manufacturer
:Apple
Example 1. Adding Content
Lets say you have to add some additional description on all pages filtered by Apple
. You can do this in MANAdev->Layered Navigation
menu by opening Manufacturer
filter, going to Filter Specific Content
tab and assigning the following to Apple
option's Additional Description
field:
<p>Here is my custom content</p>
Once you activate this rule, additional description will appear in frontend.
Example 2. Replacing Content
This example covers how the content of page meta title (<title>
element) is rendered before installing Filter Specific Content extension, just after installing it (default settings) and after creating a simple content rule.
Before installing Filter Specific Content extension, you can see the same page title no matter which filters are applied:
Electronics
Just after installing this extension all aplied filters are added to page title. In our example:
Electronics: Apple and 4 & Up
In System->Configuration
menu there is a final content rule which describes how exactly filters should be added to meta title:
{# Show default page title #}
{# ----------------------- #}
<echo>{{ meta_title }}</echo>
{# Add labels of all applied filters
and separators in between#}
{# ----------------------- #}
{% for option in filters %}
{% if loop.first %}
<echo>: </echo>
{% elseif not loop.last %}
<echo>, </echo>
{% else %}
<echo> and </echo>
{% endif %}
<echo>{{ option.label }}</echo>
{% endfor %}
{# Add page number to page title #}
{# ----------------------- #}
{% if page and page > 1 %}
<echo> (Page {{ page}})</echo>
{% endif %}
After extension installation only final content rule is processed. Lets say that you want meta title to read:
Top Rated Electronics: Apple
In this case you should create a rule which is applied anytime Rating
is filtered by 4 & Up
. You can do it in MANAdev->Layered Navigation
menu by opening Rating
filter, going to Filter Specific Content
tab and assigning the following to 4 & Up
option's Page Title
field:
{% set filters = remove(filters, 'label', '4 & up') %}
<echo>Top Rated </echo>{{ meta_title }}
Once you activate this rule, it will remove 4 & up
from applied filter list after colon (:
) and add Top Rated
before title text.
Initial Variables
Below is full list of all content variables:
- The content of the following variables is actually injected into the page. These variables may be modified by content rules:
- The following variables are replaced by each subsequent content rule:
title
- Title (<h1>
)subtitle
- Subtitle (<h2>
). Shown just after<h1>
.description
- Descriptionmeta_title
- page meta titlemeta_keywords
- page meta keywordsmeta_description
- page meta descriptionmeta_robots
- can beINDEX,FOLLOW
,INDEX,NOFOLLOW
,NOINDEX,FOLLOW
,NOINDEX,NOFOLLOW
- Content rules can add content to the following variables:
additional_description
- Additional Description, shown just after original category description or after CMS page content on CMS pages.
- The following variables are replaced by each subsequent content rule:
- The following variables about current page, currently applied filters and paging are avaiable. You can modify these variables with
{% set %}
statements:filters
- array of applied filters. Each filter has the following fields:label
- Option labelvalue
- Option ID
page
- current product list page number.page_type
- unique page identifier:- For category with ID 18 it equals
category:18
. - For CMS page with ID 18 it equals
cms:18
. - Quick search page identifier is
search
. - For option page with ID 18 it equals
option:18
.
- For category with ID 18 it equals
- The following variables contain initial original content:
initial_title
initial_subtitle
initial_description
initial_meta_title
initial_meta_keywords
initial_meta_description
initial_meta_robots
initial_additional_description
initial_filters
initial_page
Content Rule Language
Content rules use Twig template language.
Use the following syntax to output content variable:
{{ variable }}
You can use certain conditional, loop and variable modification statements:
{% for filter in filters %}
{# ... #}
{% endfor %}
{% if page %}
{# ... #}
{% endif %}
{% set variable = 'value' %}
Comments:
{# this is comment #}
Make sure to check external resources related to Twig templates:
Controlling Whitespace
Normally Twig templates output spaces, tabs and end-of-line characters as they appear in template and this is really unwanted behavior in meta content variables, namely:
meta_title
meta_keywords
meta_description
meta_robots
Consider the following template:
{{ meta_title }}
{% if page and page > 1 %}
(Page {{ page }})
{% endif %}
You might expect
Electronics (Page 2)
But instead you get
Electronics
(Page 2)
To control whitespace, use <echo>
pseudo elements:
<echo>{{ meta_title }}</echo>
{% if page and page > 1 %}
<echo> (Page {{ page }})</echo>
{% endif %}
Inside <echo>
all spaces are preserved exactly as written, outside <echo>
all whitespace is eliminated.
<echo>
pseudo elements are removed from final output, so you get nice meta title as expected:
Electronics (Page 2)
Customizing Content for Certain Category
Lets say you add Top Rated
to each page's meta title when there is 4 & Up
rating filter applied with the following rule:
<echo>Top Rated </echo>{{ meta_title }}
You may decide to customize it for some category, lets say for Apparel
category it should read The Best Apparel
. In this case you can easily configure it by using page_type
variable:
{% if page_type == 'category:18' %}
<echo>The Best Shirts and Shoes</echo>
{% else %}
<echo>Top Rated </echo>{{ meta_title }}
{% endif %}
Custom Twig Functions
In addition to functions described in official Twig documentation, the following functions are also avaiable:
remove(array, field, value)
Returns copy of array
with all elements removed where element's field
equals value
. Example filters
array:
0:
value: 25
label: Red
1:
value: 24
label: Black
The following statement removes Black
item:
{%set filters = remove(filters, 'label', 'Black') %}
filters_are(expected_filters)
Checks whether currently applied filters are exactly the same asspecified in expected_filters array
{% if filters_are({color: 'Black', manufacturer: 'IBM'}) %}
<echo>specific content</echo>
{% else %}
<echo>Default content</echo>
{% endif %}
How To Define A Category Specific Title For An Attribute Value
Typical rule for attribute value, let‘s say for meta title is:
{% set filters = remove(filters, 'label', '4 & up') %}
<echo>Top Rated </echo>{{ meta_title }}
It can be customized for category by checking current page type:
{% set filters = remove(filters, 'label', '4 & up') %}
{% if page_type == 'category:18' %}
<echo>The Best Shirts and Shoes</echo>
{% else %}
<echo>Top Rated </echo>{{ meta_title }}
{% endif %}
Keeping Rules in File System
You may also keep your content rules in file system. Designate some directory inside of Magento root directory for keeping content rules, for instance <magento_directory>/content-rules
.
Add Twig files to it, for instance add title.twig
with the following content:
Hello files: {{title}}
Enable loading content rules from files by setting MANAdev->General->Global Configuration->Twig->Allow Loading From Files
to Yes
.
Finally you can include content rule file in any content rule using % include
directive for instance:
{% include 'content-rules/title.twig' %}
Not that paths used in % include
directive are relative to Magento root directory.