homepage Welcome to WebmasterWorld Guest from 23.20.77.156
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
making html a variable
can it be done
whatson




msg:4456809
 4:18 am on May 23, 2012 (gmt 0)

If I want to make a variable like

$variable ="<html>
<body>
<div align="center">
<table border="0" width="980" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">"

Is this possible some way? How is it done as any other " will end the variable?

 

brotherhood of LAN




msg:4456810
 4:46 am on May 23, 2012 (gmt 0)

There's lots of ways to do it.

Escaping the double quotes within the variable for example:
$variable = "<html>
<body>
<div align=\"center\">
<table border=\"0\" width=\"980\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td width=\"100%\">";


Another way is to use single quotes to encapsulate the HTML.

HEREDOC is probably the most convenient in cases like this, where you don't really have to worry about escaping quotes while also being able to include variables.

This manual page [php.net] covers the options.

incrediBILL




msg:4456816
 5:19 am on May 23, 2012 (gmt 0)

Another way is to make pure HTML files with the content you want and just read them in as a string and echo them out as needed or (gasp) simply include() them which avoids double and single quote nonsense.

Very common practice especially for the header and footer parts.

Example:

<?php
session_start();
$title="Sample Title Variable used in header.htm";
$meta_desc="Sample Meta Description used in header.htm";
include( "nav/header.htm");
?>

<h2>Here's the web page</h2>
Just put some stuff in here...

<?php
include( "nav/footer.htm");
?>


The content of header.htm and footer.htm are just HTML files.

The title is displayed in header.htm like this:

<title><?=$title?></title>

Hope that gives you an idea

whatson




msg:4456879
 10:09 am on May 23, 2012 (gmt 0)

Thanks. I dont want to escape every single ", and if I use ', then they will also need to be escaped when ' occurs in the html too. Any other ideas?

incrediBILL




msg:4456957
 2:22 pm on May 23, 2012 (gmt 0)

My include solves the escape problem and the " vs ' issue as well, but does require more files.

rocknbil




msg:4457023
 4:58 pm on May 23, 2012 (gmt 0)

As for escaping: you'll have to escape at some point or another, and single quoted attributes don't validate under some doctypes. What you really need to do is use some form of templating system or including system as incrediBill mentions. In areas where you can't do that, you'll have to escape OR concatenate. Using your example, this may be a viable option:

$variable = '<html>
<body>
<div style="margin:auto;"> <!-- ALIGN IS DEPRECATED -->
<table border="0" width="980" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">' . $my_td_content .
'</td></tr></table></div>';

echo $variable;

Another templating method is to store files with "markers"

<title |THE_TITLE| </title>

Then open them and preg_replace the markers with PHP variables and output the result, similar to the include system above but does apply in some cases.

What I find missing in PHP is the qq syntax of Perl, which gets around all this. The HEREDOC is a substitute, it's just not as cool.

cffrost2




msg:4457116
 10:23 pm on May 23, 2012 (gmt 0)

Another templating method is to store files with "markers"

<title |THE_TITLE| </title>

Then open them and preg_replace the markers with PHP variables and output the result, similar to the include system above but does apply in some cases.


Interesting. I've seen this done before but never looked into implementing it. I have a few cases that this may benefit me. Could you go into a bit more detail on how this works? Not to change the subject, just curious :) Thanks.

rocknbil




msg:4457453
 4:22 pm on May 24, 2012 (gmt 0)

There's a number of ways, all of them using preg_replace(). You can use file_get_contents() to slurp up the templates, then preg_replace() the markers with content.

Generally speaking, the include method incrediBill mentions is better for PHP, but in some cases with complicated templates or a lot of templates, this works as well. This is so you can view the templates as .html in you browser to modify, making it part of a larger workflow (i.e., less time spent in design changes, have a designer work on designs independent of PHP.) With includes, they are all pieces. With markers and replace, you can look at whole pages.

eelixduppy




msg:4457486
 5:30 pm on May 24, 2012 (gmt 0)

I think before going into all these different ways to accomplish this, it is best to take a step back and ask the question "What are you trying to achieve?". Depending on your requirements, some of these solutions may not be best for you.

cffrost2




msg:4457644
 1:55 am on May 25, 2012 (gmt 0)

Rocknbil: this makes sense. A good idea indeed depending on the application. I'm looking at this for larger apps such as heavy back ends or front ends that are designed pre-php coding. Makes for a shorter coding session. IMO.

Eelixduppy: the stated case would be what I would use this example for. I have a designer that generally tells me where things are that need data. A list of "tags" or "markers", so to speak. I then go back and write the programming and change the tags to echo the needed php vars.

Eg:
<span id="loggedName">{LOGGED_NAME}</span>

I usually then do a "find/replace" for {LOGGED_NAME} and replace with <?php echo $logged_name; ?> but seems I could use rocknbil's eg and just preg_replace. With a proper function, i could just add the new tag list to an array and let the code do the work. Right?

Thanks for the input. I didn't mean to take over this thread. Just saw an interest. :)

whatson




msg:4458056
 6:06 am on May 26, 2012 (gmt 0)

Ok I have been doing some research, and I think I can use the <<< operator or heredoc.

$variable = <<<_END <html>
<body>
<div align="center">
<table border="0" width="980" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">"
_END

Will that work?

StoutFiles




msg:4458064
 6:49 am on May 26, 2012 (gmt 0)

<?php
ob_start();
?>

<html>
<body>
<div align="center">
<table border="0" width="980" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">test</td>
</tr>
</table>
</div>
</body>
</html>

<?php
$html = ob_get_clean();
?>

whatson




msg:4458075
 8:18 am on May 26, 2012 (gmt 0)

but how would that create a variable?

incrediBILL




msg:4458094
 9:37 am on May 26, 2012 (gmt 0)

Did you read the docs for ob_get_clean()?

Quite creative.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved