Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Hava a look at this small class to build smarty-webbpages

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Frameworks
View previous topic :: View next topic  
Author Message
johannes
Smarty Regular


Joined: 10 May 2003
Posts: 85
Location: Malmö, Sweden

PostPosted: Thu Jun 10, 2004 6:19 pm    Post subject: Hava a look at this small class to build smarty-webbpages Reply with quote

Hi all, I´m kind of new in Smarty, but I have put this together. You can see this in work here:
http://euroaccident.supportcenter.se/page.php?docid=18 or here
http://www.ifkvaxjo.se

I have made a simpel class and a php-page that get the webbpages and put them together by Smarty. I would be wery happy if anybody, could just take a look on this and, point out the worst misstakes!!

This is supposed to be a small but nice way to build a webbproject, planning on writing some plugins, and som working admin pages this summer. I use ADOdb, as db-layer

Well, this is the code:

1. run page.php
2. creats cacheid, and setup cache
3. check if the page is already in the cache.
4. if not, create the page
5. display page, from cache of from db

page.php
Code:
<?

/**
 * Junior page
 * package Junior cms
 * ver 0.0.2
 * date: 2004-06-05
 **/

error_reporting(E_ALL ^ E_NOTICE);
session_start();
require_once 'setup.php';
$smarty->debugging_ctrl = 'URL';
require_once 'class/junior.class.php';
$junior = new junior_start($docid,$db);

//CREATE CACHEID
$my_cacheId = $_GET['docid'];

//SET CACHE
$junior->setCache($my_cacheId,$smarty);

// CHECK IF PAGE EXISTS -------------------------------------------
// IF NOT SEND TO 404-PAGE MISSING PAGE ---------------------------
if($junior->getTpl() != "" or $junior->getTpl() != 0){

   // PAGE EXISTS - NOW LOOK FOR CACHE ---------------------------------
   if(!$smarty->is_cached($junior->getTpl(), $junior->getCacheID())) {

      //get content to the page
      $doc = $junior->getDoc($_GET['docid'],$db);

      //assign timestamp
      $smarty->assign("timestamp", $junior->getTimestamp());

      //assign to tpl
      $smarty->assign("data", $doc);

   }

   // DISPLAY PAGE - CACHED OR NEW
   $smarty->display($junior->getTpl(), $junior->getCacheID());

} else {
   
   // NOPAGE EXISTS SEND TO 404-PAGE
   Header("Location: /nopage.html");

}

$db->Close();

?>



and the class:

Code:
<?PHP

/**
 * Junior junior.class.php
 * Junior cms
 * ver 0.0.2
 * 2004-05-09
 */


class junior_start
{

   //var
   var $tpl;
   var $login;
   var $doctxt_source;
   var $cache_my_cache_id;

   var $user_id;
   var $user_name;

   var $numeric_value;
   var $error;
   var $flag;

   function junior_start($docid,$db)
   {
      if($this->setNumericValue($docid) == true) //validat as numeric id for page
      {
         //ladda tpl & login-flagga
         $sql = "select tpl,login from junior_doc where docid=".$this->getNumericValue()." limit 1";
         $result = $db->GetRow($sql);
         $this->tpl = $result[0];
         $this->login = $result[1];
      }
   }

   //SET CACHE
   function setCache($my_cacheId,&$smarty)
   {
      if($_SESSION["junior_session_login"] != "TRUE"){
         $smarty->caching = true;
         $smarty->cache_lifetime = 3600000000;
         $this->cache_my_cache_id = $my_cacheId;
      }
   }

   //GET A TEXT TO PUT ON THE PAGE, THE CONTENT SAVED IN DB.
   function getDoc($docid,$db)
   {
      $sql = "select doctxt_source,doctxt_timestamp from junior_doctxt where doctxt_id=$docid limit 1";
      $result = $db->GetRow($sql);
      $this->doctxt_source = $result[0];
      $this->doctxt_timestamp = substr($result[1], 0, 10);
      return $this->doctxt_source;
   }

   // set & get & val
   function setNumericValue($numeric_value)
   {
      if(is_numeric($numeric_value)) {
         $this->numeric_value = $numeric_value;
         return true;
      } else {
         $this->error = "Måste vara numeriskt";
         return false;
      }
   }

    function getNumericValue()
    {
        return $this->numeric_value;
    }

   function getCacheID()
    {
        return $this->cache_my_cache_id;
    }

   function getSource()
   {
      return $this->doctxt_source;
   }

   function getTimestamp()
   {
      return $this->doctxt_timestamp;
   }

    function getTpl()
    {
        return $this->tpl;
    }

    function getLogin()
    {
        return $this->login;
    }

    function getUserId()
    {
        return $this->user_id;
    }

    function getUserName()
    {
        return $this->user_name;
    }

}
?>


and a simpel tpl:
Code:
<table width="100%" cellspacing="0" cellpadding="8" border="0">
<tr>
   <td>{$data|junior_eval}</td>
</tr>
</table>


and at last a small eval plugin
Code:
/* {$data|junior_eval}*/
function smarty_modifier_junior_eval($doc) {
   global $smarty,$junior;
   $smarty->assign('doc_to_eval', $doc);
   $evald = $smarty->fetch('eval.tpl', $junior->getCacheID());
    return $evald;
}

_________________
--------------------------
nice culture on the Internet
www.poeter.se
Back to top
View user's profile Send private message Send e-mail Visit poster's website
wwccss
Smarty Rookie


Joined: 16 Jun 2004
Posts: 5
Location: Beijing China

PostPosted: Wed Jun 16, 2004 4:38 am    Post subject: Reply with quote

Quote:
$smarty->cache_lifetime = 3600000000;

Do you mean that this document will never expired? if you want to do this,you can set the cache_lifetime value to -1.
$smarty->cache_lifetime = -1
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
wwccss
Smarty Rookie


Joined: 16 Jun 2004
Posts: 5
Location: Beijing China

PostPosted: Wed Jun 16, 2004 4:40 am    Post subject: Reply with quote

Quote:
if($junior->getTpl() != "" or $junior->getTpl() != 0)

I think you use the function empty() to judge this.
if the value of a variable is zero,empty() will return false also.
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
johannes
Smarty Regular


Joined: 10 May 2003
Posts: 85
Location: Malmö, Sweden

PostPosted: Wed Jun 16, 2004 7:06 am    Post subject: thanks alot Reply with quote

Thanks alot for the comments, wery useful!

Yes, the idea is to not clear the tpl until it the page is updater by the admin page, so I will change the lifetime to -1

I see that emty() can do the job in a more clean way. I will correct that too.

/Johannes
_________________
--------------------------
nice culture on the Internet
www.poeter.se
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Frameworks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP