I have a problem trying to show a date with an external library https://github.com/fightbulc/moment.php in volt.
I'm trying to use functions in volt to do it.
I reviewed some old posts that served as a guide but the detail is that I did not know how to implement them for my case.
This is what I tried:
services.php
$di->setShared('view', function () {
$config = $this->getConfig();
$view = new View();
$view->setDI($this);
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines([
'.volt' => function ($view) {
$config = $this->getConfig();
$volt = new VoltEngine($view, $this);
$volt->setOptions([
'compiledPath' => $config->application->cacheDir,
'compiledSeparator' => '_',
'compiledExtension' => '.compiled',
]);
$compiler = $volt->getCompiler();
// example test function that works
$compiler->addFunction('strlen', function($resolvedArgs, $exprArgs) use ($compiler) {
$string= $compiler->expression($exprArgs[0]['expr']);
$secondArgument = $compiler->expression($exprArgs[1]['expr']);
return 'substr(' . $string . ', 0 ,' . $secondArgument . ')';
});
// my non-working function
$compiler->addFunction(
'dateForHumans',
function ($resolvedArgs, $exprArgs) use ($compiler) {
//$string= $compiler->expression($exprArgs[0]['expr']);
$string = $resolvedArgs;
$m = new \Moment\Moment($string, 'Europe/Berlin'); // default is "now" UTC
$m->format(); // e.g. 2012-10-03T10:00:00+0000
//return 'new \Moment\Moment('.$string.', "Europe/Berlin")->format()'; // e.g. 2012-10-03T10:00:00+0000
return $m;
}
);
return $volt;
},
//'.phtml' => PhpEngine::class
]);
return $view;
});
controller.php
namespace App\Controllers;
use Phalcon\Validation;
// use Phalcon\Paginator\Adapter\Model as PaginatorModel;
use Phalcon\Paginator\Adapter\NativeArray as PaginatorArray;
use App\Models\Ciclos as Ciclo;
class CiclosController extends ControllerBase
{
public function indexAction()
{
// Current page to show
// In a controller/component this can be:
$currentPage = $this->request->getQuery('page', 'int'); // GET
// $this->request->getPost('page', 'int'); // POST
//$currentPage = (int) $_GET['page'];
// The data set to paginate
$ciclos = Ciclo::find([
'order' => 'ciclo_id desc',
]);
$rows = array();
foreach ($ciclos as $ciclo) {
$rows[] = [
'ciclo_id' => $ciclo->ciclo_id,
'periodo' => $ciclo->periodo,
'creacion' => $ciclo->creacion
];
}
//die(var_dump($rows));
// Create a Model paginator, show 10 rows by page starting from $currentPage
$paginator = new PaginatorArray(
[
'data' => $rows,
'limit' => 10,
'page' => $currentPage,
]
);
// Get the paginated results
$this->view->ciclos = $paginator->getPaginate();
//$this->view->ciclos = 1;
return $this->view->render('ciclos', 'index');
}
view.volt
<table class="table table-striped table-condensed table-hover table-sm">
<caption>
<b>Total: </b>{{ ciclos.total_items }} registros
<a href="{{ url('ciclos/crear') }}" class="btn btn-primary btn-xs">
<i class="fa fa-plus-circle"></i> Crear
</a>
</caption>
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">DescripciĆ³n</th>
<th scope="col">CreaciĆ³n</th>
<th scope="col" class="text-center">Opciones</th>
</tr>
</thead>
<tbody>
{% for row in rows %}
<tr>
<th scope="row">{{ row['ciclo_id'] }}</th>
<td>Ciclo {{ row['periodo'] }}</td>
<!-- example test line, it works -->
<!-- <td>{# { strlen('abcdefghifklmnop', 10) } #}</td> -->
<!-- this does not work -->
<td>{{ dateForHumans(row['creacion']) }}</td>
<td class="text-center">
<a href="{{ url('ciclos/actualizar/' ~ row['ciclo_id']) }}" class="btn-warning btn-xs">
<i class="fa fa-pencil"></i>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
This is the error:
Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string ($row['creacion']) at position 0 ($): Unexpected character in A:\wamp3\www\econtrol\vendor\fightbulc\moment\src\Moment.php on line 188
.
And if I replace <td>{{ dateForHumans(row['creacion']) }}</td>
, for example, to timestamp <td>{{ dateForHumans(1499366585) }}</td>
it shows the same error.
I could use php directly in the view to try to solve this but I think it makes no sense to use volt if I'm going to end up writing normal php.
Any hero without a cape to enlighten me?
Thanks