DooPHP IRC channel


Using Smarty Template Engine with DooPHP

Share your tips, snippets and experiences about DooPHP, and discuss about best DooPHP practices.

Using Smarty Template Engine with DooPHP

Postby drewish » Sun Aug 02, 2009 7:53 pm

If anyone is interested I modified Doophp to use Smarty Template Engine.
The code goes into DooController.php for the view function.

Code: Select all
       public function view($smarty=true){
           if($smarty){
            //initiate Smarty
            if($this->_view==NULL){
              include(Doo::conf()->SITE_PATH. 'protected/class/smarty/Smarty.class.php');
              $template = new Smarty;
          $template->compile_check = false;
          $template->force_compile = true;
    //you can use this if you want to use smarty debugging
          if(isset($_GET['debug'])){
             $template->debugging = true;
          }else{
             $template->debugging = false;
          }
    //you can use this to clear template cache
          if(isset($_GET['reset'])){
             $template->clear_all_cache();
          }
          $template->template_dir = Doo::conf()->SITE_PATH . "protected/view";
          $template->compile_dir  = Doo::conf()->SITE_PATH . "protected/viewc";
          $this->_view = $template;
              }
            }else{
            if($this->_view==NULL){
                Doo::loadCore('view/DooView');
                $this->_view = new DooView;
            }
          }
            return $this->_view;
        }



Then from your controllers you can call
smarty like this:
Code: Select all
 $this->view()->assign('page', 'categories');
$this->view()->display('categories.html');
drewish
 
Posts: 39
Joined: Tue Jul 28, 2009 6:39 am

Re: Using Smarty Template Engine with DooPHP

Postby leng » Mon Aug 03, 2009 3:39 pm

Drewish, you can use Doo::loadClass() or $this->load()->class() in the Controller

Code: Select all
include(Doo::conf()->SITE_PATH. 'protected/class/smarty/Smarty.class.php');

can be shorten to
Code: Select all
Doo::loadClass('smarty/Smarty.class');
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: Using Smarty Template Engine with DooPHP

Postby leng » Mon Aug 03, 2009 3:41 pm

If you don't wish to modify the framework source you can write a class that extends DooController and override the view method or just create a new method call smartyView

Code: Select all
class SmartyController extends DooController{
     public function view($smarty=true){
            // .... do smarty stuff here
     }
}

// in your controller
Doo::loadController('SmartyController');
class MyWebsiteController extends SmartyController {
      function getNews(){
             $this->view()->smarty_functions ....
      }
}
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: Using Smarty Template Engine with DooPHP

Postby pmathia » Sun Sep 20, 2009 5:57 pm

I need help.
I tried integrating smarty template engine with doophp and my template test page is not displaying in the browser.
Here is what I have done

1. In template_engine demo folder I added smarty library in the protected/class/smarty/ folder.
2. I created SmartyController.php in the protected/contoller/ folder:

Code: Select all
<?php
class SmartyController extends DooController{
       public function smartyView($smarty=true){
           if($smarty){
            //initiate Smarty
            if($this->_view==NULL){
              //include(Doo::conf()->SITE_PATH. 'protected/class/smarty/Smarty.class.php');
      Doo::loadClass('smarty/Smarty.class');
              $template = new Smarty;
          $template->compile_check = false;
          $template->force_compile = true;
    //you can use this if you want to use smarty debugging
          if(isset($_GET['debug'])){
             $template->debugging = true;
          }else{
             $template->debugging = false;
          }
    //you can use this to clear template cache
          if(isset($_GET['reset'])){
             $template->clear_all_cache();
          }
          $template->template_dir = Doo::conf()->SITE_PATH . "protected/view";
          $template->compile_dir  = Doo::conf()->SITE_PATH . "protected/viewc";
          $template->cache_dir = Doo::conf()->SITE_PATH . "protected/cache";
          $template->config_dir = Doo::conf()->SITE_PATH . "protected/config";
          $this->_view = $template;
              }
            }else{
            if($this->_view==NULL){
                Doo::loadCore('view/DooView');
                $this->_view = new DooView;
            }
          }
            return $this->_view;
        }
     }
}

?>


3. I created another MysmartyController.php in the protected/controller/ folder

Code: Select all
<?php
/**
 * Description of MySmartyController
 *
 * @author Peter
 */
Doo::loadController('SmartyController');
class MysmartyController extends SmartyController {
    function test(){
          $data['title'] = 'Smarty Test Template';
         $data['name'] = 'Peter';
         $this->smartyView()->assign('title', $data['title']);
         $this->smartyView()->assign('name', $data['name']);
         $this->smartyView()->display('mysmarty_template.tpl');
   }
}

?>


4. I created the file mysmarty_template.tpl file in protected/view/ folder

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{$title}</title>
</head>

<body>
<p>hello {$name}.</p>
</body>
</html>


5. I entered the following entry in the protected/config/routes.conf.php file:

Code: Select all
<?php
/**
 * Define your URI routes here.
 *
 * $route[Request Method][Uri ] = array( Controller class, action method, other options, etc. )
 *
 * RESTful api support, *=any request method, GET PUT POST DELETE
 * POST    Create
 * GET      Read
 * PUT      Update, Create
 * DELETE    Delete
 */
$route['*']['/'] = array('MainController', 'index');
$route['*']['/url'] = array('MainController', 'url');
$route['*']['/example'] = array('MainController', 'example');
$route['*']['/error'] = array('ErrorController', 'index');
$route['*']['/html'] = array('HtmlController', 'test');
$route['*']['/php'] = array('PhpController', 'test');
$route['*']['/smarty'] = array('MysmartyController', 'test');
$route['*']['/about'] = $route['*']['/'];

//----------- Template routes ---------
$route['*']['/template.html'] = array('MainController', 'template_source');

?>


6. when i browse http://localhost/doophp/demos/template_engine/index.php/smarty I get webpage cannot be displayed message.

ANY ERRORS in my code??
pmathia
 
Posts: 3
Joined: Sun Sep 20, 2009 5:36 pm

Re: Using Smarty Template Engine with DooPHP

Postby leng » Sun Sep 20, 2009 6:22 pm

Can you copy-paste the exact error message here?
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: Using Smarty Template Engine with DooPHP

Postby pmathia » Mon Sep 21, 2009 8:44 am

Sure Leng,
Here is the error displayed in the browser. Am I missing any php extension perhaps? I am already running Smarty on other folders in my www root.


This error (HTTP 500 Internal Server Error) means that the website you are visiting had a server problem which prevented the webpage from displaying.

For more information about HTTP errors, see Help.


I hope I fix that with your help so I proceed learning and implementing the great doophp cms.
pmathia
 
Posts: 3
Joined: Sun Sep 20, 2009 5:36 pm

Re: Using Smarty Template Engine with DooPHP

Postby leng » Mon Sep 21, 2009 9:21 am

can you have display_error on for PHP? because this message tells me that your web server is hiding the PHP errors.
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Re: Using Smarty Template Engine with DooPHP

Postby pmathia » Mon Sep 21, 2009 5:32 pm

It was a quotation I forgot to close. :oops: Thank you leng for the tip of turning display_errors on.
pmathia
 
Posts: 3
Joined: Sun Sep 20, 2009 5:36 pm


Return to Tips & Tricks

Who is online

Users browsing this forum: No registered users and 0 guests