PHP Looping Template Engine - From Scratch

后端 未结 6 1411
鱼传尺愫
鱼传尺愫 2020-12-24 04:46

For a group project I am trying to create a template engine for PHP for the people less experienced with the language can use tags like {name} in their HTML and the PHP will

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-24 05:12

    I had a very basic answer to something KINDA like this back before I started using DOMXPath.

    class is something like this (not sure if it works quite like what you want but food for thought as it works very simple

    template = file_get_contents($template);
    }
    
    function __DESTRUCT()
    {
        //echo it on object destruction
        echo $this->template;
    }
    
    function set($element,$data)
    {
        //replace the element formatted however you like with whatever data
        $this->template = str_replace("[".$element."]",$data,$this->template);
    }
    }
    ?>
    

    with this class you would just create the object with whatever template you wanted and use the set function to place all your data.

    simple loops after the object is created can probably accomplish your goal.

    good luck

提交回复
热议问题