Php markup to html

HTML markup manipulation in PHP

Working with PHP DOM — html manipulation. It’s really very effective way to output HTML markup (at least, prevents mess of logic and markup in code). But, to generate 50 line of html markup, one must write about 200 lines in PHP 🙁 . Example piece of code: .

$signup_form = $document->createElement('form'); $signup_form->setAttribute('id', 'signup_form'); $signup_form->setAttribute('action', 'registration/signup.php'); $signup_form->setAttribute('method', 'post'); $su_fname_label = $document->createElement('label'); $su_fname_label->setAttribute('for', 'fname'); $su_fname_label_content = $document->createTextNode('Name'); $su_fname_label->appendChild($su_fname_label_content); $su_fname_textbox = $document->createElement('input'); $su_fname_textbox->setAttribute('name', 'fname'); $su_fname_textbox->setAttribute('class', 'valid'); $su_fname_textbox->setAttribute('placeholder', 'Please enter your name'); $su_fname_textbox->setAttribute('type', 'text'); 

. As you see, It’s only 1 element of form. Imagine, If there are 5 elements in HTML form, php code will be huge. I wonder, is there any way to minify for ex. by setting multiple attributes at once? Any suggestions?

Whats the problem in embedding snippets of PHP in your HTML-code? I assume it will be MUCH more readable afterwards.

apart from writing your own helper functions or dedicated classes for creating individual elements, no. DOM is a verbose API.

It’s a drawback indeed. However, I tend to not ‘make’ HTML this way: I just load in a pre-made HTML template, and fill in some details with PHP after that. I actually rather like the non-DOM-compliant DOMDocumentFragment 😉

4 Answers 4

Why don’t you just output HTML and enabled output buffering. Then you can filter out the output using the tidy extension and be sure that your output HTML is valid, while still going as fast as writing pure HTML.

Despite the above, as @Gordon suggests:

tidy is hardly suitable for programmatically authoring (x)html. its more like a post processor.

tidy has to be enabled iirc. dom is enabled by default. also, tidy is hardly suitable for programmatically authoring (x)html. its more like a post processor.

Yeah, that’s a good point. Still, I find that in environments that HAVE tidy enabled, it’s easier to use tidy. Yes, it IS a post-processor. I am also suggesting it to be used like one.

What is your reason for using the DOM to generate html output? There’s no way to make the DOM terse!

Usually you just use PHP for templating. Create a separate file like so:

Then using a template function:

function render($__filename, $__data)

Render your template from your code like so:

do_something(); $templatedata = array('title'=>'The Title'); $html = render('template.php', $templatedata); 

If you want something that is node-aware (which is a great idea for ensuring well-formedness and proper escaping!), you can use XSLT or a third-party template engine like PHPTAL.

Depending on your need for speed and if you’re able to use caching and the like, a library such as phpQuery could be of use.

It’s a «port» of kind of jQuery to PHP and has the same versatility and simplicity as jQuery . However, in my experience, it’s not quite as fast as I should like in generating the code (I have not, however, conducted any thorough tests, it’s just a feeling).

The code you’ve given as an example could be expressed along these lines with phpQuery (untested):

$form = pq ('') ->attr (array ( 'id' => 'signup_form', 'action' => 'registration/signup.php', 'method' => 'POST', )); $label = pq ('') ->html ('Name') ->attr ('for', 'fname') ->appendTo ($form); $text = pq ('') ->attr (array ( 'name' => 'fname', 'data-placeholder' => 'Please enter your name', 'type' => 'text', )) ->addClass ('valid') ->appendTo ($label); 

I’ve written it with the formatting I prefer, and it’s slightly more lines than in the code you’ve supplied; but it could easily be reduced to 5-10 or so lines with maintained readability, if you prefer more compact coding. At any rate, I find it quite a lot easier to read and you don’t have to write quite so much DOM code in order to generate quite a lot of HTML code.

Also, given that phpQuery uses the same wrapping concept as jQuery , an unwrapped node, for instance $form in the above code, is a regular DOMNode unless wrapped in pq() . This gives you a lot of flexibility to either use phpQuery ‘s convenience routines, or the more «fundamental» functionality of PHP DOM .

Источник

How does PHP interact with HTML and vice versa?

PHP is not an alternative to HTML. If you want to represent web-pages, you need to use HTML markup. PHP is merely a programming language which is (in this context) used to dynamically generate HTML markup. So, if you request a PHP file, and expect a web-page in return, the PHP script has to generate the HTML markup and send it in the response. You can put HTML markup directly into the PHP file, and you can also use templating engines (or manual techniques) to generate the HTML markup based on data from a database.

4 Answers 4

HTML is the language of the web. It is a markup language which means that the only thing we can use it for is to «markup» documents, i.e. design how content will look to the end user.

Imagine we had a page that showed the user the date.

We could use some HTML to do that:

But say we wanted to keep that page up to date. We’d have to go and manual change the date manually everyday. Because HTML is static, it can’t be changed dynamically.

Perhaps it would be useful to be able to generate automatically adding the correct date to the page, depending on when the page is loaded.

That is where PHP comes in. PHP is a scripting language, and while it can be used for lots of things, one of its main uses is to generate HTML dynamically. So instead of writing in today’s date — what we could do is use some PHP and say.

This will print out for me «Sunday 26 August 2012» today, «Monday 27 August 2012» tomorrow, and so on.

I’d need to save this new version of my page as page.php instead of page.html, because I need my server (which is set up use the PHP) to send the page to PHP interpreter. It will look for the special

We can do lots of cool stuff with PHP. It is «server side» technology, meaning that it does its work on the server and then sends us the finished page with all the dynamic content added.

Sometimes we might want to control and modify a page after it has reached the user’s browser. For this we will need some «client side» technology, i.e. code that runs in the user’s browser. And the most common client side language of choice is javascript.

Again we can do a lot with Javascript, but most often we use it in web pages to allow us to control elements of a HTML page after it has reached the user.

We might want to hide something on a page and then only show it once a user has clicked a button. We can do that with javascript.

Now because Javascript is «client side» technology, i.e. it runs in your browser it can actually be quite hard to use, because you will have to write code that works in a variety of different browsers, and now on mobile phones too! To make this job easier, very smart developers have taken a lot of the pain out of using javascript to control elements in web pages by creating libraries and frameworks to use. One of the most popular of these is the jQuery framework. I think jQuery is the most fun thing to learn, because it allows you to do all of the «cool stuff» in webpages — make stuff fade in, make stuff fade out, play sounds, move elements around etc etc

I hope this helps you work out how different technologies can help you achieve different things.

The TL;DR version of this would be:

HTML & CSS — sets out how your pages are going to look.

PHP — helps you to generate HTML dynamically.

JavaScript — helps you make your pages more interactive and can respond to clicks or other actions of your user.

The most important thing to understand is the difference between HTML and PHP. In HTML you write your code, upload it, and the user’s will subsequently download that page along with all the code. The user’s browser interprets this code and shows the user the page as you intended it (hopefully). In other words HTML is sort of what you see is what you get, in the sense that all the code goes to the user and is interpreted by the browser.

With PHP it works a bit differently because you don’t actually download the code the author wrote. What happens is that if you want to download a php page the code in that file is first processed by the server, and you download the output of the code, as opposed to the whole code as is. This in turn will be HTML just as before, this is why you never see PHP code in the source of a webpage.

With PHP the goal is to use the processing powers of the server to build (usually) dynamic webpages. A very basic example is showing the correct greeting for the time of day on a webpage.

PHP processor scans the page, character by character.
Until a (this text may be HTML, XML, JavaScript, or anything else).
Once in a Any standard «print» output from PHP is sent to the outgoing http stream.
Once a ?> is found the stream reverts to the original copy mode.

PHP has the ability to send HTML, CSS, JavaScript, or anything else. You may need to force content type, but it can be done.

There’s nothing really special about PHP. The basic difference is between static files and dynamic files written in a programming language.

Static files are simply sent directly by the server to the browser. These aren’t just HTML, this is also done for image files. And when you download applications or PDF, the same mechanism is used — it might be a ZIP file, an EXE, a disk image (common for Mac downloads).

In the case of dynamic files, the file is executed in some way, and the output it produces is sent to the browser. The dynamic file can be in any language — it could even be a binary compiled executable. However, scripting languages have generally been most popular, simply because they tend to be easier to write web applications in. And as a result, there are lots of libraries that have been written to support web applications — it’s a positive feedback situation. In the early days of the web, Perl was probably the most common language; we didn’t have the plethora of scripting languages that we do now.

What makes PHP somewhat special is that it was designed specifically for scripting web pages. In all other languages, you have to write explicit commands to produce any output. The PHP processor simply outputs the file contents verbatim until it encounters the , at which point it reverts to verbatim output.

Another way to think of it is that anything outside is treated as if it were a big echo statement. In fact, this model is necessary to understand that you can actually switch modes in the middle of a statement. You can do:

 some text else < ?>some other text

This is obviously a silly way to output just one line, but imagine if it were a huge block of text. Basically, PHP’s design allows you to write a normal HTML web page, and then embed programming code where you need it to produce dynamic content.

And while it’s most common for PHP scripts to output HTML, they don’t always. It’s not uncommon to have a PHP script that outputs images. It might do it by using a database to store the image or the location of an image file. It also has built-in and library functions that can generate image data on the fly. For instance, if you go to a web site that produces graphs, those graphs could have been produced by a PHP script.

Источник

Читайте также:  Java jar one browser
Оцените статью