by
KK » Tue Aug 13, 2013 2:32 am
Hi Simon,
You have specified only one template the cms:query tag which makes it no different than using cms:pages tag directly.
Anyways, for simplicity sake let us continue with a single template.
Forget cms:query for a little while and place a regular cms:pages tagpair on your newsfeed template. Suppose it looks something like this -
- Code: Select all
<cms:pages masterpage='msport/index.php' limit='10' paginate='1'>
.. output whatever HTML markup you need here..
<h3><cms:show k_page_title /></h3>
<cms:paginator />
</cms:pages>
Run the template and make sure the output is correct.
Now add the following code just below the code we added above -
- Code: Select all
<cms:query
sql=" SELECT p.id pid, t.name tname
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='msport/index.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'>
<cms:pages masterpage=tname id=pid>
.. output whatever HTML markup you need here..
<h3><cms:show k_page_title /></h3>
</cms:pages>
<cms:paginator />
</cms:query>
Run the template. The new code should produce exactly the same output as that of one using cms:pages.
Of course, it'd be silly to use cms:query to do exactly the same thing as done much easily by cms:pages. However, since the raw SQL is available for tweaking, we can now add more than one template to it and this is where cms:query scores over cms:pages.
Suppose you have a second template named 'msport/blog.php'. We can add it to the SQL statement -
- Code: Select all
<cms:query
sql=" SELECT p.id pid, t.name tname
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='msport/index.php' or t.name='msport/blog.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'>
<cms:pages masterpage=tname id=pid>
.. output whatever HTML markup you need here..
<h3><cms:show k_page_title /></h3>
</cms:pages>
<cms:paginator />
</cms:query>
Now we'll have a list of the titles of pages belonging to both the templates.
Of course, we are only displaying the titles which is a common field for both the templates so we can get away with a single <cms:show k_page_title /> statement.
When it comes to displaying custom field data from both the templates, since the names of the fields would likely be unique to their respective templates, we'll have to test which template a page belongs to.
This is how we can do it -
- Code: Select all
<cms:query
sql=" SELECT p.id pid, t.name tname
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='msport/index.php' or t.name='msport/blog.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'>
<cms:pages masterpage=tname id=pid>
<cms:if k_template_name='msport/index.php' >
.. here we have fields belonging to 'msport/index.php' ..
<h3><cms:show k_page_title /></h3>
.. show other fields e.g...
<cms:show my_heading />
<cms:show my_image />
</cms:if>
<cms:if k_template_name='msport/blog.php' >
.. here we have fields belonging to 'msport/blog.php' ..
<h2><cms:show k_page_title /></h2>
.. show other fields e.g. ..
<cms:show my_desc />
<cms:show my_link />
</cms:if>
</cms:pages>
<cms:paginator />
</cms:query>
So, you see, inside the cms:pages used within cms:query, you deal with only a single page out of the list.
You can now add more templates to the mix and output whatever HMTL is required by you.
Hope this helps. Please let me know.
Thanks.