FreeHostia Forums
http://forum.freehostia.com/

Tags in PHP?
http://forum.freehostia.com/viewtopic.php?f=7&t=7436
Page 1 of 1

Author:  ~Enigma~ [ Sun Aug 05, 2007 1:08 am ]
Post subject:  Tags in PHP?

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...

Author:  Josh.Waller [ Sun Aug 05, 2007 9:53 pm ]
Post subject: 

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?

Author:  ~Enigma~ [ Mon Aug 06, 2007 12:38 am ]
Post subject: 

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.

Author:  Josh.Waller [ Tue Aug 07, 2007 11:58 am ]
Post subject: 

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.

Author:  Josh.Waller [ Fri Aug 10, 2007 7:24 am ]
Post subject: 

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.

Author:  Josh.Waller [ Fri Aug 10, 2007 9:10 am ]
Post subject: 

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

Author:  ~Enigma~ [ Tue Aug 21, 2007 6:59 pm ]
Post subject: 

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!

Author:  ~Enigma~ [ Tue Aug 21, 2007 7:00 pm ]
Post subject: 

How would you manage using an array for the replacements? Do you know?

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/