Forum for discussing general topics related to Couch.
6 posts Page 1 of 1
Hi,
I would like to find a way of using multiple instances of the masterpage parameter to loop through different templates. I have a website where on the homepage, different articles generated by different templates are lined up based on time. So far, I can only use one masterpage parameter and this locks out articles made using a different template.

Regards
Hi Brian :)

The cms:pages tag, unfortunately, supports only a single 'masterpage'.

However, there is another tag in Couch 'cms:query' than can be used to execute raw SQL statements. We can use this to fetch pages from multiple masterpages, for example as follows where we use two templates 'actors.php' and 'movies.php' -
Code: Select all
<cms:query
    sql="SELECT p.id, p.template_id
        FROM <cms:php>echo K_TBL_PAGES;</cms:php> p
        inner join <cms:php>echo K_TBL_TEMPLATES;</cms:php> t
        on p.template_id = t.id
        WHERE (t.name='actors.php' or t.name='movies.php')
        AND publish_date < '<cms:date format='Y-m-d H:i:s' />'
        AND NOT publish_date = '0000-00-00 00:00:00'
        ORDER BY publish_date desc;"
    limit='10'
    paginate='1'
    fetch_pages='1'
    >

    <!-- variables from the pages fetched available here exactly like in cms:pages -->
    <a href="<cms:show k_page_link />"><cms:show k_page_title /></a><br />

</cms:query>

The only line in the SQL code above that you need to change is the following where you can add your own templates -
WHERE (t.name='actors.php' or t.name='movies.php')

e.g. add a third template
WHERE (t.name='actors.php' or t.name='movies.php' or t.name='directors.php')

Please also note that it is likely the templates will have regions of different names so you'll need to make a check of each fetched page's masterpage before displaying its variables e.g. as follows -
Code: Select all
<cms:query
    sql="SELECT p.id, p.template_id
        FROM <cms:php>echo K_TBL_PAGES;</cms:php> p
        inner join <cms:php>echo K_TBL_TEMPLATES;</cms:php> t
        on p.template_id = t.id
        WHERE (t.name='actors.php' or t.name='movies.php')
        AND publish_date < '<cms:date format='Y-m-d H:i:s' />'
        AND NOT publish_date = '0000-00-00 00:00:00'
        ORDER BY publish_date desc;"
    limit='10'
    paginate='1'
    fetch_pages='1'
    >

    <h3><a href="<cms:show k_page_link />"><cms:show k_page_title /></a></h3>
   
    <cms:if k_template_name='actors.php'>
        .. show regions from actors.php ..
    <cms:else_if k_template_name='movies.php' />
        .. show regions from movies.php ..
    </cms:if>

    <cms:paginator />
</cms:query>

Hope it helps.
Hi KK

This works perfectly for me

However there is one checkbox field that all my templates share called my_categories with values Review and Post

In my sidebar I have a widget 'Latest Reviews' and I want to draw all the reviews from all the pages.

What would I need to enter in the code to say only display pages that my_category = Review?

I tried the code below but no luck

Code: Select all
<cms:query
                                    sql="SELECT p.id, p.template_id
                                        FROM <cms:php>echo K_TBL_PAGES;</cms:php> p
                                        inner join <cms:php>echo K_TBL_TEMPLATES;</cms:php> t
                                        on p.template_id = t.id
                                        WHERE (t.name='page1.php')
                                        AND publish_date < '<cms:date format='Y-m-d H:i:s' />'
                                        AND NOT publish_date = '0000-00-00 00:00:00'
                                        ORDER BY publish_date desc;"
                                    limit='5'
                                    paginate='1'
                                    fetch_pages='1'
                                    my_category = 'Review'
                                    >
@srewebso, please also post definition of your checkbox editable.
Also, the above solution is for multiple templates, but your posted code shows a single template. If this is the case, normal cms:pages would do the job.
single template? wrote: WHERE (t.name='page1.php')
@srewebso, even if the fields in the templates have the same name ('my_categories' in your case), those *are* discrete fields in the database and have different IDs.

It won't be straightforward, I am afraid, to formulate a query that involves custom fields spread across multiple templates.
No worries I reverted back by using one template and a whole bigger chunck of editable fields grouped in pages. Then using the 'if empty show nothing' functions to hide irrelevant fields.
6 posts Page 1 of 1
cron