www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] Storing Static Page Data

  1. #1
    Join Date
    Aug 2008
    Posts
    45

    resolved [RESOLVED] Storing Static Page Data

    Hi. I'm new.

    I've been making web sites for just over 10 years but in the past few years, I've been striving toward the paradigm of separation of style, structure, and content. I love that it keeps everything so organized and accessible.

    So my latest project, I've been taking this concept to as far of an extreme as I can. What I want to do is create a file that contains all of the data for a particular page, which may include the title, main body text, and other section-specific text, and then have my structural skeleton call each part through a php include.

    My problem is that I'm not sure how I should structure my page-data file to facilitate php insertion while keeping it as simple and readable as possible.

    One way that I've done it is to code the data file as a php file and call each section with functions that dump text/HTML. This seems overly complicated and inelegant. I've also tried saving HTML code as PHP variables, but I've been running into problems with existing code having quote marks in the text or attributes, and although I could slash them out, I'd like a more elegant solution if it exists.

    I've also considered using XML, but I'm not sure how easy it is to store HTML data as a single XML node and call it using php.

    Ideally, I would like something to the extent of :

    Code:
    title: Welcome to Page A
    
    body: This is my body with <b>HTML</b> code
    
    sectionA: This text goes in the section A area of page A
    
    [etc...]
    i.e. all page relevant data in one file, where each section is then called to the correct location with PHP.

    Does that make sense? Any thoughts are appreciated!!

    Thanks in advance!

  2. #2
    Join Date
    Aug 2008
    Posts
    45
    Hi again:

    I figured out how to use XML (relatively) elegantly to store page data with sections that have HTML tags.

    I stumbled across the CDATA tag that, once surrounded, all inner text is left unparsed, which means I can put in HTML or scripts without worrying about escaping characters.

    Here's an short example of how I'm using it:

    pagedata.xml (storing data):
    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <rss version="0.91">
    <title>Hi there!</title>
    <body><![CDATA[
    Welcome to my page!  Check out this <a href="blah.html">link</a>!
    ]]></body>
    </rss>
    index.php code (structure):
    PHP Code:
    <?php
    //load in xml using simplexml_load_file(filename)
    //normally, datafile selection is determined from url parsing
    //    e.g. index.php?page=home tells me to get home.xml
    //but for demonstration, I'll just stick in the xml file from above
    $data simplexml_load_file("pagedata.xml");?>
    <html>
    <head>
    <title><?php print $data->title?></title>
    </head>
    <body>
    <?php print $data->body?>
    </body>
    </html>
    This code will insert the relevant sections and the HTML data surrounded by CDATA will be inserted as normal text:

    index.php output:
    HTML Code:
    <html>
    <head>
    <title>Hi there!</title>
    </head>
    <body>
    Welcome to my page!  Check out this <a href="blah.html">link</a>!
    </body>
    </html>
    So far, I really like this solution. The output is clean and along with mod_rewrite on Apache, all server-side activity is hidden. There's no dealing with unnecessary and messy client-side construction, i.e. using Javascript to handle XML. The xml data file has very little overhead, and is easy to read, modify, and extend. And thanks to simplexml, the page construction is indeed simple.

    Any questions and comments are welcome. I'm still relatively new at this separation paradigm, but I'm really liking it so far.

    Here are some links that were helpful to me:
    CDATA description
    SimpleXML

  3. #3
    Join Date
    Aug 2008
    Posts
    45
    EDIT that XML file should not be using rss tags obviously. That was a copy/paste oversight.

    Code:
    <?xml version="1.0" encoding="utf-8" ?>
    <mypage>
    <title>Hi there!</title>
    <body><![CDATA[
    Welcome to my page!  Check out this <a href="blah.html">link</a>!
    ]]></body>
    </mypage>

  4. #4
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    99% of the power of XML is the fact that you have DOM or SAX type parsers available in essentially every programming language which renders the old bugaboo of flat file parsing moot.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles