A decorator pattern allows us to wrap something.

I have such a question.

Is there a solution (pattern, approach) in order to drive into the variable the code of the page, which is between certain "markers"?

Those. in essence, repeat the functionality of the Heredoc syntax, but not the built-in tool in php.

Something like this plan (preferably without OOP, but you can offer a small OOP solution):

beginBlock($variable); <h1>Example...</h1> <div><?= $needCount ? $object->countRows : '' ?></div> ... endBlock($variable); 

Just do not ask "Why?". Sometimes you need. For example, when inside the html block complex calculations, and dvizhek old and implemented functional programming (without OOP). Why is Heredoc not suitable? Sometimes it does not suit (gives some discomfort) due to some IDE or for other reasons.

Once again - do not try to make me a good coder. I perfectly use in the life of the PLO. There is just a question - you just need an answer. Without philosophy.

Even if there is no need - just interested to know.

If there are such solutions - hint with a link or an example.

Thank you in advance!

    2 answers 2

     ob_start(); Тут любой код PHP, осуществляющий какой-то вывод $variable = ob_get_clean(); 

    Output control functions

    sandbox

       function foo(){ ?> <span>bar</span> <? } foo(); 

      Extremely not recommended method , however. You can store templates in the same XML, and deploy the function to php in html. And "it is sometimes necessary" - when "I do not want to do it normally." It is suitable even for one-time solutions to problems, because it significantly, simply at times, impairs the readability of the code, multiplies its flexibility and functionality by zero, replete with php closing / opening tags, and generally moveton.

      • you can store in anything (: but if you are given an old project where a bunch of html porridge hpml + php ... and you need to take some of the porridge for reuse - it does not seem normal to sit and take these masterpieces anywhere. There and so everything works barely barely) + people used to edit such and such files in such a place. here is just an option to take it into a variable with markers and paste where I need it. refactoring won't help there anymore - WebCoder
      • I can’t even imagine a situation where it would be impossible to write a simple parser / templating engine that either created a page through templates or by invoking user functions for outputting elements. This will take a couple of hours from strength, there would be a desire%) - SLy_huh