質問

Hello I'm using Symfony2 with bootstrap in my project. I would just like to ask for better solutions for this problem.

I tried to get all the tags stored from my database and assign them to the availableTags attribute of the tag-it plugin.

So I came up with this solution.

Here's my controller code:

/**
 * 
 * @Route("/ask", name="ask")
 * @Security( "has_role( 'ROLE_USER' )" )
 * @Method("GET")
 * @Template
 */
public function askAction() {

    $tags = $this->getDoctrine()->getRepository('VerySoftAskMeBundle:Tag')->findAll();


    $entity = new Question();
    $form = $this->createCreateForm($entity);

    return array(
        'entity' => $entity,
        'form' => $form->createView(),
        'tags' => $tags
    );
}

Here's my twig template:

<label class="col-lg-1 text-left askLabels" for="tagField">Tags</label>
<input id="tagField" type="text" class="form-control col-lg-11">
<input id="fieldTags" type="hidden" value="{{ tags|join(',') }}">

And here's my script:

$('#tagField').tagit({
    availableTags: $('#fieldTags').val().split(',')
});
役に立ちましたか?

解決

You can define it directly in your javascript, instead of doing this twice:

$('#tagField').tagit({
    availableTags: ["{{ tags|join('", "')|raw }}"]
});

And remove the hidden input:

<input id="fieldTags" type="hidden" value="{{ tags|join(',') }}">
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top