Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
4 posts Page 1 of 1
Hi

As the subject says, is it possible to display one calendar which gets the entries from 2 or more different cloneable templates by adding a additional masterpage?

Code: Select all
<cms:calendar date="<cms:gpc 'cal' />" masterpage='xyz_01.php'  masterpage='xyz_02.php' week_starts="1" show_future_entries='1' paginate='1' charset='utf8' >

And is it possible to give that entries a class to attach different colours to them?

Thank you very much
Hi,

The cms:calendar tag does not support multiple 'masterpage' parameters.

However, with a little effort, we can still make it display entries from multiple templates.
Following is a slightly modified version of the sample code found at http://www.couchcms.com/docs/concepts/e ... endar.html that shows entries from two templates. An explanation of how it does so follows the code.
Code: Select all
<cms:calendar date="<cms:gpc 'cal' />" >
<!-- -->
    <cms:set template_1='movies.php' />
    <cms:set template_2='actors.php' />
   
    <cms:archives masterpage=template_1 start_on=k_calendar_date stop_before=k_next_calendar_date show_future_entries='1' type='daily'>
        <cms:php>global $CTX; $CTX->set("template_1_<cms:show k_archive_date />", '<cms:show k_next_archive_date />', 'global'); </cms:php>
    </cms:archives>
   
    <cms:archives masterpage=template_2 start_on=k_calendar_date stop_before=k_next_calendar_date show_future_entries='1' type='daily'>
        <cms:php>global $CTX; $CTX->set("template_2_<cms:show k_archive_date />", '<cms:show k_next_archive_date />', 'global'); </cms:php>
    </cms:archives>

<!-- -->
    <table class="calendar_big">
        <tr>
            <th><a href="<cms:concat k_page_link '?cal=' k_prev_calendar_date />"> << </a></th>
            <th colspan="5"><cms:date k_calendar_date format='F Y' /></th>
            <th><a href="<cms:concat k_page_link '?cal=' k_next_calendar_date />"> >> </a></th>
        </tr>
        <tr>
            <cms:repeat count='7'>
            <td class="months_heading"><cms:zebra 'Su' 'M' 'T' 'W' 'Th' 'F' 'S'/></td>
            </cms:repeat>
        </tr>

        <cms:weeks>
            <tr>
            <cms:days >
                <!-- -->
                <cms:set template_1_has_pages="<cms:get "template_1_<cms:show k_date />" />" />
                <cms:set template_2_has_pages="<cms:get "template_2_<cms:show k_date />" />" />
                <!-- -->
               
                <cms:if k_timeline_position='present'>
                    <cms:set tdclass='today' />
                <cms:else />
                    <cms:set tdclass='' />
                </cms:if>
             
                <cms:if k_position='current_month' >
                    <cms:if template_1_has_pages || template_2_has_pages >
                        <td class='entries <cms:show tdclass />' align='center'>
                            <a href="#"><cms:show k_day /></a>
                            <ul>
                            <cms:if template_1_has_pages >
                                <cms:pages masterpage=template_1 start_on=k_date stop_before=template_1_has_pages show_future_entries='1' limit='2' skip_custom_fields='1'>
                                    <li>
                                        <a href="<cms:show k_page_link />"><cms:show k_page_title /></a>
                                    </li>
                                </cms:pages>
                            </cms:if>
                           
                            <cms:if template_2_has_pages >
                                <cms:pages masterpage=template_2 start_on=k_date stop_before=template_2_has_pages show_future_entries='1' limit='2' skip_custom_fields='1'>
                                    <li>
                                        <a href="<cms:show k_page_link />"><cms:show k_page_title /></a>
                                    </li>
                                </cms:pages>
                            </cms:if>
                           
                            </ul>
                        </td>
                    <cms:else />
                        <td class='<cms:show tdclass />' ><cms:show k_day /></td>
                    </cms:if>
                <cms:else />
                    <td class='other_month'><cms:show k_day /></td>
                </cms:if>
            </cms:days>
            </tr>
        </cms:weeks>
    </table>
</cms:calendar>

Explanation:
In the code above we totally remove the 'masterpage' parameter from the calendar tag.
By doing so, we simply use it to create the monthly calendar's markup easily.

We now take up the task of showing relevant entries onto ourselves.
To do so, at the very beginning of the calendar tag, we define the two templates we'll use
Code: Select all
<cms:set template_1='movies.php' />
<cms:set template_2='actors.php' />

Next, we use the cms:archives tag to provide us info about the days in the calendar's month that have published pages for the templates (using 'daily' archive)
Code: Select all
<cms:archives masterpage=template_1 start_on=k_calendar_date stop_before=k_next_calendar_date show_future_entries='1' type='daily'>
    <cms:php>global $CTX; $CTX->set("template_1_<cms:show k_archive_date />", '<cms:show k_next_archive_date />', 'global'); </cms:php>
</cms:archives>

<cms:archives masterpage=template_2 start_on=k_calendar_date stop_before=k_next_calendar_date show_future_entries='1' type='daily'>
    <cms:php>global $CTX; $CTX->set("template_2_<cms:show k_archive_date />", '<cms:show k_next_archive_date />', 'global'); </cms:php>
</cms:archives>

The trick above is to save those days as variables named e.g.
template_1_2013-02-22
template_2_2013-02-21

The values of these variables contain the next archive period - which, as we are using a daily archive, is the date of the day following the archives (we'll see shortly how we use this)
So a <cms:dump /> will reveal something like:
# template_1_2013-02-22: 2013-02-23
# template_1_2013-02-20: 2013-02-21
# template_1_2013-02-02: 2013-02-03
# template_2_2013-02-21: 2013-02-22
# template_2_2013-02-20: 2013-02-21


Now to use these variables.
We know that cms:days tag loops through the days in the month. As it iterates through each day, we check if the day has a variable that is named after it
Code: Select all
<cms:days >
    <!-- -->
    <cms:set template_1_has_pages="<cms:get "template_1_<cms:show k_date />" />" />
    <cms:set template_2_has_pages="<cms:get "template_2_<cms:show k_date />" />" />
    <!-- -->
...

If any of the two template's variables show that there are pages available, we simply use our trusty cms:pages tag to get the pages:
Code: Select all
<cms:if template_1_has_pages || template_2_has_pages >
    <td class='entries <cms:show tdclass />' align='center'>
        <a href="#"><cms:show k_day /></a>
        <ul>
        <cms:if template_1_has_pages >
            <cms:pages masterpage=template_1 start_on=k_date stop_before=template_1_has_pages show_future_entries='1' limit='2' skip_custom_fields='1'>
                <li>
                    <a href="<cms:show k_page_link />"><cms:show k_page_title /></a>
                </li>
            </cms:pages>
        </cms:if>
       
        <cms:if template_2_has_pages >
            <cms:pages masterpage=template_2 start_on=k_date stop_before=template_2_has_pages show_future_entries='1' limit='2' skip_custom_fields='1'>
                <li>
                    <a href="<cms:show k_page_link />"><cms:show k_page_title /></a>
                </li>
            </cms:pages>
        </cms:if>
       
        </ul>
    </td>
<cms:else />
    <td class='<cms:show tdclass />' ><cms:show k_day /></td>
</cms:if>

In the code above, notice how we are using the value of the variables we set (remember this value actually holds the next archive date i.e. the next day) to constrain our cms:pages listing to only a 24 hour period
start_on=k_date stop_before=template_1_has_pages

You can easily modify the code above to give a different class to each of the two templates.

I'm sorry if this all appears convoluted. Please go through the code comparing it with the original code and it will start making sense :)

Hope this helps. Do let us know.
Thanks.
Thank you very much for your quick help and support. The code works perfect. I modified it a bit to use it with a small calendar in the sidebar.

Maybe this post is good to shift it in the Tips and Tricks section.
Maybe this post is good to shift it in the Tips and Tricks section.

Done :)
4 posts Page 1 of 1