Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

A quick installer-script with Smarty template function

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Sun Apr 19, 2009 9:39 am    Post subject: A quick installer-script with Smarty template function Reply with quote

I found it interesting today. I am working for my dad, who is not a computer literate. So, I have to simplify my works to become so simple.

I built a page called install.php that setups the application environment. But I must allow the script run only once in the life. No more programming, coz, it might be a complex thing.

Using Smarty template function, it was interesting for me to solve it out.
I checked, if the installer file existed or not. If the file is there, show a link to the installer script. Otherwise, just keep quite.

In the installer script, once it comptes running/installing, it renames itself into a different name, so that it hides from the Smarty template, and makes the application easy to understand again.

Codes:

In Smarty template (index: list-home.php):
Code:
{if 'install.php'|file_exists}
  # link to install.php with more designs, and explanations.
{/if}


The gem is with modifier, file_exists. All PHP callables are possible to use as Smarty modifier function. PHP's if(file_exists('install.php')){ .. } can now be written in Smarty template as {if 'install.php'|file_exists} ... {/if}.

Later in the install.php file, the gem contents are:

Code:
$db = new mysql();

$sqls = array();
$sqls[] = "TRUNCATE query_students;";
$sqls[] = "
INSERT INTO query_codes (
   code_context, code_value
) VALUES (
   'IDENTITY_QUERY', '1'
) ON DUPLICATE KEY UPDATE
   code_value = 1
;";

foreach($sqls as $s => $sql)
{
   $db->query($sql);
}

$datestamp = date('YmdHis').mt_rand(100, 999);
rename(__FILE__, "installed-successfully-{$datestamp}.php");

redirect('index.php');


The rename() function and redirect() function work together. It is probably because the php interpreter reads and parses the contents of install.php earler. So, renaming works. And, the custom function redirect() too works after it.

Renaming the install.php into any other name hides it from the template. Hooray.

Hopefully, this helps for you too. Let me know your feedback.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP