PHP Template engine : Mun template

조건문과 반복문, 변수 출력, 변수 전달 이렇게 최소한의 기능만 파싱해서 eval로 넘겨주는
엄청 단순한 PHP 템플릿 엔진을 하나 만들어봤다.

기존에 나와있는 템플릿 엔진(Smarty나 Twig 등)을 사용하지 않고
직접 만든 이유는 기존에 제작해둔 MVC 프레임워크가 있는데 거기에 적용하려고 만든 것이다.

https://github.com/munsiwoo/mun-template


Preview

<!DOCTYPE html>
<html>
<head>
    <title>Mun Template</title>
</head>
<body>
    <h1>Mun Template</h1>
    <p>Mun Template is Simple PHP Template engine.</p>
    <h3>for statement</h3>
    @mun for($i=0; $i<10; $i++)
        @mun for($j=0; $j<$i; $j++)
            *
        @endfor
        <br>
    @endfor
    <hr>
    <h3>if statement and print variable</h3>
    @mun if(!isset($_GET[0]))
        plz input $_GET[0]
    @mun elif($_GET[0] == "munsiwoo")
        hello, admin!
    @mun else
        your input is "@var($_GET[0])"
    @endif

    <hr>
    <h3>print passed variable</h3>
    hello, @var($name)
</body>
</html>

muntemplate


MunTemplate README.md

Simple PHP Template engine
Made by munsiwoo


  • How to use?
<?php
include 'MunTemplate.class.php'; # first include class

$MunTemplate = new MunTemplate('./templates/'); # set path

$my_name = 'munsiwoo';
$MunTemplate->render_template('main.html', ['name'=>$my_name]);

Statements

  • How to use for statement?
@mun for($num=0; $num<10; $num++)
    num is @var($num+1)<br>
@endfor
  • How to use if statement?
@mun if(true)
    <h1>TRUE</h1>
@mun else
    <h1>FALSE</h1>
@endif
  • How to use variable?
<p>your name is @var($name)</p>
  • How to pass variable?
<?php
/* index.php */
render_template('main.html', ['var1'=>'1', 'var2'=>array(1,2,3)]);
<!-- main.html -->
var1 is @var($var1)<br>
var2 is @var($var2)