Jump to content

Recommended Posts

I've been looking through the DOM manual and I understand how to work with a new XML object and an externally loaded one, but I can't figure out how to get reference to HTML in the same file as the PHP, but outside of the PHP tags...

 

Fill in the pseudo-code:

 

<html>
  <?php
    // Reference that div below me.
    // Manipulate it.
    // Write it back in its same location, but with new code around it.
  ?>
  <div id="something_to_be_referenced">
    Something to be replaced.
  </div>
</html>

 

Elaboration:

 

I'm trying to template my HTML page by referencing the HTML content in PHP, manipulating it as an XML object, then putting it back with the template content wrapped around it.

 

I've done a similar thing by referencing content in an external file, and then writing the manipulated content into the current, blank file. Now, I want to get reference to and manipulate an outer-lying HTML tag that's in the same file as the PHP tag.

 

And I've done this with Javascript by waiting for the page to load, then writing a specific tag's inner HTML.

 

How about:

 

<html>
  <?php
    // Reference that div below me.
    // Manipulate it.
    // Write it back in its same location, but with new code around it.
  ?>
  <div id="something_to_be_referenced">
    <?php print $Something_to_be_ replaced; ?>
  </div>
</html>

Php is all back end.  It is not possible to write to a live page that the user is viewing.  More likely you will have to open up the page directly with php and either re-write all the necessary code or do something with include statements radi8 said or possibly like this..

 

<html>
  <?php
include "xmlWriter.php";
  ?>
  <div id="something_to_be_referenced">
    Something to be replaced.
  </div>
</html>

 

Or something along those lines.

radi8 - Yes, that seems to be the traditional PHP-way of doing things, which I'm learning. I was trying to avoid interweaving my template code with the content code.

cunoodle2 - I had a feeling that was the case.

 

It's hard to find out info like this as a beginner because I'm looking to do something the wrong way, which nobody understands or anything about. So, here I am trying to figure out the impossible with no clues from anyone. Thanks, guys!

 

Normally, I would load my HTML as external files. I'm going to try loading the HTML page being navigated to as if it were external via something like:

$doc = new DOMDocument();
$doc->loadHTMLFile($_SERVER['PHP_SELF']);

 

I'll post again whether this works.

NO. ;D That didn't take long for me to realize.

I can read it, sure, but I still won't be able to write over the HTML that's already on the page.

But, now this leaves me with some strange looking webpages. My templated HTML pages that are navigated to look literally like this:

// Include config.
<?php require_once($_SERVER['DOCUMENT_ROOT'].'/includes/config.php');?>
// Include template.
<?php require_once($_SERVER['DOCUMENT_ROOT'].'/includes/template.php');?>
// Run the template over another file.
<?php applyTemplate('path_to_my_content.xml'); ?>

 

That's it! Is it just me, or does this look ridiculously sparse  ???

Yes, that seems to be the traditional PHP-way of doing things, which I'm learning. I was trying to avoid interweaving my template code with the content code.

 

This is essentially impossible.

 

In dynamic database driven pages, your template will always be displaying dynamic data.  Therefore your template will always need:

+ a way of inserting dynamic data (i.e. PHP variables and their values)

+ a way to perform simple logic (i.e. if-[else]-endif )

+ a way to iterate (i.e. dumping result sets)

 

Therefore your template always needs, in addition to whatever output syntax you are generating (XHTML, HTML, XML, json, csv) special syntax to accommodate the three needs I presented above.  Some people have gone as far as to invent "templating" languages like smarty.  I really don't see why they do this though, because PHP is a template language itself.

 

One can make an argument that there are a developer and designer working together and the designer doesn't know PHP.  But they'll still have to learn whatever template engine syntax they decide to use, such as smarty.  So they might as well learn the basics of PHP anyways.  Also, a designer that can't handle simple programming logic in the template is probably not going to create a template sufficient for use in pages powered by lots of JavaScript.

 

Essentially, every designer should know enough of PHP to create templates using the output format (XHTML, HTML, XML, etc) and PHP to drive template decisions.  As the developer, if you want to make the amount of PHP the designer has to know and understand minimal, then you can process and simplify the information before it ever gets to them.

 

This can be inefficient though as it leads to things such as iterating over result sets from the database twice.  For example the developer fetches the data, iterates over it, cleans some things up, and slams it into an array.  Then the code passes over to the template where the designer now iterates over the array and slams it into the template.  Now you've just got double processing.

 

Personally I use the controller and view philosophy.  My controller selects and prepares data but without iterating over it.  I then invoke the view where I perform iterations, some decisions, and maybe other presentation cleanup routines.

Yo, awesome post!  :D

 

Personally I use the controller and view philosophy.  My controller selects and prepares data but without iterating over it.  I then invoke the view where I perform iterations, some decisions, and maybe other presentation cleanup routines.

 

Yeah! Model-View-Controller runs my life! Your approach seems attractive, but I'm trying to comprehend its PHP implementation. Maybe something like...

CONTROLLER: Pull database data relevant to the requested page and store in variables.

VIEW: Draw HTML for layout, embedded with PHP tags for unique data.

??

A specific example your controller's tasks and view's tasks would make this clear. Got a link for me?

 

But it still has me wondering -- is it normal to have a file, representing a page of my site, with like 3 lines of code? I'm coming from an HTML/CSS approach to websites, so this seems strange. If my data is in a database, my template is in a PHP file, my style is in a stylesheet, and my behavior is in a JS file, then what's left for that page to do that gets navigated to? It seems like a blank canvas, essentially.

<?php
// Located in: project/app/controllers/
// PostsController.php

class PostsController extends Controller {
  // Displays: http://www.domain.com/Posts/, http://www.domain.com/Posts/Index
  public function IndexAction() {
    $this->View->Posts = Posts::GetMostRecent( 10 ); // Most recent 10 posts, just an example of fetching data, this might actually return a PDOStatement
  }
}
?>

 

<?php
// Located in: project/app/views/Posts/
// Index.php
// The view for the PostsController::IndexAction method
?>
<h1>Most Recent Posts</h1>
<?php if( $this->Posts->rowCount() ) { ?>
  <ul>
  <?php while( $post = Posts::Next( $this->Posts ) ) { ?>
    <li><?php echo $post->Title; ?></li>
  <?php } ?>
  </ul>
<?php } else { ?>
  <p>There are no posts to view.</p>
<?php } ?>

 

I have my own framework I use at work that implements nicely the Controller and View.  There is a bit of processing I perform that is not shown here that maps URLs to the appropriate Controller, Controller method, and View.

Coolio :) I hadn't considered PHP OOP. It looks like that's where I'm headed, but don't yet have the time for.

 

Your mention of "URL mapping" sounds like the more sensible alternative to what I'm doing with my 3-line files.

 

Thanks much for your help!

If you want to get a jump start on that, then all you need to know is mod_rewrite for Apache.

 

I design my applications so that the only files that are web accessible are an index.php and served files (JS, CSS, images, etc).  All of my PHP files (except the one index.php) reside in a non-web accessible directory.

 

I include mod_rewrite rules that send all requests that don't correspond to an existing file or directory to index.php.  Index.php loads my framework, performs some initial set up, and then sets the framework in motion.  The framework examines the URL ($_SERVER['REQUEST_URI']) to determine where to send it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.