如何以编程方式设置树枝块内容?

问题描述 投票:0回答:2

是否可以通过 PHP 代码设置 twig 模板块的值?我正在从不同的模板引擎迁移,我需要一个桥来设置块的值而不使用树枝模板。

我刚刚获得了我希望在渲染模板之前分配的纯文本。

php twig
2个回答
5
投票

如果您想在块内包含 PHP 文件,我建议您创建一个扩展。

样品

index.php

<?php

require(__DIR__ . '/../vendor/autoload.php');

$loader = new Twig_Loader_Filesystem('./');
$twig = new Twig_Environment($loader, array());

$function = new Twig_SimpleFunction('get_php_contents', function($file, $context) {
    ob_start();
    include($file); // $context is available in your php file
    return ob_get_clean();
}, array('is_safe' => array('html')));

$twig->addFunction($function);

echo $twig->render('test.twig', array('name' => 'Alain'));

测试.twig

{% extends 'base.twig' %}

{% block content %}
{{ get_php_contents('contents.php', _context) }}
{% endblock %}

base.twig

<html>
    <div>I'm a base layout</div>
    {% block content %}{% endblock %}
</html>

contents.php

<?php

echo '<div style="color:red">';
echo "Hello {$context['name']}, it is now: ";
echo date("Y-m-d H:i:s");
echo '</div>';

结果

<html>
    <div>I'm a base layout</div>
    <div style="color:red">Hello Alain, it is now: 2014-10-28 19:23:23</div>
</html>

0
投票

正如已接受的答案所暗示的那样,对所发布问题的直率答案是“你不能”。 Twig API 在这里没有提供任何东西。

另一个答案的 Twig 扩展在 Twig 中直接渲染 PHP 文件对于某些用例来说可能很直观,但它不适合我的。我想在一个 PHP 文件中完成所有操作,获取各种 Twig 模板并在其中发送数据。对我来说更合理的答案是让我的 PHP 文件只需要渲染一个 Twig 文件,而不是让一个 Twig 文件抓取并运行我的 PHP。我基本上是这样的:

card-list.twig(父组件)

{{ top_level_stuff }}

{% block the_cards_i_want_to_override %}
  {% for item in items %}
    {{ include('single-card.twig') }}
  {% endfor %}
{% endblock %}

single-card.twig(子组件)

<h1>{{ title }}</h1>
<div>{{ content }}</div>

card-from-post.twig(从一种内容类型映射到其卡片)

{{ include(
  'single-card.twig',
  {
    title: post.post_title,
    content: post.summary
  }
) }}

我(以为我)想做的是这样的:

card-list-from-posts.php(不能以这种方式工作)

$block_content = '';

// Loop over blog posts and provide them to my mapping template to build the content for the block override.
foreach($posts as $post) {
  // The following function doesn't exist; it's a placeholder for whatever your Twig environment would call for.
  $block_content .= render_twig_template_normally( 
    'card-from-post.twig',
    [ 'post' => $post ]
  );
}

// The following function also does not exist; it's a placeholder for what I wish existed, but does not in any form.
render_twig_template_with_block_override( 
  'card-list.twig',
  [ 'top_level_stuff' => 'whatever' ],
  [ 'the_cards_i_want_to_override' => $block_content ]
);

我最终要做的就是再创建一个 Twig 文件,如下所示:

card-list-from-posts.twig

{% embed 'card-list.twig' with {
  top_level_stuff: top_level_stuff
} %}
  {% block the_cards_i_want_to_override %}
    {% for post in posts %}
      {{ include('card-from-post.twig') }}
    {% endfor %}
  {% endblock %}
{% endembed %}

...然后将我的 PHP 更改为:

card-list-from-posts.php(有效)

render_twig_template_normally(
  'card-list-from-posts.twig',
  [
    'top_level_stuff' =>'whatever',
    'posts' => $posts,
  ]
);
© www.soinside.com 2019 - 2024. All rights reserved.