It is currently Fri Mar 29, 2024 5:48 am


All times are UTC


Forum rules


Please click here to view the forum rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Tags in PHP?
PostPosted: Sun Aug 05, 2007 1:08 am 
Getting Started
Getting Started

Joined: Sun Apr 09, 2006 4:04 pm
Posts: 63
Location: Canada
Ok I'm trying a new thing with PHP.

I am trying to use tags in predefined html files and templates. Like '{_TITLE_}' is dynamically changed to the title of the page inside <title></title> html tags.

for example:

index.php
Code:
<?php
function DynamicEcho($file){
     include('./replace.php');
     include('./basics.php');
     echo $file;
     }

$file = include('./templates/default/home.html');
DynamicEcho($file);
?>


templates/default/home.html
Code:
<body>
<head>
<title>{_TITLE_}</title>
</head>
<body>
Bla bla
</body>
</html>


replace.php
Code:
<?php
str_replace('{_TITLE_}', $title, $file);
?>


basics.php
Code:
<?php
$title = 'Default';
?>


What it does is the file index.php loads the home.html, sends home.html to replace, which searches and changes what it finds to php code, sends home.html back to index.php, reads through the file ($file) and looks for variables, like $title.

But, with the code I showed, it will just put the file to the browser with no modifications.


I just tried using the html start and and with:
Code:
<?php
$file=<<<FILE   //start

//-----------------------HTML--------------------\\

FILE;
?>                    //end


But now the HTML code is not there when it sends it to the browser...

_________________
Image
Shadow Generator


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 05, 2007 9:53 pm 
Moderator
Moderator

Joined: Tue Feb 27, 2007 12:18 am
Posts: 521
wouldnt it be easier to have a header.php and footer.php and a page.php in the page.php you would do this

page.php
Code:
<?php
$title = "Blah";
require_once('header.php');
echo "Content goes here";
require_once('footer.php');
?>


header.php
Code:
<html>
<head>
<title> <? $title ?> </title>
</head>
<body>


footer.php
Code:
</body>
</html>


This seems like a much simpler idea. IF you want the page in HTML and for it to search for say {_TITLE_} then have a look at preg_match

http://uk2.php.net/manual/en/function.preg-match.php


Or am i thinking along the wrong lines for what you are trying to do?

_________________
http://www.mjmclocks.co.uk


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 06, 2007 12:38 am 
Getting Started
Getting Started

Joined: Sun Apr 09, 2006 4:04 pm
Posts: 63
Location: Canada
Can you inform me how to use preg_match? the php page is a bitch, too formal, I need an easy example and how it is used.

Also, I'm trying to make the code work so the template can replicate the way phpBB2 templates work, with no actual PHP needed in the template.

_________________
Image
Shadow Generator


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 07, 2007 11:58 am 
Moderator
Moderator

Joined: Tue Feb 27, 2007 12:18 am
Posts: 521
If you want i can try and make a simple working version of the {_TITLE_} thing for you. It wont be untill tomorow though, because ive got a load of work on in work.

_________________
http://www.mjmclocks.co.uk


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 10, 2007 7:24 am 
Moderator
Moderator

Joined: Tue Feb 27, 2007 12:18 am
Posts: 521
hey there ~Enigma~

Sory i havnt done the code yet, ive got a reasonably quiet day to day so will be able to do it.

_________________
http://www.mjmclocks.co.uk


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 10, 2007 9:10 am 
Moderator
Moderator

Joined: Tue Feb 27, 2007 12:18 am
Posts: 521
Ok ive done it :) took alot of hard work (about 2hoursish) and here it is (it is a function btw)


create a php file called

function.php
Code:
<?php
function gettitle($fileget, $start, $end){
   $lines = file($fileget);
   foreach ($lines as $line_num => $line) {
       $val = htmlspecialchars($line);
      $subject .= $val;
   }
   
   //Searches for first item set ie. gettitle(file, '{_', '_}'); would look for {_;
   $pattern = '%' . $start . '%';
   preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
   //Searches for last item set ie. gettitle(file, '{_', '_}'); would look for _};
   $pattern = '%' . $end. '%';
   preg_match($pattern, substr($subject,3), $matches2, PREG_OFFSET_CAPTURE);

   //The numbers are offset due to the 3 in substr (dont delete that 3!) this bit sorts it out.
   $val1 = $matches[0][1];
   $val1 = $val1 + 5;
   $val2 = $matches2[0][1];
   $val2 = $val2 + 3;
   $val3 = $val2 - $val1;
   //End of number offset fix
   
   $final = substr($subject, $val1 , $val3); //Get the value between the two numbers found for the start and end
   return $final; //Sets the outcome to script
}
?>


right there is the function made, now how to use it.

here is a test page i created:

Code:
<?php
require_once('function.php');
$title = gettitle('MJMClocks.htm', '{_', '_}');
echo "<html>
<head>
<title>". $title ."</title>
</head>
<body>
Test page for the title | $title
</body>
</html>
";
?>


At the top of the page you must

require_once('function.php');

so it loads up the file with the function in.


This bit gets the title using the function created
$title = gettitle('MJMClocks.htm', '{_', '_}');

the values you need to put in are as follows:

$title = gettitle('FILE NAME', 'START OF STRING', 'END OF STRING');

so in

$title = gettitle('MJMClocks.htm', '{_', '_}');

i am looking for {_TESTTITLE_} in MJMClocks.htm

_________________
http://www.mjmclocks.co.uk


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 6:59 pm 
Getting Started
Getting Started

Joined: Sun Apr 09, 2006 4:04 pm
Posts: 63
Location: Canada
Thanks, but I'm thinking more like the phpBB 2 system for template replacement. Because, it would be more then just {_TITLE_}, how about replacement of {_NAME_} to the logged in username? it would take more memory then I can use, I'm only using the basic plan with Freehostia.

But thanks!

_________________
Image
Shadow Generator


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 7:00 pm 
Getting Started
Getting Started

Joined: Sun Apr 09, 2006 4:04 pm
Posts: 63
Location: Canada
How would you manage using an array for the replacements? Do you know?

_________________
Image
Shadow Generator


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ]  Moderators: Moderators, Support Team

All times are UTC


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

Search for:
Jump to:  
cron
Hosting | Domains | Servers | Extras | Order | Support | Contacts | FreeHostia © 2011