Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
Hello,

I've come into trouble in creating tag cloud, if somebody know how to solve it, please help.

I have a gallery with <cms:editable name='genre' type='text' /> , so admins fill this field for each image. For example, for "image1.jpg" they write "green", for "image2.jpg" = "yellow", "image3.jpg" = "grey", and for "image4.jpg" they write "yellow".

So, the code
Code: Select all
<cms:pages>
<cms:if genre>
  <cms:show genre />,
</cms:if>
</cms:pages>

gives me "green, yellow, grey, yellow," . But I need the result to be "green, yellow, grey" - so the tags shouldn't be doubled. It would be better also if a string that have two or more inclusions (like "yellow" here) will have a custom class.

This task can't be resolved using pre-defined dropdown editable with certain values, because admins should be able to create new tags.

Only thing that I could do is removing doubled strings like that:
Code: Select all
<cms:pages><cms:if genre>
<cms:php>
  preg_match_all( '/\b<cms:show genre />\b/ui', '<cms:pages><cms:show genre /> </cms:pages>', $m);
if(count($m[0]) > 1)echo("");
else echo("<cms:show genre />, ");
</cms:php>
</cms:if></cms:pages>

So the result is "green, grey"

But have no idea how to render the doubled elements only once and with custom class. If anybody have an idea, please help
Hi Musman,

I think you'll be better off using 'relations' to simulate the tags functionality.

For example, you can create a clonable template named 'tags.php', create each tag as a cloned page.
Create a relation from your main template to this tag template ('Many-to-One' if a page can have only one tag else 'Many-to-Many').
Now the admins will see a list (or dropdown) of all available 'tags' while editing a page.

Except for the problem of having to go to the tags template every time a new tag is to be introduced, this approach will give you much greater flexibility and can simulate almost exactly how tags are conventionally used.
Hello KK,

Thank you, but relations don't have the simplicity for the end-users..

Finally I've changed my mind and solved the trouble with "array_unique". It doesn't allow to set custom classes on doubled items, but it suits for my needs of creating instant JS-filterable gallery with tags. The final simplified code is:
Code: Select all
<cms:php>
$tags_array = array_unique(array(<cms:pages><cms:if genre >"<cms:show genre />",</cms:if></cms:pages>));
foreach ($tags_array as $value) {echo "<a onclick='some_function_for_$value'> $value </a>";}</cms:php>

So the result for the first example is "green yellow grey".
I think I can even more improve this code, hope it will help others.

Anyway, thanks!
3 posts Page 1 of 1