Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

smarty (how to?)

Status
Not open for further replies.

JamesGMills

Programmer
Aug 15, 2005
157
GB
Hi i have been told about smarty for a problem i am having:

Original problem----------------------------------
A long time ago i used to make all my pages in html and make a new page full of new code for each page needed.

Then i found dreamweaver templates and fell in love...

Then i found the use of php includes... WOW how much easier this made life.

However one problem i have found recenly is this:

If you have an include for a header and a footer and then for every page you put your content inbetween these included files it works a treat. However then the header file is going to be the same for every page thus making your title the same for every page!

There is one way i was thinking of... having a header_part1.php and a header_part2.php and then you could add te title inbetween these and you could change the title for each page individually...

How do you lot do this or get around the issue of having a different title for all your main pages.

If i was using my CMS i would just specify the title for the page/section in te database and it would load this dynamically but not always that practical....
-----------------------------------

I have been told i can use it like this:

Code:
$header = "includes/header.php";
$title = "James Mills Personal Website Contact Page";

{include file=$header title='$title'}

However where and how do i include the smarty class because i have downloaded the "Smarty.class.php" file and included it but i can not get the above code to work?

Thanks for help in advance.
 
at a simple level (ignoring smart for a moment) this will work fine

header.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php echo $title; ?></title>
</head>

main.php
Code:
<?
//some decision tree
if ($somevalue === 1):
  $title = "Title page for first value";
else:
  $title = "Title page for other values";
endif;

include "header.php";

i.e. with include and require (and their "_once" alternatives) the code in the "included" file is parsed and evaluated on inclusion. this is in contrast to fopen and other file system functions which do not evaluate the code. Both, of course, have their uses.

So ... for include/require if the file you are including has php tags or functions in it, the code in the tags will be evaluated and the functions will be available to your script.

Above, we assign a value to the variable $title and AFTER that we include the header.php which takes the value of the $title variable as the page title. of course if you included the file before we set the value of $title then variable would not exist and the resultant page title would be blank.

note that the normal rules on variable scope apply to included files.
 
Another method that is very similar to jpadies and one I use in PHP, ASP, etc is to wrap your header and footer in functions and drop them in the same include file.
Something like:
Code:
<?
function ShowHeader($title,$desc,$keywords){
   ?>
   <html>
   <head>
      <title><? echo $title; ?></title>
      <meta name="description" content="<? echo $desc; ?>" />
      <meta name="keywords" content="<? echo $keywords; ?>" />
   </head>
   <body>
   <?
}

function ShowFooter(){
   echo '</body></html>';
}

Then it is simply a matter of including the file and calling the two functions wherever I need the actual content. Of course they would generally bemore complex and possibly have a few more values based on the site design and such:
Code:
<?
include_once('myIncludeFile.php');

//do some stuff
//do some stuff

ShowHeader('Some Title','Some Description','Some keywords');

//output some more HTML
//output some more HTML
//output some more HTML

ShowFooter();
?>

As I said, it's not much differant then the method jpadie proposed, but it can be extremely handy to have your header and footer saved in one file all by themselves and it means that you are passing it the arguments you want inserted in the header rather than declaring variables with the right name before your include. I stopped using that method a while ago after I chased down a bug in my own code due to moving a few variable declarations. With the function it is more obvious exactly what you are passing, even if you come back to the code 6 months and three projects later.

-T

signature.png
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top