TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Embeding PHP inside HTML documents (http://www.talkphp.com/general/2002-embeding-php-inside-html-documents.html)

Orc 01-18-2008 05:08 PM

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

xenon 01-18-2008 05:18 PM

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.

Orc 01-18-2008 05:20 PM

Quote:

Originally Posted by xenon (Post 8869)
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

xenon 01-18-2008 05:25 PM

You may also want to see extract if you're going to create a library (aka class).

RobertK 01-18-2008 07:12 PM

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.

sketchMedia 01-18-2008 07:29 PM

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

Orc 01-18-2008 07:33 PM

Quote:

Originally Posted by sketchMedia (Post 8886)
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!

sketchMedia 01-18-2008 07:59 PM

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

Orc 01-18-2008 08:10 PM

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


All times are GMT. The time now is 11:22 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0