dev-master
9999999-devHandle complex layouts in symfony
MIT
The Requires
by Kamran Shahzad
bundle theme layout
Wallogit.com
2017 © Pedro Peláez
Handle complex layouts in symfony
LayoutBundle help users build layout with pace and provide support for fully customized multi-themes. This bundle provide regions(header, footer, sidebar) and blocks to build compact layout. this bundle provide all components necesssary for layout building e.g : Regions (Header, Footer, Sidebar) and Blocks, (*1)
LayoutBundle Provides the following features:, (*2)
, (*3)
Using composer, (*4)
``` bash $ composer require kamran/layout-bundle dev-master, (*5)
Add the KamranLayoutBundle to your AppKernel.php file:
new Kamran\LayoutBundle\KamranLayoutBundle();, (*6)
## Configurations. 1. Add the `kamran_layout` service in symfony config: # application/config/config.yml kamran_layout: _theme_bundle: 'AppFrontBundle' _theme_namespaces: ['App','Systems'] * `_theme_bundle` : Your theme bundle with namespace. * `_theme_namespaces` : Add bundles namespaces for collect blocks for theme regions 2. The themes directories will placed in AppFrontBundle. Here is directory tree. Now create a themes directory in anyone of your bundle as specified below * `themes` All themes will be placed inside this directory
[YourBundleName]->Resources->views->themes
## Themes Configuration file For handling themes you need to create a theme configuration file with name of 'layouts.xml' inside `themes` directory:
[YourBundleName]->Resources->views->themes->layouts.xml
In `layouts.xml` file you can define your theme rules. Here is the structure of `layouts.xml` file: ```xml <themes> <theme name="bluestar"> <layouts url="/" template="default.html.twig" > <templates> <index url="/" template="front.html.twig"></index> <about url="/about" template="content.html.twig" ></about> <terms url="/terms-and-conditions" template="content.html.twig"></terms> </templates> </layouts> </theme> </themes>
<themes> : This is main tag of layouts.xml file. All theme tags be will wrapped in this tag. so we can have multiple themes in this layout configuration file<theme> : Every theme will be wrapped inside its own theme tag, (*7)
[YourBundleName]->Resources->views->themes->bluestar
<layouts> : Inside every theme there is a layout tag. This tag has two attributes.
url: This attribute defines the prefix of url like '/', this means all root level urls will go inside this tag.template: This is the default template for your theme, if you leave this attribute blank then default template will be used. <templates> : templates tag contains all url rules tag.
<any_unique_tagname> : This tag will be a unique, it will have two attributes:
url : This url can be prefix or page urltemplate : The defined template name will be used. If left blank it will inherit template from parent tagHere are example layouts.xml file for nested urls, (*8)
<themes>
<theme name="bluestar">
<layouts url="/" template="default.html.twig" >
<templates>
<login url="/login"></login>
<dashboard url="/" template="dashboard.html.twig"></dashboard>
<contact url="/contact" template="contact.html.twig" >
<templates>
<add url="/add"></add>
<edit url="/modify" ></edit>
<search url="/search">
<templates>
<show url="/show"></show>
</templates>
</search>
<remove url="/remove" ></remove>
</templates>
</contact>
<help url="/help"></help>
</templates>
</layouts>
</theme>
</themes>
If you want to implement more then one theme follow the syntax below., (*9)
[YourBundleName]->Resources->views->themes->bluestar_frontend
[YourBundleName]->Resources->views->themes->bluestar_backend
<themes>
<theme name="bluestar_frontend">
<layouts url="/" template="default.html.twig" >
<templates>
<index url="/" template="front.html.twig"></index>
<about url="/about" template="content.html.twig" ></about>
<terms url="/terms-and-conditions" template="content.html.twig"></terms>
</templates>
</layouts>
</theme>
<theme name="bluestar_backend">
<layouts url="/administrator" template="default.html.twig" >
<templates>
<login url="/login"></login>
<dashboard url="/" template="dashboard.html.twig"></dashboard>
<contact url="/contact" template="contact.html.twig" >
<templates>
<add url="/add"></add>
<edit url="/modify" ></edit>
<search url="/search">
<templates>
<show url="/show"></show>
</templates>
</search>
<remove url="/remove" ></remove>
</templates>
</contact>
<help url="/help"></help>
</templates>
</layouts>
</theme>
</themes>
In your themes you must define parent template using url rules. This parent template is defined in layouts.xml file.
Here is a simple theme parent template:, (*10)
[YourBundleName]->Resources->views->themes->bluestar->default.html.twig
<!doctype html>
<html class="no-js" lang="en">
<head>
<title>Themes default page</title>
</head>
<body>
<!--body-->
{% block body %}{% endblock %}
<!--@body-->
</body>
</html>
Theme parent template is ready, now you need to implement this template with your project. Now add this line in your child template :, (*11)
{% extends themes %}
{% block body %}
.... child content
{% endblock %}
A theme templates may have many regions(Header, Footer, Sidebar) and inline blocks. Region is collection of blocks.
All regions that are to be used in theme are defined in layouts.xml file., (*12)
<themes>
<theme name="undp_frontend">
<layouts url="/" template="main-default.html.twig" >
<regions>
<sidebar></sidebar>
<header></header>
</regions>
<templates>
<index url="/" template="front.html.twig"></index>
<about url="/about"></about>
<terms url="/terms-and-conditions"></terms>
</templates>
</layouts>
</theme>
</themes>
<regions> : All regions are defined in this tag.
* <unique_regionname> : Define regions, (*13)
<!doctype html>
<html class="no-js" lang="en">
<head>
<title>Theme template</title>
</head>
<body>
{{ region('header') }}
{{ region('sidebar') }}
{% block body %}{% endblock %}
</body>
</html>
A region consists of many blocks. You can define any number of blocks in 'layout.xml', (*14)
A sub configuration file is created for every bundle that is included in the layout sub configuration file. Here is the pattern for creating sub configuration file, (*15)
[bundlename]_layout.xml : The file name starts with bundlename as prefix. Use lowercase letter for file name.[FrontBundle]->Resources->views->config->[frontbundle]_layout.xml
Here is the structure of '[frontbundle]_layout.xml' file., (*16)
<layout>
<regions>
<header>
<logo template="logo.html.twig"></logo>
<topmenu template="topmenu.html.twig"></topmenu>
</header>
<sidebar>
<sidemenu template="sidemenu.html.twig"></sidemenu>
</sidebar>
</regions>
</layout>
Create blog template file: All block template are placed in blocks directory., (*17)
[FrontBundle]->Resources->views->blocks->logo.html.twig
[FrontBundle]->Resources->views->blocks->topmenu.html.twig
[FrontBundle]->Resources->views->blocks->sidemenu.html.twig
block template example:, (*18)
<div class="topmenu">
.... block content
</div>
If you want to insert a block direct in theme template file, then you can use inline block option., (*19)
<layout>
<inlineblocks>
<loginform template="sidemenus.html.twig"></loginform>
</inlineblocks>
<regions>
<header>
<logo template="logo.html.twig"></logo>
<topmenu template="topmenu.html.twig"></topmenu>
</header>
</regions>
</layout>
`<inlineblocks>` : All regions are defined in this tag.
* `<unique_inlineblock>` : Define your bundle inline blocks for theme.
<!doctype html>
<html class="no-js" lang="en">
<head>
<title>Theme template</title>
</head>
<body>
{{ inlineblock('loginform') }}
{% block body %}{% endblock %}
</body>
</html>
class FrontHelper{
public static function getMenuOption(){
return array(
'menu'=>array(
'home'=> array(
'label'=>'Home',
'link'=> 'front_index',
),
'user'=> array(
'label'=>'Users',
'link'=> 'user_index',
),
'organization' => array(
'label' => 'Organization',
'link' => 'organizations_index',
),
)
);
}
}
[FrontBundle]->Resources->views->config->[frontbundle]_layout.xml
<layout>
<regions>
<header>
<topmenu template="topmenu.html.twig">\Namespace\FrontBundle\Helper\FrontHelper::getMenuOption</topmenu>
</header>
</regions>
</layout>
topmenu.html.twig
<!--nav-->
<ul id="large-nav" class="">
{% for k,v in menu %}
<li class="">
<a href="{{ path(v.link) }}">
<span>{{ v.label }}</span>
</a>
</li>
{% endfor %}
</ul>
<!--@nav-->
Issues and feature requests are tracked in the Github issue tracker., (*20)
The contribution for this bundle for public is open, anybody could help me to participate bugs, documentation and code., (*21)
This software is licensed under the MIT license. See the complete license file in the bundle:, (*22)
Resources/meta/LICENSE
Read the License, (*23)
Handle complex layouts in symfony
MIT
bundle theme layout