Question

How can I select a single tag from blog.tags? I am trying to link a single tag from blog.tags property. Right now when I hover over a single tag I get see the group of tags instead just the single one.

Tags

tags for the post

Group of tags when hovering over one tag:

group tags instead of just one

I am using a for loop for all blogs and it's properties in the twig file. I need to be able to select on each tag in the blog.tags field to link it, how do I set this up?

twig file

{% for blog in pagination %}
 {% for tag in blog.tagsArray %}
   <p>Tags: <span class="highlight"><a href="{{ path('AcmeDemoBundle_tag', { 'tag': tag }) }}">{{ tag }}</a></span></p><br><br>
 {% endfor %}    
{% endfor %}

entity

/**
 * @var string
 *
 * @ORM\Column(name="tags", type="text")
 */
private $tags;

/**
 * Set tags
 *
 * @param string $tags
 * @return Blog
 */
public function setTags($tags)
{
    $this->tags = $tags;

    return $this;
}

/**
 * Get tags
 *
 * @return string
 */
public function getTags()
{
    return $this->tags;
}

controller

public function indexAction($tag = null)
{
    $em = $this->getDoctrine()->getManager();

    $blogs = $em->getRepository('LSHealthFitnessBundle:Blog')
        ->getBlogs();

    $tags = $em->getRepository('LSHealthFitnessBundle:Blog')
        ->getTags();

    $postTags = $em->getRepository('LSHealthFitnessBundle:Blog')
        ->getPostsByTags($tag);

    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $blogs,
        $this->get('request')->query->get('page', 1)/*page number*/,
        5/*limit per page*/
    );

    return array(
        'blogs'      => $blogs,
        'tags'       => $tags,
        'postTags'   => $postTags,
        'pagination' => $pagination,
    );
}

List format: (per answer response)

enter image description here

Was it helpful?

Solution

You should write second for loop in you twig tamplate, like this:

{% for blog in pagination %}
    {% for tag in blog.tagsArray %}
        <a href="{{ path('AcmeDemoBundle_tag', { 'tag': tag }) }}">{{ tag }}</a>&nbsp;
    {% endfor %}
    <br/>
{% endfor %}

This will create a list of tag for each blog (each in separate line)

[EDIT]:

Add something like this to your Blog entity:

public function getTagsArray()
{
    return explode(',' ,$this->tags);
}

This will convert tags string to array (delimited by comma). Then you can get tags array by calling blog.tagsArray in twig. I have updated code above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top