TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-18-2008, 05:08 PM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Confused Embeding PHP inside HTML documents

How would I go about embeding PHP inside HTML Documents? Such as variables?

I do not want to use an .HTACCESS

Considering this I need it for my new site which has almost everything written in PHP using variables and functions.
Such as I have a variable named:
PHP Code:
$site_title 
which gives the title, how would I embed this var inside an HTML document? Is it considered something as the Smarty Engine?


@James
Orc is offline  
Reply With Quote
Old 01-18-2008, 05:18 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Capture the output of the file into the buffer, then just echo it. Like so:

PHP Code:
ob_start();
require 
$template_file;
$template_contents_parsed ob_get_contents();
ob_end_clean();

echo/return 
$template_contents_parsed
This is the path I've chosen (over Smarty-like "template engines" and php function based templates). It's very fast, very useful, and extremely flexible. You can go with what ever suits you, just think of your needs first.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following 2 Users Say Thank You to xenon For This Useful Post:
Orc (01-18-2008), RobertK (01-18-2008)
Old 01-18-2008, 05:20 PM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
Capture the output of the file into the buffer, then just echo it. Like so:

PHP Code:
ob_start();
require 
$template_file;
$template_contents_parsed ob_get_contents();
ob_end_clean();

echo/return 
$template_contents_parsed
Brilliant! Thankz you
Orc is offline  
Reply With Quote
Old 01-18-2008, 05:25 PM   #4 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

You may also want to see extract if you're going to create a library (aka class).
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
Orc (01-18-2008)
Old 01-18-2008, 07:12 PM   #5 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

I like your method, Xenon, I hadn't gotten to that point just yet. Though I have used that method for caching sections. Thanks for posting it.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-18-2008, 07:29 PM   #6 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

that code snippet can be made shorter i believe:
PHP Code:
ob_start(); 
require 
$template_file
echo 
ob_get_clean(); //(or return) 
ob_get_clean() executes both ob_get_contents() and ob_end_clean() automatically
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Orc (01-18-2008)
Old 01-18-2008, 07:33 PM   #7 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
that code snippet can be made shorter i believe:
PHP Code:
ob_start(); 
require 
$template_file
echo 
ob_get_clean(); //(or return) 
ob_get_clean() executes both ob_get_contents() and ob_end_clean() automatically
Thanks! I'll update my code!

UPDATE:
PHP Code:
   // Grab the html files
 
public    function html_grab($html.".html"
    {
         if (    
file_exists($html.".html")       )
      {
             
ob_start();
             include 
$site_path."/html/".$html.".html";
           
$tpl_content_parse =ob_get_clean();

             return 
$tpl_content_parse;
          }
            else die(
"The HTML Document currently does not exist");
        }


// Thus I just use the function e.g. html_grab("header") 
You're more than welcome to update any code that should necessary/helps!
Orc is offline  
Reply With Quote
Old 01-18-2008, 07:59 PM   #8 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

PHP Code:
public function html_grab($html)
{
    
file_exists($html '.html') or die('The HTML Document currently does not exsist');
    
ob_start();
    include 
$site_path "/html/" $html ".html";
    return 
ob_get_clean();

I've tidied it up abit:
PHP Code:
$html.".html" 
cannot be in the function parameters, plus if it did work it would be pointless.
PHP Code:
           $tpl_content_parse =ob_get_clean(); 

             return 
$tpl_content_parse
removed this as there is no need for the temp variable, just return the HTML from ob_get_clean();

to use:
PHP Code:
echo html_grab('testfileorwatever'); 
unless you change 'return' for echo but u proably already know that, bah i need coffee
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)

Last edited by sketchMedia : 01-18-2008 at 08:07 PM. Reason: *sigh* oh how i wish i could spell
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
Orc (01-18-2008)
Old 01-18-2008, 08:10 PM   #9 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

I'm a bit sick today so I cannot really complete much of my code, much less my sentences >.< need some tea
Orc is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:41 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design