← Back to team overview

anewt-developers team mailing list archive

[Branch ~uws/anewt/anewt.uws] Rev 1822: [page] Refactor HEAD/BODY building into own methods

 

------------------------------------------------------------
revno: 1822
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt
timestamp: Thu 2011-03-17 17:17:53 +0100
message:
  [page] Refactor HEAD/BODY building into own methods
  
  This is cleaner, but more importantly allows AnewtPage
  descendants to override build_body() for complete
  control over the way a page is assembled from its blocks.
modified:
  page/module.doc.xml
  page/page.lib.php


--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws

Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription
=== modified file 'page/module.doc.xml'
--- page/module.doc.xml	2011-03-17 14:52:38 +0000
+++ page/module.doc.xml	2011-03-17 16:17:53 +0000
@@ -82,11 +82,11 @@
 
 	<anewt:section>
 
-		<anewt:title>Simple and block-based pages</anewt:title>
+		<anewt:title>Simple, block-based and fully customisable pages</anewt:title>
 
-		<p><anewt:classref>AnewtPage</anewt:classref> allows you to build
-			two slightly different and mutually exclusive page types: simple
-			pages and block-based pages.</p>
+		<p><anewt:classref>AnewtPage</anewt:classref> supports two slightly
+			different and mutually exclusive page types: simple pages and
+			block-based pages.</p>
 
 		<p><strong>Simple pages</strong>, as the name suggests, allow you
 			to build simple pages by setting some properties and adding some
@@ -131,6 +131,16 @@
 			<anewt:title>Adding content to a block-based page</anewt:title>
 		</anewt:example>
 
+		<p>If neither the simple page type or the block-based page type
+		suits your needs, you can completely override the way the page is
+		assembled. To do this, override the
+		<anewt:functionref>AnewtPage::build_body()</anewt:functionref>
+		method and provide your own code to assemble the BODY element of
+		your HTML page. This way you can still use all the API that is
+		available for block-based pages, including the array operators,
+		while the output is fully customisable. See the API docs for more
+		information about this.</p>
+
 	</anewt:section>
 
 	<anewt:section>

=== modified file 'page/page.lib.php'
--- page/page.lib.php	2011-03-17 15:18:17 +0000
+++ page/page.lib.php	2011-03-17 16:17:53 +0000
@@ -230,8 +230,8 @@
 	}
 
 	/** \} */
-	
-	
+
+
 	/** \{
 	 * \name Content methods
 	 *
@@ -334,7 +334,7 @@
 
 		$block = null;
 
-		if (array_key_exists($name, $this->_blocks)) 
+		if (array_key_exists($name, $this->_blocks))
 		{
 			$block = $this->_blocks[$name];
 
@@ -346,7 +346,7 @@
 			if (method_exists($this, $block_build_method))
 				$block = $this->$block_build_method();
 		}
-		
+
 		return $block;
 	}
 
@@ -363,22 +363,17 @@
 	 */
 
 	/**
-	 * Render this page into XHTML.
-	 *
-	 * This methods renders the whole page into a complete XHTML page. Usually
-	 * you want to use flush() to output the page to the browser.
-	 *
-	 * \return
-	 *   The rendered page as a string.
-	 *
-	 * \see AnewtPage::flush
+	 * Build the \c HEAD node and all its contents.
+	 *
+	 * This method assembles all the properties of this AnewtPage instance and
+	 * fills the elements in the \c HEAD node. This includes stylesheets, links,
+	 * metadata, and so on.
+	 *
+	 * \return Completely populated AnewtXHTMLHead node
 	 */
-	public function render()
+	private function build_head()
 	{
-		/* Create basic element nodes */
-
 		$head = new AnewtXHTMLHead();
-		$body = new AnewtXHTMLBody();
 
 		/* Content-type in meta tag. This must be the first element inside the
 		 * <head>...</head> element. */
@@ -481,8 +476,24 @@
 		}
 
 
-		/* Body content */
-
+		/* Return */
+
+		return $head;
+	}
+
+	/**
+	 * Build the body node.
+	 *
+	 * This method implements all the \c blocks logic and assembles the page. If
+	 * a different way to assemble the page from various blocks is needed, this
+	 * method can be overridden. The only requirement in that case is that the
+	 * return type is a AnewtXHTMLBody instance.
+	 *
+	 * \return AnewtXHTMLBody instance.
+	 */
+	protected function build_body()
+	{
+		$body = new AnewtXHTMLBody();
 		if ($this->_get('blocks'))
 		{
 			/* This is a page using div blocks */
@@ -542,7 +553,26 @@
 		}
 
 
-		/* Assemble the top level elements */
+		return $body;
+	}
+
+	/**
+	 * Render this page into XHTML.
+	 *
+	 * This methods renders the whole page into a complete XHTML page. Usually
+	 * you want to use flush() to output the page to the browser.
+	 *
+	 * \return
+	 *   The rendered page as a string.
+	 *
+	 * \see AnewtPage::flush
+	 */
+	public function render()
+	{
+		$head = $this->build_head();
+		$body = $this->build_body();
+
+		assert('$body instanceof AnewtXHTMLBody; // custom build_body must return a AnewtXHTMLBody instance');
 
 		$document = new AnewtXMLDomDocument();
 		$document->set_document_type($this->_get('document-type'));