3 good reasons not to use php template engines! Say NO to Smarty!

Friendly speaking I thought that I would never touch this topic,  but recently I had to apply changes and fixes to one project and was really surprised to see template engine that uses str_replace...

Resons for not using template engine
  1. PHP is a Hypertext Preprocessor and can be embedded into PHP. It is the main benefit of PHP. So, why one should refuse it?
  2. Speed! Really, even when you are using "cool template engines" (no names here) with caching they will be much slower than plain PHP. Especially if you are enabling Eacelerator & optimize your code.
  3. New language really? Each template engine uses its own language at least for iterations, so one should learn it. What is the reason? is it more functional than PHP?

Problems & solutions

I understand that design should be separated from the program logic - just do not put the logic into the template! I understand that developers want to have one main template with placeholders for modules & templates for modules. PHP can handle this too, and I am sure you know it.

Just some code for illustration. Very basic template handling class:

class Tpl{
	static public $Vars = array();
	
	static public function Add($var, $val, $context){
		self::$Vars[$context][$var] = $val;
	}

	static public function Out($file){
		extract(self::$Vars['core'], EXTR_OVERWRITE | EXTR_REFS);
		extract(self::$Vars[$file], EXTR_OVERWRITE | EXTR_REFS);
		require("./templates/{$file}.html");
		self::$Vars[$file] = array();
	}
	
	static public function Get(){
		extract(self::$Vars['core'], EXTR_OVERWRITE | EXTR_REFS);
		extract(self::$Vars[$file], EXTR_OVERWRITE | EXTR_REFS);
		ob_start();
		require("./templates/{$file}.html");
		self::$Vars[$file] = array();
		return ob_get_clean();
	}
}

Script code:

include("class.Tpl.php");

Tpl::Add("V1", "Hello", 'test');
Tpl::Add("V2", "World!", 'test');
Tpl::Out("test");

And template:

<html>
<head>
<title><?=$V1?> <?=$V2?></title>
</head>
<body>
Working<br/>
<?=$V1?> <?=$V2?>
</body>
</html>


Sure, in real projects I am using more complicated solution with HTML cache, controls and validators, but is is using PHP as a template language.

Erlang captcha code for websites

Today I want to publish simple Erlang captcha library. It uses very simple algorithm - code & captcha image is generated and file name contains code itself. So, if file exists, than code entered correctly. Once code is entered, file is deleted. One can reload (in terms of web) or generate new code. In this case old file will be removed and new generated.

Sorrym but this article have been moved to my new blog on erlycoder.com

Read complete article: Erlang captcha code for websites

How to send email with attachment from Erlang

Today I am going to publish small script for sending email from Erlang. It is quite simple. The idea was taken from some other script (not mine, but I do not know who is an author. Sorry for that). Actually many linux users or at least admins will find it familar.
Main difference from original code, that I have found in the Internet, is that this script allows attachments. It does not cover all possible cases, but you can simply modify it for your needs.

Sorry, but this article was permanentaly moved to my new blog erlycoder.com

Read complete article: How to send email with attachment from Erlang

Erlang convert IP to integer & integer to IP.

Jut needed to convert ip to integer for Erlang project and have not found code via Google. So decided to post it here for anybody who will find it helpful.

First function converting IP tuple to integer:

ip_to_int({A,B,C,D}) -> (A*16777216)+(B*65536)+(C*256)+(D).

Code to convert integer to IP tuple:

int_to_ip(Ip)->	{Ip bsr 24, (Ip band 16711680) bsr 16, (Ip band 65280) bsr 8, Ip band 255}.

And brief dumb example:

Eshell V5.8.4  (abort with ^G)
1> Ip = {1,2,3,4}.
{1,2,3,4}
2> db_server:int_to_ip(db_server:ip_to_int(Ip)).
{1,2,3,4}
3> 

Hope this code helped you, if you have reached this page :)

What is Erlang open_port and os:cmd?

Sometimes it happens that you need to execute external command. Same happens when you are programming Erlang. Usually it is enough to use os:cmd. It executes the command and returns the result. Like the following

LsOut = os:cmd("ls"), % on unix platform

But sometimes you need to start the process and communicate with it via standard input and output. For example most of linux users know GnuChess program. And may be somebody even tried to start it from the shell without GUI. So, the following simple example will show how to start GnuChess and communicate with the process from Erlang.

-module(pipe).
-author('Sergiy Dzysyak ').

-compile(export_all).

start()->
	spawn(?MODULE, read, []).
	
read() ->
  Port = open_port({spawn,"/usr/bin/gnuchess -xe"},[binary,{line, 255}]),
  do_read(Port).

do_read(Port) ->
  receive
    {Port,{data,Data}} ->
    	io:format("Data: ~p~n",[Data]);
    {Port,eof} ->
      read();
    {go, Go} ->
    	Port ! {self(), {command, Go}};
    Any ->
      io:format("No match fifo_client:do_read/1, ~p~n",[Any])
  end,
  do_read(Port). 

Example is very basic and to stat it you should do the following:

[serg@localhost erl.chess]$ erl
Erlang R14B03 (erts-5.8.4) [source] [64-bit] [rq:1] [async-threads:0] [kernel-poll:false]

Eshell V5.8.4  (abort with ^G)
1> c(pipe).
{ok,pipe}
2> Pid = pipe:start().
<0.38.0>
3> Pid ! {go, <<"e2e4\n">>}.
{go,<<"e2e4\n">>}
Data: {eol,<<"Chess">>}
Data: {eol,<<"Adjusting HashSize to 1024 slots">>}
Data: {eol,<<"1. e2e4">>}
Data: {eol,<<"1. ... e7e5">>}
Data: {eol,<<"My move is: e7e5">>}
4> 

In a similar way communication via linux named pipes can be organized with external programs or even more complicated goals may be achieved. I have not tried to load drivers & communicate with them via port_open, but if I will get a minute, I will try and describe it here in my blog...