Jump to content

list of image tags


jploch
 Share

Recommended Posts

Hey guys,

i want to put out a list with tags from an image field (thumbnail) of the page beeing viewed.

The tags should link to pages, with the same name as the tags. The Tags are seperates by "|" (instead of a space).

Here is my code (wich is not working):

function footerProjekte(){

$alltags = $page->thumbnail->tags;

echo "<ul>";
foreach($alltags as $tag) {
    echo "<li><a href='{$tag}'>{$tag}</a></li>";
}
echo "</ul>";

}

any ideas?

Thx!

Link to comment
Share on other sites

@LostKobrakai

thanks for your reply!

Iam still quite new to php and the api. The explode function is exactly, what I was looking for.

I tryed the code above, but its just outputting the "ul" and a "li" with a empty link in it.

If I just print $page->thumbnail->tags it shows the tags, so they definitely exist.

Another problem Iam facing is that some tags are like "corporate design" with a space in it, for the link I will need an array wich converts that to "corporate-design". 

Link to comment
Share on other sites

It still outputs an emtpy list.

any ideas why the tags are not showing up?

function footerProjekte(){

$alltags = explode("|", $page->thumbnail->tags);

echo "<ul>";
foreach($alltags as $tag) {
    echo "<li><a href='{$tag}'>{$tag}</a></li>";
}
echo "</ul>";

}

thx!

Link to comment
Share on other sites

$page is a local variable in template files just like each custom ones you define yourself. These are not somehow automatically available in functions. There are already other topics about that in the forum, just search for something like "variable scope" and "functions". 

  • Like 1
Link to comment
Share on other sites

$page is a local variable in template files just like each custom ones you define yourself. These are not somehow automatically available in functions. There are already other topics about that in the forum, just search for something like "variable scope" and "functions". 

THX again! that worked for me.

Now I only have to figure out the part with the $sanitizer.

Iam not sure how to use this and couldn‘t find an example. I want to apply the sanitizer to the array "$alltags" and save it in another array (eg.  $alltagsLinks)

here is an example of what I try to do (its not the right syntax)

$alltags = explode("|", $page->thumbnail->tags);
$alltagsLinks = $alltags->$sanitizer->pageName($value, true)
Link to comment
Share on other sites

Sanitizer docs are here: http://processwire.com/api/variables/sanitizer/

Maybe one of the $sanitizer->array() will do what you are after...?. 

I am not sure this will work, haven't tested: Edit: It will do what you are after. Tested and it works. But don't know how to pass the 'true' to it

$sanitizer->array($value, 'pageName');

Otherwise, loop through the array and sanitize each value as you save them in a new array

Edited by kongondo
Confirmed works
  • Like 1
Link to comment
Share on other sites

$sanitizer is a variable like $page or $pages as well.

// In functions replace $page with wire('page') and $sanitizer with wire('sanitizer')
$alltags = explode("|", $page->thumbnail->tags);

foreach($alltags as $tag){
  $sanitized = $sanitizer->pageName($tag, true);
  …
}
  • Like 1
Link to comment
Share on other sites

$sanitizer is a variable like $page or $pages as well.

// In functions replace $page with wire('page') and $sanitizer with wire('sanitizer')
$alltags = explode("|", $page->thumbnail->tags);

foreach($alltags as $tag){
  $sanitized = $sanitizer->pageName($tag, true);
  …
}

That worked!! Thank you so much! I Really learned something. Now I can finally use my functions in a separate file and feel more like a developer ;)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...