by
KK » Wed Feb 04, 2015 5:49 pm
Hi,
We can use 'relations' to create the 'tags' functionality (to be precise, a 'fixed tagging' functionality).
As a concrete example, suppose we have a blog.php template and we need to tag the individual posts.
For this, use a clonable template, say named 'tags.php' -
- Code: Select all
<cms:template title='Blog Tags' clonable='1' ></cms:template>
Relate the blog template with it by adding the following region to blog.php -
- Code: Select all
<cms:editable name='tags' type='relation' masterpage='tags.php' label='Tags' />
Now, the user can create all the tags terms as cloned pages of tags.php.
While creating a blog post, the tags will be shown in a listbox with multiple checkboxes.
On the front-end, we can use the following code to show the tags a blog post is related to -
- Code: Select all
<!-- blog.php - showing tags related to a post -->
<cms:capture into='my_tags'>
<cms:related_pages 'tags' >
<cms:show my_tags /><cms:if k_count gt '1'>,</cms:if> <a href="<cms:show k_page_link />"><cms:show k_page_title /></a>
</cms:related_pages>
</cms:capture>
<cms:if my_tags ><cms:show my_tags /></cms:if>
Each of the tag shown above leads to the tags.php template's page_view where we can display all the blog posts related to that particular term
- Code: Select all
<!-- tags.php - showing pages related to the tag (in page-view of a tag) -->
<cms:set my_custom_field_str="tags=<cms:show k_page_name />" />
<cms:pages masterpage='blog.php' limit='10' custom_field="<cms:show my_custom_field_str />">
... all data for blog posts ...
</cms:pages>
Finally, we can also create a
tag-cloud like this -
- Code: Select all
<!-- show tags that have atleast one post associated -->
<div class="tags"> <strong class="stitle"> Tags</strong>
<ul>
<cms:pages masterpage='tags.php' custom_field="blog.php::tags=ANY" >
<li><a href="<cms:show k_page_link />"><cms:show k_page_title /></a></li>
</cms:pages>
</ul>
</div>
<!-- same as above but with count display -->
<div class="tags"> <strong class="stitle"> Tags</strong>
<ul>
<cms:pages masterpage='tags.php' aggregate_by="blog.php::tags" >
<li><a href="<cms:show k_page_link />"><cms:show k_page_title /> (<cms:show k_rel_count />)</a></li>
</cms:pages>
</ul>
</div>
IMP: All code above uses Couch v1.4.5RC1 (http://www.couchcms.com/forum/viewtopic.php?f=5&t=8581) and above.Hope it helps.
Addendum: For the use of 'tags' to filter pages listing, please see a variation of this technique in the following thread -
http://www.couchcms.com/forum/viewtopic ... 43&p=19515