奏鸣曲块 - 传递自定义参数

时间:2015-04-20 08:16:09

标签: symfony sonata-admin

我是Sonata Block Bundle的新成员。 我想把一张地图放进我的块里。它使用一些JS库。上下文的功能,我需要传递不同的高度,宽度等...例如。 但我不知道它是否符合我的需要。

起初,我想使用Sonata Block,因为我的地图与某些服务有依赖关系。所以这很酷,我可以集中他们。

但是我可以传递调用我的块的父级的一些参数函数吗?

感谢您的回答。

Redfog

1 个答案:

答案 0 :(得分:1)

好的,如果我理解你的问题,你想要做的是将一些自定义参数传递给你正在执行块的php类的模板(你可以将块称为精确)。让我们开始吧:

让我们添加选项来传递高度属性:

{% sample render of your block %}
{{ sonata_block_render({'type':'your.block.id'}, {'height': 50}) }}

现在,在你的块服务(php / class)中。您必须在方法中添加此属性作为默认选项: setDefaultSettings ,如下所示:

public function setDefaultSettings(OptionsResolverInterface $resolver) {
    $resolver->setDefaults(array(
        // your options goes here, and we add our new option right after them
        'height' => null // or whatever suits your needs
    ));
}

最后,您只需从执行方法访问您的选项,如下所示:

public function execute(BlockContextInterface $blockContext, Response $response = null) {
    $settings = $blockContext->getSettings();
    // now your value can be access from $settings['height'];
}

如果您正在寻找的话,请告诉我。

相关问题