Question

I use sonataadmin bundle and twig in templates. There is the entity with text field. A typical html-code with urls may is contained in this text field . The content manager which edits the text shouldn't worry about the html-structure.

I want make a simple template, short code like

[template=button_block]{url=>name} {url2=>name2} ...[/template]

which content manager can paste in textarea form and on the page the text field is converted as html

<div class="labels">

      <div class="button">
         <a href="url1>name1</a>
      </div>

      <div class="button">
         <a href="url2>name2</a>
      </div>

      ...

</div>

What the solution's ways of this problem are there?

Was it helpful?

Solution

You could add the template text to the entity either as a default value in the entity class, or set the text in the data option for the create form, or you could assign the text to the entity in the controller when you define the new object and then pass it to the form.

All 3 approaches would give you sample text to edit in your form, but they arent the best solution in my opinion.

It would be much better to setup your entity properly with a OneToMany or ManyToMany relationship to a Link entity which stores URI and name for you. You could then control formatting for a list of links purely in the twig template. You can find a tutorial here http://symfony.com/doc/current/cookbook/form/form_collections.html which guides you through the process. This option will take you longer, but is the best way to do it as it keeps the view seperate, keeps the data cleaner, and makes it less likely for the user to be able to break the template by mistake.

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