If I don't use the template engine with my PHP, what should my code look like?

I do not want to use the MVC structure. I do not want to use the template engine. I am a few people shop where developers, where all the hats, no schedules. We do everything (all layers). I don’t want the code to be mixed with the presentation, as I have with the classic ASP.

But, I do not know how my code should look like between the server and the actual view.

If I don’t scare the HTML in my server side code, how does it get to the HTML page so that I can do something like <span><?= $myvar ?></span> ? and put loops on the html page?

Thanks for any advice.

+4
source share
7 answers

To use loops and all, I use alternative syntax for control structures.

Example:

 <div id="messages"<?php if(!(isset($messages) && count($messages))): ?> class="hidden"<?php endif; ?>> <?php if(isset($messages)): ?> <?php foreach($messages as $message): ?> <div class="message"><?php echo $message; ?></div> <?php endforeach; ?> <?php endif; ?> </div> 

For more information see this: http://php.net/manual/en/control-structures.alternative-syntax.php

Oh, I also use the semi-MVC structure, where I have a class that processes templates (views), basically it's just a class in which I create an instance, pass in a set of variables, and then render the template when the instance is destroyed. I have an array of variables in this class, and then use extract to pass all the variables to include, for example:

 extract($this->variables, EXTR_SKIP); include($this->file); 

EDIT: Here is the same example in Smarty:

 <div id="messages"{if isset($messages) && !count($messages)} class="hidden"{/if}> {if isset($messages)} {foreach from=$messages item=message} <div class="message">{$message}</div> {/foreach} {/if} </div> 
+5
source

Simple PHP projects usually generate full HTML instead of populating templates, so you are just echo in your PHP code.

This gets messy, so you end up coding some kind of template system for any moderately complex site.

A possible alternative is to use your page as completely static HTML / CSS and use AJAX to dynamically display the actual content (JSON will be a good transport format, it is native to JS and can be easily generated with PHP). This will save you all the HTML placed in your PHP code. Whether this is a viable alternative or not depends on the case.

+2
source

<span><?= $myvar ?></span> works.

The loop will look like this:

 <html> <body> <?php for ($i=1; $i<=5; $i++) { echo "The number is " . $i . "<br />"; } ?> </body> </html> 

An example taken from here .

+1
source

I really recommend you use php Template Inheritance . (Don't let this scare you, this is just one file.) This is a simple set of functions that helps you create extensible PHP views without the problems and limitations of a guide.

This is still pure PHP, so you don’t need to learn some weird template language. It may not be much, but it is really powerful once you start using it.

0
source

Of course, you can do all this yourself, but what you need is an MVC template or separation of problems ("I do not want the code to be mixed with the presentation"). Or at least MV, for very simple applications (although it is still dirty, the presence of models directly affects the presentation).

The easiest way to achieve this is to first collect and process all the data, and then simply print it. In php files directly opened on the Internet, the use of complex code is not allowed.

 <?php require('model.inc'); process_capuchin_monkey_order_form(); ?> ... <h1>Thank you for your order of <?php echo $order->num_monkeys; ?> monkeys.</h1> ... 
0
source

usually you just need to make sure that you have at least PHP in your HTML as much as possible. This means that all data processing is performed before processing and just a set of variables is passed in one way or another to a method or function that includes HTML.

Any HTML with interleaved PHP can be considered a template. Here's a simplified example:

 // view class class View { public function render($html_template) { include('view_path/' . $html_template . '.php'); } } // html template file 'view_path/main.php' <html> <body> <h1><?= $this->title ?></h1> </body> </html> // usage $view = new View(); $view->title = 'Some Title'; $view->render('main'); 
0
source

You should use MVC separation of interests no matter what you do. It means:

  • At least one file is all html and it is provided with several variables. (View / Template)
  • At least one file is all php and only talks to the database. (Model)
  • At least one file processes the http request, extracts data from the database, and performs the presentation.

The core of every php templating language is the use of extract() and include inside a function:

 function render_template($___filename, $___data) { extract($___data, EXTR_SKIP); include $__filename; } 

You can deal with this with the class interface (view objects) or with output buffering, but this is the core of every template system. The responsibility of your controller is simply to collect this argument $__data (usually with data from the database) for a given view.

0
source

Source: https://habr.com/ru/post/1387594/


All Articles