Templating engine like Blade not rendering ?

Support MB Blocks Templating engine like Blade not rendering ?Resolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #21780
    JulienJulien
    Participant

    Hello,

    I tried to use mb blocks with blade (templating engine from laravel) and code isn't interpreted.

    I use a starter theme (Sage from Roots.io).

    Render

    And the code in my php template

    <?php echo 'Hello World'; ?>
    @php echo 'Hello World'; @endphp
    {{ 'Hello World' }}

    Do you think it's possible to fix that ?

    Thanks

    #21785
    JulienJulien
    Participant

    I found a solution with the render_callback, do you think it's possible to get the render_template value inside the callback ?

    With this value I can make only one render_callback for all of my custom blocks.

    There is the current solution for someone who need it

    public function my_hero_callback($attributes, $is_preview = false, $post_id = null){
    
        if (file_exists(get_theme_file_path("views/blocks/hero.blade.php"))) {
          echo \App\template("blocks.hero", [
            'attributes' => $attributes,
            'is_preview' => $is_preview,
            'post_id' => $post_id
          ]);
        }
    
      }
    #21788
    JulienJulien
    Participant

    We could maybe add information about render_template into the "set_block_data" function as follow ?

    Base

    private function set_block_data( &$attributes ) {
            $attributes['name'] = $this->id;
            $data = isset( $attributes['data'] ) ? $attributes['data'] : [];
            $this->storage->set_data( $data );
            ActiveBlock::set_block_name( $this->id );
        }

    Into

    private function set_block_data( &$attributes ) {
            $attributes['name'] = $this->id;
            $attributes['render_template'] = $this->render_template;
            $data = isset( $attributes['data'] ) ? $attributes['data'] : [];
            $this->storage->set_data( $data );
            ActiveBlock::set_block_name( $this->id );
        }
    #21798
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Please take a look at the function render_block() under the function set_block_data(),

    private function render_block( $attributes = [], $is_preview = false, $post_id = null ) {
        $this->enqueue_block_assests();
    
        if ( $this->render_callback ) {
            call_user_func( $this->render_callback, $attributes, $is_preview, $post_id );
            return;
        }
    
        if ( file_exists( $this->render_template ) ) {
            include $this->render_template;
        } else {
            locate_template( $this->render_template, true );
        }
    }

    if the callback is available, it will run the callback and end the function before call the template so it is impossible to run a template inside the callback. But thanks for the idea, I will discuss with the developer team to support this case.

    #21799
    JulienJulien
    Participant

    Thanks for your response but I did the trick on my first reply. Did you see it ?

    For the code on your reply, you didn't read code correctly, if the render_callback exist, the function render_callback is called and then stop because you have a "return;"

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.