Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

conditional nested blocks

edited July 2013 in Apps

I've noticed that I can embed a block in another block by entering:

<span class="embedded" data-embed="blocks/block-id" data-label="Embedded Block" title="Click to edit"></span>

can you do so conditionally? Using for example, something like:

{% if id === 'mypage' %}
    <span class="embedded" data-embed="blocks/block-id1" data-label="Embedded Block" title="Click to edit"></span>
{% else %}
    <span class="embedded" data-embed="blocks/block-id2" data-label="Embedded Block" title="Click to edit"></span>
{% end %}

Also I would like to create a dynamic object that allows site manager to insert a block from the blocks table. I created:

/apps/blocks/conf/embed.php

; <?php

[blocks/index]

label = "Embed a Block"

id[label] = Block
id[type] = select
id[require] = "apps/blocks/lib/Functions.php"
id[callback] = "block_list_all"
id[not empty] = 1
id[message] = Please choose a block.

; */ ?>

/apps/block/lib/Functions.php

<?php

/**
 * List all blocks for the dynamic objects dialog.
 */
function block_list_all () {
    $res = block\Block::query ('id, title')
        ->order ('title asc')
        ->fetch_assoc ('id', 'title');
    $out = array ();
    foreach ($res as $k => $v) {
        $out[] = (object) array (
            'key' => $k,
            'value' => $v
        );
    }
    return $out;
}

/**
 * Get a count of the results for a particular block.
 */
function block_results_count ($id) {
    return block\Results::query ()
        ->where ('block_id', $id)
        ->count ();
}

?>

Am I on the right track?

Thanks for any help.

Comments

  • You can't really add conditions inside a block's body, because it uses Template::run_includes instead of Template::render.

    The solution is usually to write a short handler with the condition in a view template and then embed that, or to include the condition in the layout template instead of the page or block body.

    For the embeddable blocks, it looks like you just have to change block\Block to Block (there's no namespace in this case). Seems to work with that change.

  • Yeah, the "Embed a Block" works like a charm after changing what you suggested. I have not clear how to create the short handler with the condition. Is there any example in the docs to help me understand? Thanks a lot.

  • A handler is just a PHP script in an app's handlers folder, so it should just be a matter of putting an if statement in there. Let me know what you're trying to switch between and I can put together an example for you.

  • The first goal is to include the blocks based on page id and user role.

  • Hmm, you may be able to just add a few blocks to your layout to do this. For example:

    {! blocks/index?id=admin-links !}
    {! blocks/index?id=member-links !}
    {! blocks/index?id=public-[id]&fallback=public-default !}
    

    From here, create an admin-links block with Access set to Private, a member-links block with Access set to Member, and a public-default block with Access set to Public.

    On any page you want a specific block to replace public-default, click the "Add Page-Specific Block" link next to it and it will let you override the public-default block with one for that page instead, but still show public-default on all other pages that use that layout.

    The limitation above is that the public-default will still show below the member-links block when members are logged in, but if you don't want that then you could wrap that in an if statement in the layout too:

    {% if User::require_acl('content/member') %}
        {! blocks/index?id=member-links !}
    {% else %}
        {! blocks/index?id=public-[id]&fallback=public-default !}
    {% end %}
    

    Let me know if that helps.

  • Thank you, it is very useful to take advantage of the layout but my ultimate goal is to create a small interface to allow the site editor to include the blocks conditionally without putting hand to the layout.

Sign In or Register to comment.