dwoo-project / dwoo

[UNMAINTAINED] php template engine
http://dwoo.org
GNU Lesser General Public License v3.0
167 stars 58 forks source link

Pass the params #66

Closed aolko closed 7 years ago

aolko commented 7 years ago
namespace\Template::dwoo('index.tpl',[
    "userdata" => [
        "username" => $_SESSION['username'],
        "uid" => $_SESSION['user_id']
    ],
    "meta" => namespace\Metadata::meta()
]);
class Template{
        public static function dwoo($tpl,$params=[]){
            $dwoo = new \Dwoo\Core();

            function set_paths($theme_root, $theme_dir, $tpl_dir){

                $path = [
                    "tpl_path" => '' . $theme_root . '/' . $theme_dir . '/' . $tpl_dir . '/',
                    "theme_path" => '' . $theme_root . '/' . $theme_dir,
                    "cache_path" => '' . $theme_root . '/' . $theme_dir . '/' . 'cache/'
                ];

                return $path;
            }

            $dwoo_paths = set_paths('themes','colorbars','tpl');

            $params=[
                'theme' => [
                    'path' => $dwoo_paths['tpl_path'],
                    'assets_path' => $dwoo_paths['theme_path'] . '/assets/'
                ]
            ];

//cache
            $dwoo->setCompileDir($dwoo_paths['cache_path']);

            $dwoo->setTemplateDir($dwoo_paths['tpl_path']);

            echo $dwoo->get($tpl, $params);
        }
    }

what exactly am i doing wrong over here? (my params won't pass externally(first snippet)) by the way, any alternative to json_decode?

emulienfou commented 7 years ago

Hi @aolko, in your static function dwoo($tpl, $params = []), the 2nd parameter $params is never used.

You need to merge arrays such as:

$params = array_merge($params, [
    'theme' => [
        'path'        => $dwoo_paths['tpl_path'],
        'assets_path' => $dwoo_paths['theme_path'] . '/assets/'
    ]
]);

You will be able to access your username such as: {$userdata.username}.

Let me know if it's OK!

aolko commented 7 years ago

that's ok, thanks, btw, how do i disble caching for specific tpl? tired of cleaning cache folder every time

emulienfou commented 7 years ago

You cannot disable caching, dwoo will always create caching files. If you update your template file dwoo is supposed to rebuild the cached file.

If it's not working, you can remove cached files by using some code like:

protected function clearDir($path, $emptyOnly = false)
        {
            if (is_dir($path)) {
                foreach (glob($path . '/*') as $f) {
                    $this->clearDir($f);
                }
                if (!$emptyOnly) {
                    rmdir($path);
                }
            } elseif (is_file($path)) {
                unlink($path);
            }
        }
aolko commented 7 years ago

sometimes cache is a bottleneck, when you have a series of rapid tpl edits, smarty had a handy cache disabling (globally and per tpl)