CakePHP Recommended Syntax for Templates (Views)

Ever since I used CakePHP, I have been asking myself a better understanding of the recommended syntax for CTP files, which is basically an HTML file with all the PHP code enclosed in square brackets with tags. I find it very difficult to read, and I should think that the context is switching between HTML and PHP, will add some performance limitation. Wouldn't it be faster and clearer to collect all the output in a string and echo at the end? But there is an even deeper meaning, I just don’t see it. To make yourself clearer, here is an example:

CakePHP:

<?php if (!empty($file['User']['email'])): ?>
<div class="mailto"><?php echo $this->Html->link($file['User']); ?></div>
<?php endif; ?>
<?php if (!empty($file['Document']['comments'])): ?>
<div class="file-comment file-extra column grid_6">
<div class="content"><?php echo $file['Document']['comments']?></div>
</div>
<?php endif; ?>

My approach:

<?php
$out = '';
if (!empty($file['User']['email'])) {
 $out .= '<div class="mailto">'.$this->Html->link($file['User']).'</div>';
}
if (!empty($file['Document']['comments'])) {
 $out .= '<div class="file-comment file-extra column grid_6">'
  .'<div class="content">'.$file['Document']['comments'].'</div>'
  .'</div>';
}
echo $out;
?>

So my question is: what are the disadvantages of my approach compared to CakePHP?

+4
1

: PHP, - . , HTML PHP, . , IDE.

, , :

<?php if (!empty($file['User']['email'])): ?>
<div class="mailto"><?php echo $this->Html->link($file['User']); ?></div>
<?php endif; ?>
<?php if (!empty($file['Document']['comments'])): ?>
<div class="file-comment file-extra column grid_6">
<div class="content"><?php echo $file['Document']['comments']?></div>
</div>
<?php endif; ?>

... , .


, , . , , .

  • HTML , .

  • , , PHP HTML.

  • - (<?= <?php echo).

  • PHP , HTML .

  • (HTML PHP), HTML, .

<?php
    $user = $file['User'];
    $comments = $file['Document']['comments'];
?>

<!-- User --> 

<?php if (!empty($user['email'])) : ?>

<div class="mailto"><?= $this->Html->link($user); ?></div>

<?php endif; ?>

<!-- File Comments -->

<?php if (!empty($comments)) : ?>

<div class="file-comment file-extra column grid_6">
    <div class="content"><?= $comments; ?></div>
</div>

<?php endif; ?>
0

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


All Articles