My page

Generate HTML With PHP

Hi. Have you met KvzHTML? It’s a standalone PHP Class for generating HTML.

It’s been hiding deep inside the caverns of my secret GitHub repo: kvzlib

  • a collection of code snippets too small or unfinished to deserve their own repository. But I find working with this class so pleasant, I thought I’d share the fun.

KvzHTML (yeah, not the most imaginative name, sorry for that) will generate regular HTML or XML, it just makes the job a bit easier on you because of these features:

  • Automatic Table Of Contents support
  • Very compact syntax
  • Tag nesting & automatic indentation

Without wasting your time going on & on about it, just let me show you some examples, and you be the judge.

Introduction

 error_reporting(E_ALL); require_once 'KvzHTML.php'; // These are the default options, so might // as well have initialized KvzHTML with an // empty first argument $H = new KvzHTML(array( 'xhtml' => true, 'track_toc' => false, 'link_toc' => true, 'indentation' => 4, 'newlines' => true, 'echo' => false, 'buffer' => false, 'xml' => false, 'tidy' => false, )); echo $H->html( $H->head( $H->title('My page') ) . $H->body( $H->h1('Important website') . $H->p('Welcome to our website.') . $H->h2('Users') . $H->p('Here\'s a list of current users:') . $H->table( $H->tr($H->th('id') . $H->th('name') . $H->th('age')) . $H->tr($H->td('#1') . $H->td('Kevin van Zonneveld') . $H->td('26')) . $H->tr($H->td('#2') . $H->td('Foo Bar') . $H->td('28')) ) ) ); ?> 

Result

      

Important website

Welcome to our website.

Users

Here's a list of current users:

id name age
#1 Kevin van Zonneveld 26
#2 Foo Bar 28

Automatic TOC

Keeping track of a Table of Contents can become quite tedious because every change you make; you have to make twice. So why not let KvzHTML handle this for you?

In this example I also show you that

You can open a tag by setting it’s body to TRUE, or by leaving it empty.

You can then later close it with ->tag(false)

  • You can set echo on if you don’t nest KvzHTML functions, so that every function will immediately get echoed.
  • ..unless you turn on buffering.

all these different options can lead to the same thing. But are there to reduce your need to type to an absolute minimum.

Result

  • New application
    • Users
      • Permissions
        • General Concept
        • Exceptions
        • Point 1
        • Point 2
        • Point 3
          Has some implications.

        Generating XML

        As an added bonus, you can even make XML documents with KvzHTML

         error_reporting(E_ALL); require_once 'KvzHTML.php'; $H = new KvzHTML(array( 'xml' => true, )); echo $H->xml( $H->auth( $H->username('kvz') . $H->api_key(sha1('xxxxxxxxxxxxxxxx')) ) . $H->server_reboot( $H->dry_run(null) . $H->hostname('www1.example.com') . $H->server_id(888) ) ); ?> 

        Result

          kvz a7a7c2e911a47b967d34b5a8807c040e9d167815   www1.example.com 888  

        Special Functions

        I had most fun with KvzHTML while generating HTML that would be converted to PDF afterwards. These were massive HTML documents that no designer was going to touch. So no need to keep the HTML plain for Dreamweaver or anything.

        In this light, some of the functions below will make more sense.

         error_reporting(E_ALL); require_once 'KvzHTML.php'; // I find it easy to work with 2 instances. // One that will echo directly: $E // and One that supports nesting: $H $H = new KvzHTML(); $E = new KvzHTML(array('echo' => true, 'buffer' => true, 'tidy' => true)); // To save you even more typing. The following tags // have an inconsistent interface: // a, img, css, js $E->html(); $E->head( $H->title('Report') . $H->style(' div.page < font-family: helvetica; font-size: 12px; page-break-after: always; min-height: 1220px; width: 830px; >') . $H->css('/css/style.js') . $H->js('/js/jquery.js') ); // Page 1 $E->page(true, array('style' => array( 'page-break-before' => 'always', ))); $E->h1('Report') . $E->p( $H->a('https://true.nl', 'Visit our homepage') . $H->img('/assets/images/posts/2009-10-11-generate-html-with-php-0.png') ); $E->ul( $H->li('Health') . $H->li('Uptime') . $H->li('Logs') . $H->li('Recommendations') ); $E->page(false); // Page 2 $E->page(); $E->float($H->img('https://en.gravatar.com/userimage/3781109/874501515fabcf6069d64c626cf8e4f6.png')); $E->float($H->img('https://en.gravatar.com/userimage/3781109/874501515fabcf6069d64c626cf8e4f6.png')); $E->clear(); $E->page(false); // Page 3 $E->page( $H->h2('Warnings') . $H->p('Disk space', array('class' => 'warning')) ); $E->html(false); echo $E->getBuffer(); ?> 

        Result

        div.page div.c3 div.c2 div.c1

        Report

        Visit our homepage

        • Health
        • Uptime
        • Logs
        • Recommendations

        Warnings

        Disk space

        And There You Have it..

        Get it while it’s hot, and if you have improvements: Leave me a comment or even better: GitHub me some patches! 🙂

        Источник

        How to generate a HTML page dynamically using PHP?

        I have a page which displays info about a property, based on the unique ID from the url, searching the mysql database for that ID, getting all the info from that row etc, fairly standard really. I was wondering if/how I can ‘create’ a html page for each database row, as I’m assuming this would be better for SEO? Having multiple pages with keywords on rather than one dynamic page? My properties are added to the database by a form/ upload system on the site, I was thinking creating the page on upload might be easiest, but am open to suggestions!

        8 Answers 8

        Just in case someone wants to generate/create actual HTML file.

        $myFile = "filename.html"; // or .php $fh = fopen($myFile, 'w'); // or die("error"); $stringData = "your html code php code goes here"; fwrite($fh, $stringData); fclose($fh); 

        I have a document where it will print an html document. Along with the html document it comes variables from the main php document. How can I print variables from the main php document to the dynamically created html document?

        I was wondering if/how I can ‘create’ a html page for each database row?

        You just need to create one php file that generate an html template, what changes is the text based content on that page. In that page is where you can get a parameter (eg. row id) via POST or GET and then get the info form the database.

        I’m assuming this would be better for SEO?

        Search Engine as Google interpret that example.php?id=33 and example.php?id=44 are different pages, and yes, this way is better than single listing page from the SEO point of view, so you just need two php files at least ( listing.php and single.php ), because is better link this pages from the listing.php .

        Extra advice:

        example.php?id=33 is really ugly and not very seo friendly, maybe you need some url rewriting code. Something like example/properties/property-name is better 😉

        accepted this as it gives some extra info which is useful, thank you, ill try some of this out when I get home

        As per your requirement you dont have to generate a html page dynamicaly. It can be done by .htaccess file .

        Still this is sample code to generate HTML Page

        you can create any .html , .php file just change extention in file name

        It looks funny but it works.

           

        New HTML file

        "; // Write the contents back to the file file_put_contents($file, $current); ?>

        You dont need to generate any dynamic html page, just use .htaccess file and rewrite the URL.

        I’ve been working kind of similar to this and I have some code that might help you. The live example is here and below, is the code I’m using for you to have it as reference.

        create-page.php

        ', '', '', '', ''); // Get the couples-template.php as a string. $template = file_get_contents($template_path.$template_file); // Fills the template. $new_file = str_replace($placeholders, $data, $template); // Generates couple's URL and makes it frendly and lowercase. $couples_url = str_replace(' ', '', strtolower($data['groom-name'].'-'.$data['bride-name'].'.php')); // Save file into couples directory. $fp = fopen($couples_path.$couples_url, 'w'); fwrite($fp, $new_file); fclose($fp); // Set the variables to pass them to success page. $_SESSION['couples_url'] = $couples_url; // If working in root directory. $_SESSION['couples_path'] = str_replace('.', '', $couples_path); // If working in a sub directory. //$_SESSION['couples_path'] = substr_replace($base_path, '', -1).str_replace('.', '',$couples_path); header('Location: success.php'); ?> 

        Hope this file can help and work as reference to start and boost your project.

        Источник

        Saved searches

        Use saved searches to filter your results more quickly

        You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

        Php class to generate html code

        License

        Airmanbzh/php-html-generator

        This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

        Name already in use

        A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

        Sign In Required

        Please sign in to use Codespaces.

        Launching GitHub Desktop

        If nothing happens, download GitHub Desktop and try again.

        Launching GitHub Desktop

        If nothing happens, download GitHub Desktop and try again.

        Launching Xcode

        If nothing happens, download Xcode and try again.

        Launching Visual Studio Code

        Your codespace will open once ready.

        There was a problem preparing your codespace, please try again.

        Latest commit

        Git stats

        Files

        Failed to load latest commit information.

        README.md

        Create HTML tags and render them efficiently.

        return HtmlTag::createElement(); // returns an empty HtmlTag Container
        return HtmlTag::createElement('a'); // returns an HtmlTag containing a 'a' tag
        • it always generates valid HTML and XHTML code
        • it makes templates cleaner
        • it's easy to use and fast to execute
        echo(HtmlTag::createElement('a'));
        $tag = HtmlTag::createElement('a') echo( $tag );
        echo HtmlTag::createElement('div');
        echo(HtmlTag::createElement('p')->text('some content'));
        echo(HtmlTag::createElement('div')->addElement('a')->text('a text'));
        $container = HtmlTag::createElement('div'); $container->addElement('p')->text('a text'); $container->addElement('a')->text('a link');

        Classics attributes (method : 'set')

        $tag = HtmlTag::createElement('a') ->set('href','./sample.php') ->set('id','myID') ->text('my link'); echo( $tag );
        a href='./sample.php' id='myID'>my linka>

        Shortcut to set an ID attribute (method : 'id')

        $tag = HtmlTag::createElement('div') ->id('myID'); echo( $tag );

        Class management (method : 'addClass'/'removeClass')

        $tag = HtmlTag::createElement('div') ->addClass('oneClass') ->text('my content') echo( $tag );
        div class pl-s">oneClass">my contentdiv>
        $tag = HtmlTag::createElement('div') ->addClass('aClass') ->addClass('anothereClass') ->text('my content') echo( $tag );
        div class pl-s">aClass anothereClass">my contentdiv>
        $tag = HtmlTag::createElement('div') ->addClass('firstClass') ->addClass('secondClass') ->text('my content') ->removeClass('firstClass'); echo( $tag );
        div class pl-s">secondClass">my contentdiv>

        Text and content are generated according to the order of addition

        $tag = HtmlTag::createElement('p') ->text('a text') ->addElement('a') ->text('a link');

        To generate content before text, 2 solutions :

        $tag = HtmlTag::createElement('p') ->addElement('a') ->text('a link') ->getParent() ->text('a text');
        $tag = HtmlTag::createElement('p'); $tag->addElement('a')->text('a link'); $tag->text('a text');

        Источник

        Читайте также:  There is no php closing tag in this file
Оцените статью