如何使用hinclude.js来嵌入异步内容
使用hinclude.js-javascript库能够异步嵌入控制器。这个嵌入的内容来自其他页面(或者控制器),symfony使用一个标准版本的render函数去配置hinclude标签:
|
{{ render_hinclude(controller('...')) }}
{{ render_hinclude(url('...')) }} |
|
<?php echo $view['actions']->render(
new ControllerReference('...'),
array('renderer' => 'hinclude')
) ?>
<?php echo $view['actions']->render(
$view['router']->url('...'),
array('renderer' => 'hinclude')
) ?> |
hinclude.js需要包含在你的网页中才能工作。
当使用一个控制器作为一个URL,你启用symfony的fragments
配置:
|
# app/config/config.yml
framework:
# ...
fragments: { path: /_fragment } |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="Http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<!-- ... -->
<framework:config>
<framework:fragments path="/_fragment" />
</framework:config>
</container> |
|
// app/config/config.php
$container->loadFromExtension('framework', array(
// ...
'fragments' => array('path' => '/_fragment'),
)); |
可以程序配置中设置全局默认内容(正在加载或者禁用javascript):
|
# app/config/config.yml
framework:
# ...
templating:
hinclude_default_template: hinclude.HTML.twig |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!-- app/config/config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<!-- ... -->
<framework:config>
<framework:templating hinclude-default-template="hinclude.html.twig" />
</framework:config>
</container> |
|
// app/config/config.php
$container->loadFromExtension('framework', array(
// ...
'templating' => array(
'hinclude_default_template' => array(
'hinclude.html.twig',
),
),
)); |
你能够定义每一个默认模板的render
函数(覆盖任何已经定义的全局默认模板):
|
{{ render_hinclude(controller('...'), {
'default': 'default/content.html.twig'
}) }} |
|
<?php echo $view['actions']->render(
new ControllerReference('...'),
array(
'renderer' => 'hinclude',
'default' => 'default/content.html.twig',
)
) ?> |
或者你也可以指定一个字符串,以现实默认内容:
|
{{ render_hinclude(controller('...'), {'default': 'Loading...'}) }} |
|
<?php echo $view['actions']->render(
new ControllerReference('...'),
array(
'renderer' => 'hinclude',
'default' => 'Loading...',
)
) ?> |