php code in smarty

Your can write your php code in your smarty template and its true. But you have to check for some global settings of smarty to do this.

There is a smarty variable “$php_handling” that decides how the php code should be handled by the smarty engine. The possible four values of this variable are:

  • SMARTY_PHP_PASSTHRU – Smarty echos tags as-is.
  • SMARTY_PHP_QUOTE – Smarty quotes the tags as html entities.
  • SMARTY_PHP_REMOVE – Smarty removes the tags from the templates.
  • SMARTY_PHP_ALLOW – Smarty will execute the tags as PHP code.

from the above four SMARTY_PHP_PASSTHRU is the default setting.

you can change this variable value to work with php code as follows:

<?php

require(‘Smarty.class.php’);
$smarty = new Smarty;

$smarty->php_handling = SMARTY_PHP_ALLOW;

?>

after enabling this varialbe to allow the php code in smarty, to write php code in smarty you just have to write the php code inside the {php}{/php} tags.

i.e. {php}

//your php code here

{/php}

One more thing, there is another variable ‘$security_settings’.

These settings are used to override when security is enabled. The possible settings are:

  • PHP_HANDLING – true/false. If set to true, the $php_handling setting is not checked for security.
  • IF_FUNCS – This is an array of the names of permitted PHP functions in IF statements.
  • INCLUDE_ANY – true/false. If set to true, any template can be included from the file system, regardless of the $secure_dir list.
  • PHP_TAGS – true/false. If set to true, {php}{/php} tags are permitted in the templates.
  • MODIFIER_FUNCS – This is an array of the names of permitted PHP functions used as variable modifiers.
  • ALLOW_CONSTANTS – true/false. If set to true, constants via {$smarty.const.name} are allowed in the templates. The defaults is set to “false” for security.

to change or access these setting you can write the code as follows:

$smarty->security_settings['ALLOW_CONSTANTS'] = true;

Alternative to ‘insert’ in mysql

There is one more method other than using “insert” statement to insert data in mysql tables. This method is to load data from a text (.txt) file. The text file from which you want to insert data into the given table should be in proper format.

Here’s an example how to do it:

suppose this is the table structure to which we want to insert data from a myfriends.txt file.

if you don’t have then you can create a table as given below:

CREATE TABLE myfriends (name VARCHAR(20), location VARCHAR(20), sex CHAR(1), birth DATE);

and the myfriends.txt should be like this:

Fluffy~India~f~1993-02-04#
Claws~US~m~1994-03-17#
Buffy~UK~f~1989-05-13#

and the statement to load the above given data goes as below:

mysql> load data local infile ‘myfriends.txt’ into the table myfriends fields terminated by ‘~’ line terminated by ‘#’;

where ‘~’ is the fields separator and ‘#’ is the row or line separator.

also the myfriends.txt file should be in the same directory where the bin folder is present.

Hope this helps you,
Sachin (samsami2u@gmail.com)

Follow

Get every new post delivered to your Inbox.