A
A
Anton Medvedev2011-05-22 14:06:58
PHP
Anton Medvedev, 2011-05-22 14:06:58

Description of variables used in the template

Should I describe the variables used in the template?
For example, there is such a template (view.php):

<h1>Hello, <?=$user?>!</h1>


Now I'm doing something like this:

$view = new View('view.php');
$view->assign('user', $user->name);
echo $view->render();


Is it worth creating something like this for each template with a description of its variables:

class SomePage extends View
{
    /**
     * Current user name.
     * @var string
     */
    public $user;
}

$view = SomePage();
$view->user = $user->name;
echo $view->render();

Answer the question

In order to leave comments, you need to log in

6 answer(s)
L
LastDragon, 2011-05-22
@Elfet

My opinion: if further development is planned and / or coding / layout will be performed by third-party people, it’s worth it. If you write only for yourself - as you prefer, but when specifying properties, it becomes possible to use autocommit in templates, which eliminates the need to constantly look at the code.
By the way, it is not necessary to create properties, you can use @property:

/**
 * @property string user
 */
class SomePage extends View {
}

A
AndreDerMorgenstern, 2011-05-22
@AndreDerMorgenstern

I don't think there is a clear answer to this question. I would not do this: such a rule would multiply the amount of code many times over, but would not give a clear benefit.

K
Konstantin, 2011-05-22
@Norraxx

I know that you didn't ask this and that it will be very difficult to switch to Smarty, but still, use Smarty, you will sin less. www.smarty.net/

M
Maxim Dyachenko, 2011-05-22
@Mendel

I’ll tell you from my own experience - when you take your own code that you haven’t seen for several years, then sometimes with poor commentability it’s easier to rewrite :)
Of course, with the growth of professionalism, the code becomes more and more readable, but my humble opinion is that the description should always be. Moreover, it is often not always possible to understand what it means from the name of a variable. The defaults are different for everyone.

D
Denis Turenko, 2011-05-23
@Dennion

It is necessary to describe variables in the template engine, I even made an IDE for the template engine for such purposes in my bike . Convenient, visually shows variables and descriptions, in some places you can even move them. Everything is included from an external XML file, so it's easy to add new ones without getting into the IDE code. Of the minuses of the IDE - so far it only works in the windows.

N
neyronius, 2011-06-28
@neyronius

It is convenient to do this, in addition, in such classes, you can describe some display logic in the form of functions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question