Grow your CSS skills. Land your dream job.

preg_replace help?!

  • # July 14, 2010 at 8:17 am

    Hi,

    I’m trying to make a templating system that replaces certain patterns (using preg_replace), like {{ variable }} would be turned into <?=$variable?> in the document – does that make sense? So, here’s my code for the document:

    Code:
    include(‘classes/template_classes.php’);
    $template = ”
    < !DOCTYPE HTML>



    This is my site, by {{ author }}

    {% foreach news as article %}

    {{ article['title'] }}
    {{ article['body'] }}

    {% else %}

    There are currently no articles

    {% endforeach %}



    “;
    $tpl = new Template;
    $tpl->replace_variables($template);

    And here is the code I have that should be replacing at LEAST the ‘{{ title }}’ area right now…

    Code:
    final class Template
    {
    public function replace_variables($template)
    {
    $replace = preg_replace(“/{{([.*]+)}}”,”< ?=$\1?>“,$template);

    //echo $tempalte;

    //find anything that exists within {{ * }}
    //find out what * is
    //replace it how it’s supposed to be replaced
    //if it isn’t definded, say so
    echo $template;
    }
    }

    Please help me understand what I’m doing wrong in my regex so I can make this work. I appreciate any help. I have looked at tutorials for days and can’t figure it out!!!

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".