PHP 模板系统

v1.0.1 2016-05-16 13:06 UTC

This package is auto-updated.

Last update: 2024-08-29 04:09:02 UTC


README

小巧且功能强大的模板引擎,语法几乎与laravel/blade相同。

安装

可以通过Composer在项目的composer.json中要求安装"artoodetoo/dirk"包来安装此包。

{
    "require": {
        "artoodetoo/dirk": "dev-master"
    }
}

使用方法

/views/hello.dirk.html

@extends('layout/main')

<h1>Hello {{{ $name }}}!<h1>

{{ $timestamp or 'Timestamp not defined' }}

@section('sidebar')

  @foreach($list as $l)
    <p>{{ $l }} @if($l == 3) is equal 3 ! @endif</p>
  @endforeach

@endsection

/views/layout/main.dirk.html

<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>

<sidebar>

@yield('sidebar', 'Default sidebar text')

</sidebar>

@yield('content')

</body>
</html>

/web/index.php

<?php

require 'vendor/autoload.php';

use R2\Templating\Dirk;

$view = new Dirk([
    'views' => __DIR__.'/views',
    'cache' => __DIR__.'/cache'
]);

$name = '<artoodetoo>';
$list = [1, 2, 3, 4, 5];

$view->render('hello', compact('name', 'list'));

功能列表

输出和注释

  • {{ $var }} - 输出。注意:默认情况下是转义的,就像在Laravel 5中一样!
  • {!! $var !!} - 不转义的原始输出
  • {{ $var or 'default' }} - 使用默认值输出内容
  • {{{ $var }}} - 转义输出内容
  • {{-- 注释 --}} - 一个注释(在代码中,不在输出中)

条件语句

  • @if(condition) - 开始一个if块
  • @else
  • @elseif(condition)
  • @endif
  • @unless(condition) - 开始一个unless块
  • @endunless

循环

  • @foreach($list as $key => $val) - 开始一个foreach块
  • @endforeach
  • @forelse($list as $key => $val) - 开始一个带有空块的foreach
  • @empty
  • @endforelse
  • @for($i = 0; $i < 10; $i++) - 开始一个for块
  • @endfor
  • @while(condition) - 开始一个while块
  • @endwhile

继承和部分

  • @include(file) - 包含另一个模板
  • @extends('layout') - 通过布局扩展模板
  • @section('name') - 开始一个部分
  • @endsection - 结束部分
  • @yield('section') - 输出部分的内通
  • @show - 结束部分并输出其内容
  • @stop - 结束部分
  • @append - 结束部分并将其附加到同名部分
  • @overwrite - 结束部分,覆盖之前的同名部分

许可证

Dirk 是开源软件,许可协议为MIT 许可协议