2006-10-25

Totalement crétin

Pub acer
2006-10-13

IE CSS background flicker : bunch of solutions

Making a CSS image based navigation with rollover I got the infamous CSS background flickering bug of Internet Explorer 6. I have lost several hours trying to solve the problem by different ways.

This is the first workaround that works fine for me :

I hope that will help someone to find a solution faster than me.

2006-10-03

A template translator in 20 lines of code

Looking at the architecture of a big CMS I am thinking that my templating software is overcomplicated.

With templight, I have produced a kind of syntax checker and an interpreter for a high level templating language. It's maybe a solution with a compiled language like .NET, Java or C++. But certainly not with a dynamic language like PHP or Python.

Dynamic languages have a big advantage over the others : you can easily produce programs that produce other programs and start to use them immediatly.

If you can understand this fact, you can also understand that it is 10 times more easy to write a templating language interpreter in a dynamic language.

The idea is simple :

  1. Choice a dynamic language for the work
  2. Choice a templating language
  3. Write a program that translate the templating language in the dynamic language

I was able to produce a little translator in minutes that can do most of the basic things needed for a templating language. And it was only 20 lines of PHP. I realize now that my previous work was not required (but it was fun). It's not necessary to re-invent the wheel by producing a compilator and an interpreter. Just use the one provided by the dynamic language.

My templating language file :

<html>
    <head>
        <title>{{block title}}</title>
    </head>
    <body>
        
        <p>In PHP I am sure of that :
            {{if 1==2}}
            1==2
            {{elseif 10>0}}
            10>0
            {{else}}
            nothing interessting
            {{endif}}
        </p>
        
        <ul>
            {{loop testArray}}
            <li>{text}</li>
            {{endloop}}
        </ul>
    </body>
</html>

My translate program (delete the space in $ 1 variables):

<?php
     
function translate_template($c)
{
     // escape the simple quote
     $c = preg_replace('#([^\])\'#si','\'',$c);
     
     // conditionals
     $c = preg_replace('#{{if (.*?)}}#si','\'; if($ 1){ echo \'',$c);
     $c = preg_replace('#{{elseif (.*?)}}#si','\'; }elseif($ 1){ echo \'',$c);
     $c = preg_replace('#{{else}}#si','\'; }else{ echo \'',$c);
     $c = preg_replace('#{{endif}}#si','\'; } echo \'',$c);
     
     // loops
     $c = preg_replace('#{{loop ([\w]*)}}#si','\'; foreach($ as $item){ echo \'',$c);
     $c = preg_replace('#{{endloop}}#si','\'; } echo \'',$c);
     $c = preg_replace('#{([\w]*)}#si','\'; echo $item[\'$ 1\']; echo \'',$c);
     
     // blocks
     $c = preg_replace('#{{block ([\w]*)}}#si','\'; echo $; echo \'',$c);
     
     return '<?php echo \''.$c.'\'; ?>';
}
     
$c = translate_template((file_get_contents('tpl.html')));
$fp = fopen('cache/tpl.html','w');
fwrite($fp,$c);
fclose($fp);

$title = 'My nice Page';
$testArray = array(array('text'=>'hello'),array('text'=>'world'));
include('cache/tpl.html');
     
?>

The output produced by translate_template function :

<?php echo '<html>
    <head>
        <title>'; echo $title; echo '</title>
    </head>
    <body>
        
        <p>In PHP I am sure of that :
        '; if(1==2){ echo '
        1==2
        '; }elseif(10>0){ echo '
        10>0
        '; }else{ echo '
        nothing interesting
        '; } echo '
        </p>
        
        <ul>
        '; foreach($testArray as $item){ echo '
        <li>'; echo $item['text']; echo '</li>
        '; } echo '
        </ul>
    </body>
</html>'; ?>

The final result :