Tuesday, February 22, 2022

[FIXED] How can helper function render a template manually, and echo it

Issue

I have a Helper as the following:

 class IconHelper extends Helper{

     public function showList(){
         //render a template from ctp file
         $template = loadFromTemplate('path/to/my.ctp');
     }
 }

I want a function to render a .ctp template and return it .


Solution

I found View Cells:

In the case that your content can is small inline template, then you can use the Helper, as following:

 class IconHelper extends Helper{

 public function show($icon){

    $template = '<a class="btn btn-default">'.$icon.'</a>';

    return $template
 }

but in the case when the content is saved in a template CTP file, the best practice is to use the View Cells:

 //helper class
 class IconListCell extends Cell{
 public function display($icon){
      //script .....
       $this->set(copmat('icon));
   }
 }
 //file: src/Template/Cell/show.ctp
 <a class="btn btn-default" style="font-size: 40px;width: 70px;">
     <span class="'.$icon.'" id="icon-value-button" data-pack="default">/span>
 </a>


Answered By - Eymen Elkum

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.