Using MB Blocks in Theme Templates

Support MB Blocks Using MB Blocks in Theme TemplatesResolved

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #42079
    dmccandmccan
    Participant

    Hello Meta Box Team,

    I would like to use Meta Box blocks in Site Editor templates. I watched the two videos about creating MB Blocks and they both showed manually filling in a value on the right side bar. That is fine for post content, but not for a template where you want the custom field value entered on the post edit screen to be dynamically used by the template.

    I have a CPT named Book Authors, which has an associated field group called Author Fields. One of the fields has the slug authors-website. All of the CPT records have a value for this URL field.

    https://share.getcloudapp.com/Bluz0Q74

    Using MB Builder I created a MB Block called Author Button Block. In the code area I've tried:

    {{ authors-website }}

    And I've tried:

    {% set post = mb.get_post( post_id ) %}
    {{ post.authors-website }}

    And I've tried this:

    {% set args = { 'post_type': 'book-author', } %}
    {% set someposts = mb.get_posts( args ) %}
    {% for thispost in someposts %}
    {{ thispost.post_title }}: {{ thispost.authors-website }}
    {% endfor %}

    In all cases the output value for the custom field is a zero '0', though the post title field is displayed correctly.

    Can the MB Blocks be used in a template to dynamically pull the value as I want? Thank you very much.

    #42089
    dmccandmccan
    Participant

    Now I've also tried:

    {% set post = mb.get_post( post_id ) %}
    {{ post.author-fields.authors-website }}
    #42104
    PeterPeter
    Moderator

    Hello,

    Do you mean to use the custom block in Full Site Editing (Appearance > Editor)? Currently, MB Blocks does not work with FSE, our development team is working on this feature to make the custom block works in FSE.

    #42112
    dmccandmccan
    Participant

    Thank you for the information.

    #42135
    Anh TranAnh Tran
    Keymaster

    Hi David,

    Can you please try changing the dash in the authors-website to underscore, e.g. authors_website? Twig doesn't allow dashes in the variable name.

    #42141
    dmccandmccan
    Participant

    Thank you for the suggestion. I made the change, but it just returns a zero. Note, this is a template created in the Full site Editor.

    #42142
    dmccandmccan
    Participant

    I made a video. It is on a new site that I just created. I never used dashes. Just returns 0, unless I am missing a step or the code is wrong.

    https://share.getcloudapp.com/YEu48kZr

    Thanks

    #42154
    Anh TranAnh Tran
    Keymaster

    The problem is how to refer to the field in Twig. The correct code that I tried is:

    {% set p = mb.get_post( post_id ) %}
    <p>
      <strong>Job title:</strong> {{ p.job_title }}
    </p>

    Here is the screencast: https://monosnap.com/file/Fh9dBfRYNYj0AZ4lYG3XwlGzcFXL97

    #42156
    dmccandmccan
    Participant

    Thank you. That works as shown in the video and in the comment above. In the video you point to the field group having underscores instead of dashes. I'm guessing that would come into play if I was doing a loop on a page, so I will keep that in mind. In my test for this case the field group name didn't matter, I guess because it was not referenced.

    I noticed that by default Meta Box puts dashes between the words in the field group name.

    #42157
    Anh TranAnh Tran
    Keymaster

    Dashes are not used by Twig, so we need to change them to underscores. Or you can get via the attribute method in Twig:

    https://twig.symfony.com/doc/3.x/functions/attribute.html

    The field group ID is the slug of the field group, and uses dashes by default. It’s rarely used in MB Blocks or MB Views, so it’s ok to keep it as it is.

    #42159
    dmccandmccan
    Participant

    @Anh Tran - Thank you for your help and the resource.

    #44383
    DaveDave
    Participant

    Do Metabox have an ETA on full compatibility with Site Editor, it's really a deal breaker on using blocks and you've been working on it for a while now?
    In site editor it's pretty easy to create blocks that will invalidate and break, that will cause console errors, that will have broken field layouts, that simply mess up in nearly every way possible.

    #44783
    Vinny GreenVinny Green
    Participant

    Any progress on this? I wish I didn't discover this issue when trying to build with blocks.

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