Smarty Variables in PrestaShop: The Complete Guide

Smarty Variables in PrestaShop - The Complete Guide

Introduction
Welcome to a comprehensive guide to Smarty variables in PrestaShop!
If you are involved in creating or modifying templates in PrestaShop, knowledge of Smarty variables is absolutely essential.
In this article, we will discuss what Smarty variables are, why they are important, and how to use them effectively when creating templates in PrestaShop.

What are Smarty variables?
Smarty is a template engine used in PrestaShop that allows you to separate business logic (PHP) from presentation (HTML).
Smarty variables are special tags that act as placeholders for dynamic data that will be inserted when the page is rendered.

Info
Smarty variables in PrestaShop are always surrounded by curly brackets and preceded by a dollar sign, for example. {$product}.

Why are Smarty variables important?
Smarty variables are the basis of dynamic data presentation in PrestaShop store. Thanks to them you can:
  • Display information about products, categories, customers, etc.
  • Create conditional display of elements on the page
  • Iterate through collections of data (such as a list of products)
  • Format the data as needed
  • Localize the store for different languages

How to use Smarty variables in PrestaShop templates?
The basic syntax of Smarty variables is simple:
{$name_variable}

You can also access object properties using dot syntax:
{$product.name}
{$product.price}

Or using the syntax of arrays:
{$product['name']}
{$product['price']}


Examples of using Smarty variables
Displaying the product name:
{$product.name}

Conditional display of promotional price:
{if $product.has_discount}
  <span class="regular-price">{$product.regular_price}</span>
  <span class="price">{$product.price}</span>
{else}
  <span class="price">{$product.price}</span>
{/if}

Iterate through the list of products:
{foreach from=$products item="product"}.
  <div class="product">
    <h3>{$product.name}</h3>
    <p>{$product.description_short nofilter}</p>
    <span class="price">{$product.price}</span>
  </div>
{/foreach}

Warning
Remember that the availability of Smarty variables depends on the context of the page.
For example, product variables will only be available on product pages.


The most important Smarty variables in PrestaShop
The following table shows the most important Smarty variables available in PrestaShop, along with their description and context of use:
VariableDescriptionContext
{$shop}Store information (name, address, email, etc.).Global
{$currency}Information about the current currencyGlobal
{$customer}Logged-in customer informationGlobal (when the client is logged in)
{$language}Information about the current languageGlobal
{$cart}Customer shopping cart informationGlobal
{$page}Information about the current siteGlobal
{$product}Product detailsProduct page
{$products}List of productsCategory page, search
{$category}Information about the current categoryCategory page
{$categories}List of categoriesHome page, menu
{$manufacturer}Manufacturer informationManufacturer's website
{$supplier}Supplier informationSupplier's website
{$cms}CMS website informationCMS website
{$link}Link generation facilityGlobal
{$urls}Useful URLs (images, js, css, pages)Global
{$breadcrumb}Navigation path informationGlobal
{$static_token}Token to secure formsGlobal


Detailed table of Smarty variables in PrestaShop
SEE FULL TABLE



Best practices for working with Smarty variables
Tip
Here are some tips to help you work effectively with Smarty variables in PrestaShop:
1. debugging variables - use the modifier {debug} or {$variable|print_r} to see the contents of the variable.

2 Checking the existence of variables - always check if a variable exists before using it:
{if isset($product.description_short) && $product.description_short}
  {$product.description_short nofilter}
{/if}
3. use modifiers - Smarty modifiers allow you to format data, such as:
{$product.name|escape:'html':'UTF-8'}
{$date|date_format:'%d/%m/%Y'}
4. remember the filters - use nofilter for HTML content that should not be filtered:
{$product.description nofilter}
5. enable developer mode - In the PrestaShop admin panel to see Smarty errors.



Summary
Smarty variables are a powerful tool in creating dynamic templates in PrestaShop.
They allow users to easily display data, create flexible page layouts and personalize the user experience.
Knowing the available variables and their properties is essential for any PrestaShop developer.
Info
I hope this guide will help you better understand and more effectively use Smarty variables in PrestaShop.
If you have any questions, feel free to comment below!

Leave Your comment

Your email address will not be published. Required fields are marked *

Scroll To Top

Recently viewed products

PrestaShop Banner

(0)
$49,00 VAT excluded

WordPress OLX integration

(1)
$45,00 VAT excluded

Login

Shopping Cart

Close
Buy $79,46 more to get Free installation