Coming from another framework, I needed a way to conditionally render 
sections/regions/(slots in symfony speak) in a layout based on whether they 
are empty or not.

The framework had no way to check that for me so I implemented the following 
helper class.

http://yiisns.googlecode.com/svn/trunk/yiisns/protected/helpers/Layout.php

The application of this class in the layout is as follows:

http://yiisns.googlecode.com/svn/trunk/yiisns/protected/views/layouts/main.php

If you notice this section:

<div id="canvas">
<?php
if(Layout::hasRegions('sidebar.left','sidebar.right'))
{
$tagClass = ' class="sidebars clearfix"';
}else if(Layout::hasRegions('sidebar.left'))
{
$tagClass = ' class="sidebar-left clearfix"';
}else if(Layout::hasRegions('sidebar.right'))
{
$tagClass = ' class="sidebar-right clearfix"';
}else{
$tagClass = '';
}
?>
<div id="canvas-content"<?php echo $tagClass; ?>>

<?php if(Layout::hasRegion('sidebar.left')): ?>
<!-- sidebar-left -->
<div id="sidebar-left" class="sidebar">
<div class="sidebar-content">
<?php Layout::renderRegion('sidebar.left'); ?>
</div>
</div>
<!-- /sidebar-left -->
<?php endif; ?>

<div id="content-container">
<?php $this->widget('FlashMessengerWidget', 
array('categories'=>array('success','error','warning'))); ?>
<!-- content -->
<div id="content">
<?php echo $content; ?>
</div>
<!-- /content -->
</div>

<?php if(Layout::hasRegion('sidebar.right')): ?>
<!-- sidebar-right -->
<div id="sidebar-right" class="sidebar">
<div class="sidebar-content">
<?php Layout::renderRegion('sidebar.right'); ?>
</div>
</div>
<!-- /sidebar-right -->
<?php endif; ?>

</div>
</div>

The class method Layout::hasRegion('region identifier') checks if content 
has been assigned to the "slot" "region identifier" and displays markup. 

Granted, this code is kind of ugly to be placed in a layout, but it is 
something I could live with 

<?php
if(Layout::hasRegions('sidebar.left','sidebar.right'))
{
$tagClass = ' class="sidebars clearfix"';
}else if(Layout::hasRegions('sidebar.left'))
{
$tagClass = ' class="sidebar-left clearfix"';
}else if(Layout::hasRegions('sidebar.right'))
{
$tagClass = ' class="sidebar-right clearfix"';
}else{
$tagClass = '';
}
?>
<div id="canvas-content"<?php echo $tagClass; ?>>

I could render a 1 column layout, a 2 column layout and a 3 column layout 
from the same file by checking if each of the slots had content.

I was checking the documentation of SF2 if there is anything I could exploit 
to accomplish the same. I've not found anything yet, so I'm asking: Is there 
any known way I can accomplish this in Twig template or will I have to write 
my own helper?

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en

Reply via email to