xeoncross / PHP-Template

Simple PHP template inheritance and view management class using pure PHP
28 stars 4 forks source link

assign variable to template #2

Closed kamov closed 7 years ago

kamov commented 10 years ago

Hi first of all thanks for your awesome php template, I really love it.

I would like to ask you, how I can assign variable to template from a controller? I can only via template assign variable to view files, but not from controller.

thanks!!

xeoncross commented 10 years ago

What framework are you using? You can assign a variable to a template from any system as shown in the example.

$template = new Template('page');
$template->title('Example Site');
print $template;
kamov commented 10 years ago

Thanks for your reply, I got it now!! I don't use any framework.

Do you have an idea how to implment cache on this theme?

All the best

xeoncross commented 10 years ago

Caching can be done using a verity of methods. You can easily employ memcache to cache it. However, loading a template using this library is much faster than even loading a cache template from a large library like smarty.

$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211);

if ($html = $memcache->get('mypagetemplate')) {
    die($html); // done
} else {

    $template = new Template('page');
    $template->title('Example Site');

    $memcache->set('mypagetemplate', $template->toString());
    die($template);
}
kamov commented 10 years ago

Thanks, this line help me get what I want.

$memcache->set('mypagetemplate', $template->toString()); :+1:

However, loading a template using this library is much faster than even loading a cache template from a large library like smarty.

Sound good! A browser caching can be enough using by setting Last-Modified header.

I will make my test.

Thanks!!