📜 ⬆️ ⬇️

Block system in CMS LiveStreet


LiveStreet is a wonderful and favorite system in Habré. However, if you look through the sites created with its help, it’s easy to see that most of them inherit two columns of standard templates.
But LS can boast a simple, but no less functional system of blocks. Why site owners do not use it? Perhaps the matter is in the missing documentation.
This is me today with your help and try to fix it.


Let's start



I will try to tell you about the system of blocks using an example. Sketch a simple test pattern that looks something like this:
')

so:
< div class = "container" >
< div class = "header" >
< / div >
< div class = "content" >
< div class = "leftside" >
< / div >
< div class = "maincontent" > < / div >
< / div >
< div class = "sub-content" >
< / div >
< div class = "footer" > < / div >
< / div >


Block class

Let's start by creating classes for our blocks, which must inherit the system class Block and implement the Exec () method.
Looking at the Block class, you will see that it only accepts the block parameters through its constructor and writes them into the $ aParams property, as well as gives us the opportunity to call the module methods through its wrapper __call ();

Here I will give the code for one of the central blocks. Let him display your latest topics.

< ? php

BlockLast extends block
{
public function Exec ( )
{
$ oUser = $ this - > User_GetUserCurrent ( ) ;
if ( $ oUser ) {
$ aTopics = $ this - > Topics_GetLastTopicsByUserId ( $ oUser - > getId ( ) , 60 * 60 * 24 ) ;
// In order to avoid conflict, create a local view object
$ oViewer = $ this - > Viewer_GetLocalViewer ( ) ;
$ sTopicsLast = $ oViewer - > Fetch ( 'block.topics_last.tpl' ) ;
// Pass the data to the global view object
$ this - > Viewer_Assign ( 'sTopicsLast' , $ sTopicsLast ) ;
}
}
}


Configuration

My favorite part. With the help of the standard LiveStreet config, we will be able to maximally flex the output of our blocks. I will give an example:

// Block created by us in the previous paragraph
$ config [ 'block' ] [ 'rule_topic' ] = array (
'path' => array (
'___ path.root.web ___ / blog / * $' ,
) ,
'action' => array (
'index' => array ( 'blog' ) ,
'new'
) ,
'blocks' => array (
'central' => array (
'last' => array ( 'priority' => 100 , 'params' => array ( ) )
)
) ,
'clear' => false ,
) ;

// Another block that we will describe later
$ config [ 'block' ] [ 'rule_user' ] = array (
'path' => array (
'___ path.root.web ___ / profile / * $' ,
'___ path.root.web ___ / settings / * $'
) ,
'action' => array ( ) ,
'blocks' => array (
'upper' => array (
'block.user.tpl'
)
)
) ;


As you can see the configuration of the blocks is a rather trivial task. The names of the parameters speak for themselves, so they will not have to understand them for a long time.



Template

The easiest step. The system will automatically search for a template for our block at the address _path_to_sablon_ / block._block_name_.tpl

For the topics block, we passed the $ sTopicsLast variable to the template. Just put it in it.

< div class = "block last-topics" >
< div class = "topics" >
{ $ sTopicsLast }
< / div >
< / div >


For the second block defined in the configuration, we will also create a simple template in the block.user.tpl file

< div class = "hello-user" >
Hi, { $ oUserCurrent - > getLogin ( ) }
< / div >


It remains only to add to the template that I gave at the beginning of the article, the output of our blocks. And again the example with the block we created

< div class = "container" >
< div class = "header" >
< div class = "upper" >
{ include file blocks. tpl sGroup = 'upper' }
< / div >
< / div >
< div class = "content" >
< div class = "leftside" >
{ include file sidebar. tpl }
< / div >
< div class = "maincontent" > < / div >
< / div >
< div class = "sub-content" >
{ include file blocks. tpl sGroup = 'central' }
< / div >
< div class = "footer" > < / div >
< / div >


And finally the blocks.tpl file

{ if isset ( $ aBlocks. $ sGroup ) }
{ foreach from = $ aBlocks. $ sGroup item = aBlock }
{ if $ aBlock. type == 'block' }
{ insert name = "block" block = $ aBlock. name params = $ aBlock. params }
{ / if }
{ if $ aBlock. type == 'template' }
{ include file = $ aBlock. name params = $ aBlock. params }
{ / if }
{ / foreach }
{ / if }


That's all! As you can see the scheme is both simple and flexible.

Thanks for attention!

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


All Articles