mohamedhelal/arabtemplate

arabTemplate模板系统,用于区分代码和设计

1.0.0 2017-01-05 17:25 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:43:58 UTC


README

愿平安与真主慈悯你们,arabTemplate模板系统,阿拉伯模板版本10,从零开始重新编程和开发。请试用并指出出现的任何错误。

奉真名至大安拉

首先,通过包管理器composer安装

composer require mohamedhelal/arabtemplate

// 创建类的副本

$artpl = new \ArTemplate\ArTemplate([
    // اضافة مجلد القوالب
    'template' => realpath('path'),
    // مجلد الملفات المحولة
    'compiler' => realpath('path'),
    // تفعيل وإلغاء الكاش
    'caching'         => false,
    // مجلد ملفات الكاش
    'cache'    => realpath('path')
]);

调用模板

$artpl->display('index');

// 或者

echo $artpl->fetch('index');

传递变量到模板

$artpl->assign('obj', 'MyTest' );
$artpl->with('obj', 'MyTest' );

在模板中使用变量

{{ $var }}

在模板中使用数组

{{ $row.key }}
{{ $row[key] }}
{{ $row[$key.name] }}

在模板中使用类

{{ $obj->property }}
{{ MyClass::$property }}
{{ MyClass::$property.key.name }}
{{ $obj::$property }}
{{ $obj::$property.key.name }}

在模板中使用函数

{{ myName($row,'mohamed') }}
{{ $obj->method('name') }}
{{ MyClass::method('name') }}
{{ $obj::method('name') }}

在模板中使用类

类的示例

class MyTest
{
    public static $Myname = "Mohamedhelal";
    public static $array  = array('names' => array('first' => 'Mohamed'));
    public static function setMyName($val)
    {
        self::$Myname = $val;
        return new self();
    }
    public function getThis()
    {
        return $this;
    }
    public function getName()
    {
        return self::$Myname;
    }
}

在模板内部

{{ $obj::setMyName('Mohamed')->getThis()->getThis()->getThis()->getThis()->getName() }}

或者

{{ MyTest::setMyName('Mohamed')->getThis()->getThis()->getThis()->getThis()->getName() }}

在模板中调用模板

{{ include file="index" }}


{{ include 'index'  }}
{{ include $var  }}

从模型目录调用模板

$artpl->setModuleDir('test', dirname(__FILE__).'/modules/test/views/');
$artpl->setModuleDir('users', dirname(__FILE__).'/modules/users/views/');

从模型目录显示模板

$artpl->display('test::index');
$artpl->display('users::index');

或从模型内部调用模板

{{ include file="test::index" }}
{{ include $var }}

在模板中创建变量

{{ $name = 'mohamed helal' }}
{{ $i = 2 }}
{{ ++$i }}
{{ --$i }}
{{ $i *= 2 }}
{{ assign('my','value') }}
{{ with('my','value') }}

在模板中使用其他名称的函数

$artpl->setFunction('ReturnArray', 'MyTest::getMyName');
{{ ReturnArray($rows) }}
{{ $myfunc = ReturnArray($rows) }}

在模板中使用函数而不打印它

{{ |function_name($var,...)| }}

在模板中创建函数

 
        {{ function createMenuMapList($row,$mylinks) }}
        	{{ $row->name }} || {{ $mylinks }}
        {{ /function }}
        

从模板中调用创建的函数

{{ createMenuMapList($row,$mylinks) }}

使用web foreach

{{ foreach $rows as $row }}
	{{ $row@key }}
   {{ foreachelse}{
{{ /foreach }}

{{ foreach $rows as $key => $val }}
   {{ foreachelse }}
{{ /foreach }}

使用 key => val 进行重复

{{ foreach $rows as $key => $val }}
   {{ foreachelse }}
{{ /foreach }}

使用对象变量

{{ foreach $rows as $row }}
   {{ $row@index }}
   {{ $row@first }}
   {{ $row@last }}
   {{ $row@first }}
   
   {{ $rows@count() }}
   
   {{ $row@is_div_by(2) }}
   
   {{ $row@is_even_by(2) }}
   
{{ /foreach }}

使用 for 循环

	{{ for $i = 0;$i < 10;$i++ }}
		{{ $i }}
	{{ /for }}

使用多循环 for

	{{ for $i = 0,$j = 0;$i < 10,$j < 10;$i++,$j+=2 }}
		{{ $i }}
		{{ $j }}
	{{ /for }}

使用 break|continue

{{ break|continue }}

在模板中使用条件

{{ if $name =="mohamed" }}
// do same thing
{{ elseif $name =="helal" }}
// do same thing
{{ else }}
// do same thing
{{ /if }}

使用短条件

{{ $var == 'mohamed'?true:false }}

合并变量

{{ $var ."MohamedHelal" }}

注释

{{*
	// تعليقات  لن يتم معلجنها
	{{ $var }}
*}}

模板的继承

parent.tpl

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{ block 'header' }}My Default Page  Title {{ /block }}</title>
</head>
<body>
	{{ block 'body' }}
		My Default Page  Content
	{{ /block }}
</body>
</html>

son.tpl

{{ extends file="parent" }}
{{ extends "parent" }}
{{ extends $layout }}

{{ block "header" }}
	My Extend Page Header
{{ /block }}


{{ block "body" }}
	My Extend Page Content
{{ /block }}

输出

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>
	My Extend Page Header
</title>
</head>
<body>
	
	My Extend Page Content

</body>
</html>