# SEO - Dynamic Descriptions

> Auto-fill missing meta titles and descriptions, reposition category copy below the product grid, and template descriptions with a sandboxed Twig engine.

## General Information

Two everyday SEO problems show up on most Magento stores. First: thousands of products and categories ship with empty meta titles or meta descriptions because nobody had time to write them all — and Magento's default fallback (an empty `<meta>` tag) is worse than a generated one. Second: the long, keyword-rich category description that's great for SEO sits *above* the product grid where it pushes products below the fold and tanks conversion.

The Qoliber SEO Dynamic Descriptions extension fixes both: it auto-fills missing meta fields from the entity itself, lets you template category descriptions with a safe Twig engine, and repositions the long-form copy below the grid where it earns SEO without hurting UX.

## What Does This Extension Do?

- **Auto-fill empty meta titles** on products and categories — uses the product/category name when the meta-title field is blank.
- **Auto-fill empty meta descriptions** — uses the product description when the meta-description field is blank.
- **Reposition category descriptions** — moves the long-form description below the product list and pagination, keeping products above the fold.
- **Strip descriptions on paginated views** — page 2, 3, 4 of a category get the description stripped, eliminating duplicate-content signals across the paginated cluster.
- **Twig templating for category descriptions** — write descriptions with `{% if %}` / `{% for %}` / `{% set %}` and a curated set of safe filters (`escape`, `upper`, `lower`, `trim`, etc.).
- **Sandbox-secured Twig engine** — no method calls, no object access, no template inheritance: just safe data interpolation.
- **Request-scope memoisation** — the same field is rendered at most once per request. No duplicate Twig processing per page load.

## Why Is This a Game Changer?

- **Empty meta tags become a non-problem.** Imported a 50,000-SKU catalogue without meta titles? Every page still gets a usable title and description from day one.
- **Categories convert and rank.** The classic conflict — long description for SEO vs. clean grid for conversion — disappears. Both audiences get what they want.
- **No duplicate content from pagination.** Google sees one canonical description for `?p=1` and treats `?p=2..N` as paginated continuations, exactly as the spec intends.
- **Safe templating, even with junior staff.** The Twig sandbox means a marketer typing `{{ category.name }}` can't accidentally execute arbitrary PHP or leak server state.

## How Does It Work?

Two plugin layers do the work:

1. **Meta-fill plugin** — wraps `Product::getData()` / `Category::getData()` for the meta-title and meta-description fields. When the stored value is empty, the plugin returns the synthesised fallback. Result is memoised per request.
2. **Layout repositioning plugin** — moves the `category.description` block in the layout XML so it renders below `product_list_toolbar_pager`. On paginated requests (`?p=2..N`), the block is removed entirely.

### Example: templated category description

```twig
{% if filters.brand %}
    Browse our collection of {{ category.name }} from {{ filters.brand }}.
{% else %}
    Discover the full range of {{ category.name }} — over {{ category.product_count }} styles in stock.
{% endif %}
```

### Configuration at a glance

Find the module under **Stores → Configuration → Qoliber → SEO: Dynamic Descriptions**. Toggle each behaviour independently — auto-fill, repositioning, pagination strip — per store view.

## What's New in 2.1

- **Critical security hardening** — Twig sandbox enabled, HTML auto-escape on, unsafe `html_entity_decode()` removed from the render pipeline. Template context is now scalars-and-null only.
- **Hot-path bail-out** — meta-field plugin exits in O(1) for unhandled fields, eliminating per-call iteration over the modifier pool.
- **Request-scope memoisation** caches `(event_prefix, entity_id, field)` tuples. Significant TTFB improvement on product-heavy pages.
