Hello. I create blocks in admin panel. The block is html content. I assign each block a unique name, such as blockHead.

Question: how to implement the output of this block when inserting something like:

{blockHead} 

page code:

 <?php //подключение базы и прочих функций {blockHead} ?> 

The name of the blocks are stored in the database.

How to implement this?

  • one
    at a minimum, write a code for replacing this short code with the HTML-generated code. - Jean-Claude
  • one
    Template engine use. - Visman
  • @Denis, did I understand correctly that you initially assigned a set of block names, and the user only uses them like a content container? Ie the blocks are not generated, is it just a set? - Kirill Korushkin
  • @Visman can have any examples? What to read ... - iKey
  • @KirillKorushkin yes, that’s right -

1 answer 1

In your case it is more logical not to use the syntax of templating but stupidly to substitute blocks through conditions. Judging by the example in question, you do not use mvc and your logic is not separable from the presentation. Then why reinvent the wheel =)

For example, collect all user blocks by request into an array, in which the key is that block name (it is its position in the template) and the value is the insert code itself:

 Array [n]( "blockHead" => Array( "enabled" => "1", "content" => "...content..." ), ... "blockFoot" => Array( "enabled" => "1", "content" => "...content..." ) ) 

Well, then bring them in their places:

 <!--blockHead placing--> <?=($blocksArray['blockHead']['emabled']) ? $blockArray['blockHead']$ : '';?> <!--/blockHead placing--> 

ZY The honesty instilled in me by my grandmother as a child does not allow to call such an implementation correct (for many standards and terry postulates are violated), but quite reasonable in the framework of the question.