A
A
AVXNWNK2016-11-21 18:48:32
PHP
AVXNWNK, 2016-11-21 18:48:32

How to connect Fenom to my project?

Greetings to all who have looked into my ask. There was such a nuisance with my PHP project, not so long ago I started to master Composer and decided to start connecting the Fenom template engine, to replace my regular one, which worked on the principle of buffer output through ob. In general, I connected it to the project, displayed my Layout, but I just can’t display Content, I’m stuck tightly and don’t know what to do. Complains about not being able to convert an array to a string. Maybe, of course, I "nailed it" here, or even worse, but still it is necessary to solve the problem somehow. Here are the code snippets:
index.php:

define('VIEWS_PATH', ROOT . DS . 'views');

App::run($_SERVER['REQUEST_URI']);

App.class.php:
self::$router = new Router($url);

$controller_class = ucfirst(self::$router->getController()) . 'Controller';
$controller_method = strtolower(self::$router->getMethodPrefix() . self::$router->getAction());

$layout = self::$router->getRoute();

$fenom = Fenom::factory(VIEWS_PATH, VIEWS_PATH.'/cache', array(
    'disable_cache' => 'true',
));
$controller_object = new $controller_class;

if (method_exists($controller_object, $controller_method)) {
    $content = $fenom->display($controller_object->$controller_method(), $controller_object->getData());
} else {
    throw new Exception('meth_not_found');
}

$fenom->display($layout.'.html', compact('content'));

Well, in Layout I display the variable in this form:
{$content}
Well, to make it more clear, in the old "template engine" everything looked something like this:
App.class.php:
if (method_exists($controller_object, $controller_method)) {
    $view_path = $controller_object->$controller_method();
    $view_object = new View($controller_object->getData(), $view_path);
    $content = $view_object->render();
} else {
    throw new Exception('meth_not');
}

$layout_path = VIEWS_PATH . DS . $layout . '.html';
$layout_view_object = new View(compact('content'), $layout_path);

echo $layout_view_object->render();

View.class.php:
public function render()
{
    $data = $this->data;
    ob_start();
    include($this->path);
    $content = ob_get_clean();

    return $content;
}

And in Layout, the content was connected like this: Thank you in advance!
<?=$data['content']?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AVXNWNK, 2016-11-22
@AVXNWNK

I solved the problem as follows: I
declared $fenom in App.class.php, then I created a getter for it, which allowed me to connect Fenom to View.class.php, then in App.class.php I displayed Layout:
In the meantime, View.class.php knows where we are and writes the $path string and also builds the $data array. Then Fenom connects to the View from App and the path to the template and data are written to $content:
Then the $content is returned to the App and it already draws it in the Layout in the {$content} variable.

A
Alexander Kuznetsov, 2016-11-21
@DarkRaven

What do you have in the $content variable itself? Have you looked through var_dump?
Also, this line is confusing:
There, based on the documentation, you need this:
Documentation link:
https://github.com/fenom-template/fenom/blob/maste...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question