| by munsiwoo | No comments

PHP Template engine : Mun template

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

Smarty나 Twig를 사용하지 않고 직접 만들어서 사용하는 이유는
하나부터 열까지 직접 만들어보는 PHP프레임워크에 적용하기 위해서다.

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)

Leave a Reply