php - Symfony2 : Add method to an Entity -


i add method entity. method returns x characters entity's title, , print title in twig file. possible ?

twig :

<ul class="nav nav-pills nav-stacked"> {% blog in listblogs %}     <li>         <a href="{{ path('l3_blog_view', {'id': blog.id}) }}">             {{ blog.gettruncatedtitle }}         </a>     </li> {% endfor %} 

entity :

class blog {  /**  * @var string  *  * @orm\column(name="content", type="text")  */ private $content;  public function settruncatedtitle($content) {     $this->content = "hey";      return $this; }  /**  * content  *  * @return string   */ public function gettruncatedtitle() {     return $this->content; } 

}

looking @ code replace content simple string. in template file not call method @ all, directly access getter. x characters means? if want truncate title , return part of it, symfony providing handy filters that. have register service:

services: twig.extension.text:     class: twig_extensions_extension_text     tags:         - { name: twig.extension } 

after simple {{ blog.content | truncate(50) }}. if want preserve words, include second argument, | truncate(50, true). if want custom separator, include 3rd argument.

if did not understood question correctly, let me know.

- update -

if want define custom method specific logic property, this:

public function gettruncatedtitle() {     $title = $this->title; // or $this->content - whatever suits needs.      // can apply custom logic here that.     return substr($title, 0, 10); } 

then calling blog.truncatedtitle invoke method.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -