CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PHP – Mixing Smarty tags with PHP code

PHP is used to develop sever side scripts and it is embedded with in HTML code. Smarty Template Engine is used to create HTML templates that are used in PHP scripts.

PHP validates the code based on its validation rules. We should follow these rules while developing PHP scripts. The same way, Smarty Template Engine validates the code based on its rules; once the rules are followed; code is compiled to PHP code.

PHP scripts starts with <?php and ends with ?>. Smarty tags are enclosed in open and closed curly braces { and }.

Smarty allows to mix-up PHP code with Smarty tags. But you should enclose your PHP code with in {php} and {/php} tags.

Lets take simple example:

{php}
   function samplefunction() {
      echo 'Simple Message';
   }
{/php}

We can use the function using below Smarty tag:

{samplefunction}

This is the simple way we can mix-up Smarty and PHP code.

One more important thing is the variables defined in PHP code should be available in Smarty tags. In order to do this we need to assign these variables using assign function. Below is the example:

{php}
   $some_php_var = "some value";

   $this->assign('some_smarty_var', 'some_php_var');
{/php}

Accessing a variable using Smarty tag is simple. Below is the code:

{$some_smarty_var}

Like this Smarty allows to mix-up PHP code with Smarty tags.

**

PHP – Mixing Smarty tags with PHP code

Leave a Reply

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

Scroll to top