Category: Programming | Posted date: 2023-06-22 01:48:54 | Posted by: Admin
Smarty is a single platform that streamlines and automates daily processes for sales, invoicing, and promotional activities.
What is Smarty?
For small business owners, independent contractors, and entrepreneurs, Smarty is a single platform that streamlines and automates daily processes for sales, invoicing, and promotional activities. Users can use a single platform through the system to manage their CRM, email marketing, campaigns, lead generation, sales, invoices, and accounting.
Key benefits of using Smarty
How does it work?
Smarty builds PHP scripts from copies of the templates internally. In this way, both the syntax of the template tag and the speed of PHP are advantageous. When a template is called, it is initially compiled; afterwards, the compiled versions are used. Instead of maintaining the created versions, the template designer simply needs to manage the Smarty templates because Smarty takes care of this for you. This tactic maintains the templates easy to maintain while maintaining extremely fast execution times. The compiled versions of the scripts are PHP, thus OPC or ZendCache still work on them.
Sites using Smarty
The Smarty website receives tens of thousands of unique visits every day, the majority of whom are developers reading documentation. Smarty is used by many well-known PHP projects, including XOOPS CMS, CMS Made Simple, and Tiki CMS/Groupware, to mention a few.
The basics
The markup-like syntax used by Smarty may be recognizable to you from HTML or XML. All Smarty tags and functions start with a left curly bracket and end with a right curly bracket, just like the > in HTML. When template files contain Smarty code, they are named and have the.tpl extension. They resemble standard HTML files that have additional Smarty tags. The template engine will render these files, which will result in accurate HTML. The example that follows defines a simple tag.
Example: Simple Smarty tag
{s name="shopName"}DemoShop{/s}
The "s" element, which is used to define text as a snippet and make it translateable, is utilized in our sample. As with other markup languages, some Smarty tags wrap the content with a starting and a closing tag. Additionally, they can have other qualities, such as the name="" snippet in our example.
So, in addition to our standard HTML code, Smarty also provides us with a variety of tags and actions. Why then do we require them? What can we use them for? Take a deeper look.
Variables
The data we receive from the shop system is first output using Smarty. Therefore, the data required to display the requested content is served to every page that the template engine renders. In so-called template variables, the data is kept. With a starting $ and the variable name, Smarty allows access to all template variables.
Example: Variable output
<h2>{$sArticle.articleName}</h2>
This brief example will output the variable's content in a typical h2> element. The separation. It is possible to obtain subvalues by using the variable name. As demonstrated in this example, a template variable can contain a huge collection of data, known as an array, in addition to simple string or numeric data. In our case, the field name is one of several fields in an array contained by the template variable sArticle. You can nest template variables even further. In our template code, you can encounter anything similar to $sArticle.image.thumbnails[0].source. With the [] syntax, which you may be familiar with from PHP, you can also access all fields of an array.
Modifiers
Sometimes we want to alter the values of a variable before they are printed in addition to just printing its contents. Smarty modifiers come into play in this situation. You can alter the output by applying a tiny function to the template variables.
Example: Applying a modifier to a variable
<div>
{$sArticle.description|truncate:120}
</div>
As you can see, the modifier is applied by merely following the variable name with a | and the name of the modifier. The description in this example is truncated to a specified maximum number of characters using the truncate modifier. The modifier receives additional parameters via: such as the value of 120.
By aligning them, you may also apply many modifiers to a single variable. They are implemented in the order that they are added. For instance, you would place the appropriate modifier right before the truncate modifier if you wanted to remove the HTML elements from the description variable before truncating it.
Example: Applying two modifiers
<div>
{$sArticle.description|strip_tags|truncate:120}
</div>
Conditions
Depending on your choices, the output might need to be changed. You can do a check of a variable against a condition to determine what is being rendered. A initial "if" tag, which includes the check, and a closing "/if" tag define a basic condition. Only when the check is true is the content between these tags rendered.
Example: Defining a condition
{if $sArticle.description}
{$sArticle.description}
{else}
{$sArticle.description_long|strip_tags|truncate:120}
{/if}
To verify whether the variable is defined, we write a straightforward condition in the example. As you can see, the condition is enclosed by a "else" element that, if true, renders a fallback content. Additional tests can be added with the "elseif" operator to further nest conditions. We perform a very basic check in our example, but you may combine different operators to create more intricate conditions. An exhaustive list of all potential operators is available here.
Loops
We can generate dynamic output by looping through a larger set of data, such as a list of products, to manage the data. For instance, you might automatically create content for each product by iterating through an array containing the data for various products.
Example: Looping through an array
<ul>
{foreach $sArticles as $item}
<li>{$item.articleName}</li>
{/foreach}
</ul>
A foreach tag marks the beginning of the loop, and a closing /foreach tag marks its conclusion. The array we wish to iterate through ($sArticles) and the name of the variable that will hold the single data set during the loop ($item) are both defined in the commencing tag. The code between the "foreach" tags will now be rendered by the template engine for each item in the array, providing the single item in a new variable. In this example, we create a new list item with the product name for each product by iterating through the product data in $sArticles.
Blocks
For logically segmenting template code, smarty blocks are employed. Other files may access these blocks to extend the template at a specific place. In Smarty, you use the "block" tag to define a block's content as a new segment. For each block to specify a distinct name for the segment, a name="" property is required. Using the inheritance system, this name is used to access the block from other files.
Example: Defining a content block in Smarty
{block name="frontend_index_logo"}
<div class="logo--shop">
//...
</div>
{/block}
All blocks from the parent file are made available to you when you inherit a template file using the "extends" function. These are your access points where you can make adjustments or add extra codes.
{block name="frontend_index_logo"}
{$smarty.block.parent}
<div class="shop--slogan">
<h2>My shop is the best!</h2>
</div>
{/block}
The parent block is overridden in the aforementioned example, and after that, we call "$smarty.block.parent" to append our own code to the frontend_index_logo block and insert a slogan immediately following the store logo. The new material for our motto is added immediately after the original code, which is left alone. The procedures below must be taken in order to alter or extend current templates:
Register custom Smarty plugins
The possibility to register your own unique Smarty plugins in your theme was added in Shopware 5. This gives you the ability to develop new Smarty functions and modifiers. Simply construct the required directory structure in your theme directory to register a new Smarty plugin. You can refer to the official documentation to learn more about creating Smarty plugins.
Example: Path to custom Smarty plugins
themes/Frontend/ThemeName/_private/smarty/function.markdown.php
themes/Frontend/ThemeName/_private/smarty/modifier.picture.php
Notice: Mail templates do not support Smarty plugins.