opendena / jade.php

HAML-like template engine for PHP 5.3
MIT License
18 stars 0 forks source link

Pass context to template #4

Open olanod opened 11 years ago

olanod commented 11 years ago

I didn't find a way to pass a context/scope to the template being rendered. Is it possible?

echo $jade->render($file,array('user' => 'admin'));
SebastienElet commented 11 years ago

Actually, the way to use php vars is the fallowing :

<?php

require '../vendor/autoload.php';

use Jade\Jade;

$jade = new Jade();

$user = 'Admin';
eval(
    '?>'.
    $jade->render(
        'h1 Hi #{$user}'
    )
);

But trust me, i really hate the eval and the lack of scope.

olanod commented 11 years ago

it looks ugly but it does the job. I'm more concerned about the security of using eval(), is the data scaped when is dumped? in my(or anyone) use cases the input shouldn't be trusted.
For example I have a render function which receives an array that will be used as the scope in the template and the array could be anything like user submitted data:

function render($template,$scope=null) {
    if ($scope !== null && is_array($scope)) {
        extract($scope);
    }
    ob_start();
    eval('?>' . $jade->render($template));
    $content = ob_get_contents();
    ob_clean();
    return $content;
}