Giter Site home page Giter Site logo

dirk's Introduction

Dirk PHP Templates

Tiny and powerfull template engine with syntax almost the same as in laravel/blade.

Installation

The package can be installed via Composer by requiring the "artoodetoo/dirk" package in your project's composer.json.

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

Usage

/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'));

Feature list

Echoes and comments

  • {{ $var }} - Echo. NOTE: it's escaped by default, like in Laravel 5!
  • {!! $var !!} - Raw echo without escaping
  • {{ $var or 'default' }} - Echo content with a default value
  • {{{ $var }}} - Echo escaped content
  • {{-- Comment --}} - A comment (in code, not in output)

Conditionals

  • @if(condition) - Starts an if block
  • @else
  • @elseif(condition)
  • @endif
  • @unless(condition) - Starts an unless block
  • @endunless

Loops

  • @foreach($list as $key => $val) - Starts a foreach block
  • @endforeach
  • @forelse($list as $key => $val) - Starts a foreach with empty block
  • @empty
  • @endforelse
  • @for($i = 0; $i < 10; $i++) - Starts a for block
  • @endfor
  • @while(condition) - Starts a while block
  • @endwhile

Inheritance and sections

  • @include(file) - Includes another template
  • @extends('layout') - Extends a template with a layout
  • @section('name') - Starts a section
  • @endsection - Ends section
  • @yield('section') - Yields content of a section.
  • @show - Ends section and yields its content
  • @stop - Ends section
  • @append - Ends section and appends it to existing of section of same name
  • @overwrite - Ends section, overwriting previous section of same name

License

The Dirk is open-source software, licensed under the MIT license

dirk's People

Contributors

artoodetoo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

miovisman

dirk's Issues

Предлагаю добавить команду для inline вывода

Привет!

Предлагаю добавить две команды @inline и @endinline для удаления в шаблоне между ними (+ перевод строки до @inline) всех пробельных символов из начала строк + переводы строк.

Для чего это нужно: вывод inline элементов на страницу без пробелов. Можно конечно и костыли использовать как в самом шаблоне <!-- -->, так и в css, но это муторно.

Пример:
Скомпилированный шаблон до:

<?php if($cur['moderators']): ?>
              <p class="f-modlist">
                <span><?= __('Moderated by') ?></span>
<?php foreach($cur['moderators'] as $mod): ?>
<?php if(is_string($mod)): ?>
                <?= htmlspecialchars($mod, ENT_HTML5 | ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?>
<?php else: ?>
                <a href="<?= $mod[0] ?>"><?= htmlspecialchars($mod[1], ENT_HTML5 | ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?></a>
<?php endif; ?>
<?php endforeach; ?>
              </p>
<?php endif; ?>

В оригинале после <p class="f-modlist"> и </p> поставили команды @inline и @endinline. Получили:

<?php if($cur['moderators']): ?>
              <p class="f-modlist"><span><?= __('Moderated by') ?></span><?php foreach($cur['moderators'] as $mod): ?><?php if(is_string($mod)): ?><?= htmlspecialchars($mod, ENT_HTML5 | ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?><?php else: ?><a href="<?= $mod[0] ?>"><?= htmlspecialchars($mod[1], ENT_HTML5 | ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') ?></a><?php endif; ?><?php endforeach; ?></p>
<?php endif; ?>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.