DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Inheritance in PHP: A Simple Guide With Examples
  • Automated Bug Fixing: From Templates to AI Agents
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Dynamic File Upload Component in Salesforce LWC

Trending

  • DZone's Article Submission Guidelines
  • Using Python Libraries in Java
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  1. DZone
  2. Coding
  3. Languages
  4. Practical PHP Patterns: Template View

Practical PHP Patterns: Template View

By 
Giorgio Sironi user avatar
Giorgio Sironi
·
Jul. 21, 10 · Interview
Likes (0)
Comment
Save
Tweet
Share
6.6K Views

Join the DZone community and get the full member experience.

Join For Free

In this series we have seen two Web presentation patterns so far:

  • Page Controller, also known as Action Controller, which responds to a specific HTTP request from a client.
  • Front Controller, which acts as the single entry point of a web application and routes requests to different Page Controllers.

These two patterns are part of the Controller component of an MVC implementation. But do controllers generate the HTML response directly? The response is, usually, no. Controllers are mostly glue code which deals with orthogonal concerns such as authentication, not a mean to generate a response.

In the MVC pattern, the Model is application-specific; the Controller layer is a combination of the two patterns treated earlier, while a View component is needed to complete the picture. There are different levels of complexity of the various View patterns available in the field of the web presentation, but we'll start with the classic Template View.

A Template View is a bunch of HTML code with dynamic points submerged in it; again, PHP files can be thought as an instance of this pattern.

<html>
<head>
<title>A simple Hello World script as an example of Template View</title>
</head>
<body>
<?php
echo "<p>Hello, {$_GET['name']}.</p>";
?>
</body>
</html>

Template View as an MVC component

In a web MVC approach, the data is not pulled into the view script by accessing directly the Model, because the Controller layer should be responsible for that. Nor the view script contains the behavior of the application like in the example above.

Instead, the non-static data used in the Template View is passed to it by the Controller.

As a result, the View has no dependencies towards any other classes than the ones of the object passed to it (plain data structures or Model classes such as Entities and Value Objects).

Ideally, a Template View should be declarative. It is usually implemented as a PHP script, so we have various options to render data, which ranges from the almost-declarative <?= to the programmatic one. The differences are in the noise introduced in the HTML-PHP mix and in the freedom of what can be printed.

An almost declarative mark: <?=$variable?>
A more complex statement: <?php echo $variable; ?>
A versatile approach: <?php echo $object->method(); ?>

PHP and other server-side programming languages originally started using the < and > tags because WYSIWYG editors would ignore them, and let a designer access the Template View for editing without messing up the code. There is also the problem of well-formed XML documents, which these scripts are not (though it is not usually a problem unless you want to generate them.)

PHP as a templating language even offers some alternative construct you may use in Template Views to simplify the structure of the code (bracket-less constructs):

<?php foreach ($list as $element): ?>
<a href="<?=$element?>">
<?=$element?>
</a>
<?php endforeach; ?>

Popularity

The Template View pattern is in the foundation of dynamic web pages.

PHP is born with these ideas (the recursive acronym PHP means PHP: Hypertext Preprocessor.) JSP and ASP are also similar solutions where the dynamic pieces are put into an HTML context. The problem with all these three languages when used is the quantity of business logic that get transported in the views, while we would want to separate it in a testable, reusable object model.

Smarty and some other PHP template engines take the Template View a level further, by defining a syntax for markers and parse the Template Views instead of directly executing them (thus this views are not .php files but are in some other format like .tpl). Personally I never used them since I think PHP is already a template language, which has all the flexibility I would want from one; at the same time, template engines eventually replicates the PHP syntax:

{if $edit_flag}
<input type=checkbox name=edit value="y"> edit <br>
{/if}

Advantages and disadvantages

The radical advantage of adopting a Template View pattern is the clean separation of presentation logic from business-related one. This results also in the freedom of HTML modelling from people with only basic knowledge of PHP (see designers, SEO gurus, etc.)

There are also some issues in using Template Views, but they are common to all View-related patterns. Basically, the presentation logic contained in views can be taken too further, to the point that domain-related logic scatters into the view instead of sitting in the Controller or in the Model. This is a problem which happened very often in legacy PHP applications, built before the current framework generation.

Examples

Zend Framework, our reference for Web presentation patterns, defines a generic View class which takes the path of a view script as an input, and renders it into a scope internal to one of its methods (automatically: once the controller is finished, the view is found and run.) This way, the context variables set up by the controller are made available to the view script on $this via __get and __set overloading.

<p>Hello, <?php echo $this->name; ?>.</p>

Not only variables (scalar and objects) can be put on $this, but also View helpers via __call() overloading. In this picture, custom methods can factor out presentation logic on external plugin objects which are wired to the generic View object, which forwards calls to them or returns them to view script itself.

<?php
$this->headTitle($this->object);
$this->headTitle($this->methodName);
if ($this->result) {
echo "<p>Result: $this->result</p>";
}
if ($this->form) {
echo $this->form;
}

Zend Framework also switches automatically the name of the view to render basing on some conventional parameters like format. For example, ?format=xml will cause a comments.xml.phtml to be rendered instead of comments.phtml:

<?php
if (isset($_SERVER['HTTP_HOST'])) {
$host = 'http://' . $_SERVER['HTTP_HOST'];
} else {
$host = 'http://localhost';
}
$entries = array();
foreach ($this->comments as $c) {
$guid = $host . $this->url(array('slug' => $this->article['slug']), 'article', true) . '#comment_' . $c['id'];
$entries[] = array(
'title' => 'Comment #' . $c['id'],
'link' => $guid,
'guid' => $guid,
'description' => strip_tags($c['text'])
);
}
$feed = Zend_Feed::importArray(array(
'title' => $this->article['title'],
'description' => $this->article['description'],
'author' => $this->article['author'],
'link' => $host . $this->url() . '?format=xml',
'charset' => 'utf-8',
'entries' => $entries
));
echo $feed->saveXml();

Note that all the data resources needed by the view are accessed through $this (they have to be previously injected), the View object.

PHP Template

Opinions expressed by DZone contributors are their own.

Related

  • Inheritance in PHP: A Simple Guide With Examples
  • Automated Bug Fixing: From Templates to AI Agents
  • The Blue Elephant in the Room: Why PHP Should Not Be Ignored Now or Ever
  • Dynamic File Upload Component in Salesforce LWC

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!