Change layout of your Template dependent on Module

Recently there was a question in CMSMS Forum board how output of the page template could be changed depending on Module and Modules detail view template.
You can find the topic here: News mod: Detail View Settings for each category 

Actually this is very easy and can be done with almost any module that makes use of summary and detail template, like for example News, Products, CGBlog and other.

Before i write to much nonsense let's just have a look how it can be done with News module as it is a module that comes with CMSMS install and probably used the most.

First thing we will do is edit News detail tamplate.
To do this simply go to Content -> News and click on tab "Detail Templates" and open your Template, in our case just the Sample tempalte as by default.

{* set a canonical variable that can be used in the head section if process_whole_template is false in the config.php *}
{if isset($entry->canonical)}
    {assign var='canonical' value=$entry->canonical}
{/if}
{if $entry->postdate}
    <div id="NewsPostDetailDate">
        {$entry->postdate|cms_date_format}
    </div>
{/if}
<h3 id="NewsPostDetailTitle">{$entry->title|cms_escape:htmlall}</h3>
<hr id="NewsPostDetailHorizRule" />
{if $entry->summary}
    <div id="NewsPostDetailSummary">
        <strong>{eval var=$entry->summary}</strong>
    </div>
{/if}
{if $entry->category}
    <div id="NewsPostDetailCategory">
        {$category_label} {$entry->category}
    </div>
{/if}
{if $entry->author}
    <div id="NewsPostDetailAuthor">
        {$author_label} {$entry->author}
    </div>
{/if}
    <div id="NewsPostDetailContent">
        {eval var=$entry->content}
    </div>
{if $entry->extra}
    <div id="NewsPostDetailExtra">
        {$extra_label} {$entry->extra}
    </div>
{/if}
<div id="NewsPostDetailPrintLink">
    {$entry->printlink}
</div>
{if $return_url != ""}
    <div id="NewsPostDetailReturnLink">
        {$return_url}{if $category_name != ''} - {$category_link}{/if}
    </div>
{/if}
{if isset($entry->fields)}
    {foreach from=$entry->fields item='field'}
    <div class="NewsDetailField">
        {if $field->type == 'file'}
        {* this template assumes that every file uploaded is an image of some sort, because News doesn't distinguish *}
            <img src="{$entry->file_location}/{$field->value}"/>
        {else}
            {$field->name}: {eval var=$field->value}
        {/if}
    </div>
    {/foreach}
{/if}

Now all we have to do is add one single line in News detail template.
We simply assign $set_detail variable that we can use later in our page Template. 

{assign var='set_detail' value='1'}

As we have a variable available in our detail Template we can use it now in our page Template for example to show a sidebar if we are viewing News Detail page.
The first thing we will do is assigning a variable to our {content} tag so we can reuse it in Template.
To do this open your page Template and on top of the Template and add following

{content assign="mycontentvar"}

This gives us a variable that we can use in our page Template with {$mycontentvar}.
Now we can build our Template dependent on News Detail template like this. 

{if isset($set_detail)}
    <div class="content_left">
        <h1 class="title">{title}&lit;/h1>
        {$mycontentvar}
    </div>
    <div class="sidebar">
        <p>
            Some sidebar content
        </p>
    </div>
{else}
    <div class="content_full">
        <h1 class="title">{title}&lit;/h1>
        {$mycontentvar}
    </div>
{/if}

And there you go now you have a dynamic Template dependent on News module detail view.
If all you want is change your stylesheet like font and background colors or images you could simply change it like this.

{if isset($set_detail)}
    {cms_stylesheet name='detailviewstyle'}
{else}
    {cms_stylesheet}
{/if}

Comments