shineunited / silex-timeline
2.0.1
2017-09-28 18:53 UTC
Requires
- php: >=5.5.9
- silex/silex: ^2.0
Requires (Dev)
- phpunit/phpunit: ~4.0|~5.0
- twig/twig: ^1.33
Suggests
- twig/twig: Allow use of timeline inside of twig templates.
README
Silex 包,用于在模板和路由中管理基于时间的动态内容。
安装
推荐通过 composer 安装
$ composer require shineunited/silex-timeline
配置
<?php require_once(__DIR__ . '/../vendor/autoload.php'); use Silex\Application; use ShineUnited\Silex\Timeline\TimelineServiceProvider; $app = new Application(); $app->register(new TimelineServiceProvider(), [ 'timeline.timezone' => 'UTC', 'timeline.epochs' => [ 'epoch1' => 'Monday, 15-Aug-2005 15:52:01', 'epoch2' => '2005-08-15T15:52:01', 'epoch3' => new \DateTimeImmutable('15-Aug-05 15:52:01') ] ]);
使用
添加纪元
<?php // add epochs directly $app['timeline']['epoch4'] = 'Monday, 15-Aug-2005 15:52:01'; $app['timeline']['epoch5'] = '15 Aug 2005 15:52:01'; $app['timeline']['epoch6'] = new \DateTimeImmutable('15-Aug-05 15:52:01');
获取纪元
通过数组引用
<?php $app['timeline']['now']; $app['timeline']['epoch1']; $app['timeline']['epoch2']; $app['timeline']['epoch3'];
通过参数引用
<?php $app['timeline']->now; $app['timeline']->epoch1; $app['timeline']->epoch2; $app['timeline']->epoch3;
比较纪元
时间线函数
<?php // isBefore if($app['timeline']->isBefore('epoch1')) { // before 'epoch1' } // isAfter if($app['timeline']->isAfter('epoch1')) { // before 'epoch1' } // isUpcoming if($app['timeline']->isUpcoming('epoch1')) { // before 'epoch1' } // isComplete if($app['timeline']->isComplete('epoch1')) { // before 'epoch1' }
纪元函数
<?php // isBefore if($epoch->isBefore('epoch1')) { // $epoch is before 'epoch1' } // isAfter if($epoch->isAfter('epoch1')) { // $epoch is after 'epoch1' }
直接比较
<?php // by array reference if($app['timeline']['now'] < $app['timeline']['epoch1']) { // before 'epoch1' } if($app['timeline']['now'] >= $app['timeline']['epoch1']) { // after 'epoch1' } // by param reference if($app['timeline']->now < $app['timeline']->epoch1) { // before 'epoch1' } if($app['timeline']->now >= $app['timeline']->epoch1) { // before 'epoch1' }
Twig 模板
Twig 操作符
{# before operator #} {% if before "epoch" %} BEFORE {% else %} AFTER {% endif %}
{# after operator #} {% if after "epoch" %} AFTER {% else %} BEFORE {% endif %}
Twig 函数
{# isBefore function #} {% if isBefore("epoch") %} BEFORE {% else %} AFTER {% endif %}
{# isAfter function #} {% if isAfter("epoch") %} AFTER {% else %} BEFORE {% endif %}
{# isUpcoming function #} {% if isUpcoming("epoch") %} UPCOMING {% else %} COMPLETE {% endif %}
{# isComplete function #} {% if isComplete("epoch") %} COMPLETE {% else %} UPCOMING {% endif %}
Twig 测试
{# upcoming test #} {% if "epoch" is upcoming %} UPCOMING {% else %} COMPLETE {% endif %}
{# complete test #} {% if "epoch" is complete %} COMPLETE {% else %} UPCOMING {% endif %}